Giter Club home page Giter Club logo

k-configurable's Introduction

K-Configurable

Make your classes configurable with this nifty little gem. Instead of implementing this over and over again, or copying it over and over again into every project where I need this, I decided to extract this tiny gem. Feel free to use it.

There are other gems that do something similiar with a different syntax, e.g.:

  1. dry-configurable
  2. Configurable
  3. simply_configurable
  4. ruby-configurable
  5. block_configurable

Without have looked too much at those, dry-configurable is probably the best choice since the guys over at dry have quite a few mini gems they maintain and use. Also, there are many, many more than those up there.

Installation

Add this line to your application's Gemfile:

gem 'k-configurable'

And then execute:

$ bundle

Or install it yourself as:

$ gem install k-configurable

Usage

Include the K::Configurable module in the class definition body of the class you would like to make configurable. For example, maybe you need to wrap a database connection with an adapter:

class DatabaseAdapter
  # make host, port, user and password configurable
  include K::Configurable[:host, :port, :user, :password]
  
  # ...
  
  # use configurable attributes to open a connection
  # which may look something like this:
  def connection
    config = self.class.configuration
    Connection.open(
      "#{config.user}:#{config.password}@#{config.host}:#{config.port}"
    )
  end
end

# configure it
DatabaseAdapter.configure do |config|
  config.host = 'localhost'
  config.port = '12345'
  config.user = 'foo'
  config.password = File.read('/run/secrets/dbpassword')
end

The configuration attributes are accessible via the configuration class method since I don't want them to pollute the class interface. If you however find it tedious to always have to refer to configuration and don't mind having those attribute setter and getter methods on your classes interface, then simply add some forwardables like this:

class DatabaseAdapter
  include K::Configurable[:host, :port, :user, :password]
  extend SingleForwardable
  def_single_delegators :configuration, :host, :port, :user, :password
end

Be careful about name collisions though. E.g., name would be a very bad attribute to delegate.

Configurable with Defaults

You can specify defaults like this:

class DatabaseAdapter
  include K::Configurable[:user, :password, host: 'localhost', port: 12345]
end

Defaults are implemented using named parameters, therefore you have to mind all the same rules you do when invoking any method in Ruby. I.e., you cannot mix named parameters and normal parameters, they have to be strictly ordered, normal parameters first, then named parameters.

Define methods on configuration

Do NOT use

It is possible to define methods during configuration definition, however, I do not like the way it is implemented and really have used it only in one instance yet. So use at own risk it may change without further notice. If you absolutely want to use it, it is at your own risk.. See the tests on how it works.

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/kaikuchn/k-configurable.

License

The gem is available as open source under the terms of the MIT License.

k-configurable's People

Contributors

kaikuchn avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.