Skip to content

daws/simply_configurable

Repository files navigation

Simply Configurable

Makes it simple to set arbitrary config key/value pairs on a class.

Synopsis:

require 'simply_configurable'

class Foo

  include SimplyConfigurable

  config :color => 'red'
  config :style => 'awesome'

  def style
    puts "my style is #{config[:style]}"
  end

  class << self
    def color
      puts "my color is #{config['color']}"
    end
  end

end

Foo.color  # prints 'my color is red'
foo = Foo.new
foo.style  # prints 'my style is awesome'

Foo.config  # returns { 'color' => 'red', 'style' => 'awesome' }
Foo.config[:color]  # returns 'red'

Foo.config :color => 'green'
Foo.config['color']  # returns 'green'

Class and Instance Access

The #config method is defined on both the class and its instances, so it may be simply called from either context.

Hash Access

This library uses the HashWithIndifferentAccess class of the ActiveSupport library, allowing you to access/set config values by either symbol or string.

Nesting

Any subclasses of a SimplyConfigurable class will also be SimplyConfigurable. The subclass will inherit all config values from its ancestors, but any values set on the subclass will not overwrite those on the ancestors.

For example:

class Foo
  include SimplyConfigurable

  config :color => 'red'
  config :style => 'awesome'
end

class Bar < Foo
  config :style => 'less awesome'
end

Foo.config[:style]  # returns 'awesome'
Bar.config[:style]  # returns 'less awesome'
Bar.config[:color]  # returns 'red'

About

Makes it simple to set arbitrary config key/value pairs on a class.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages