Cascading Configuration Setting

http://rubygems.org/gems/cascading-configuration-setting

Description

Adds methods for cascading configuration settings. Support package for cascading-configuration.

Summary

Cascading configuration methods for settings, which returns the appropriate lowest accumulated value. Configuration inheritance can cascade through modules, classes, and to instances.

This means that we can create configuration modules, optionally setting configuration defaults, and include those configuration modules in other modules or classes.

Install

  • sudo gem install cascading-configuration

Usage

Including the module will enable support for singleton and for instances.

module AnyModuleOrClass
  include CascadingConfiguration::Setting
end

Extending the module will enable support for singleton only.

module AnyModuleOrClass
  extend CascadingConfiguration::Setting
end

Including or extending CascadingConfiguration includes or extends:

  • CascadingConfiguration::Variable

:attr_configuration

:attr_configuration provides inheritable single-object configurations that cascades downward. The value lowest in the ancestor hierarchy will be returned.

Define initial configuration in a module or class:

module SomeModule

  include CascadingConfiguration::Setting

  attr_configuration :some_setting

  some_setting # => nil

  # note: if we don't specify the receiver here (self) assignment creates local variable instead
  self.some_setting = :some_value

  some_setting # => :some_value

end

Include initial module in a module or class:

class SomeClass

  include SomeModule

  some_setting # => :some_value

  self.some_setting = :some_other_value

  some_setting # => :some_other_value

  SomeModule.some_setting # => :some_value

end

And it cascades to instances:

instance = SomeClass.new

instance.some_setting.should == :some_value

instance.some_setting = :another_value

instance.some_setting.should == :another_value

:attr_module_configuration, :attr_class_configuration

:attr_class_configuration works like :attr_configuration but does not cascade to instances.

Define initial configuration in a module or class:

module SomeModule

  include CascadingConfiguration::Setting

  attr_class_configuration :some_setting

  some_setting # => nil

  self.some_setting = :some_value

  some_setting # => :some_value

end

Include initial module in a module or class:

class SomeClass

  include SomeModule

  some_setting # => :some_value

  self.some_setting = :some_other_value

  some_setting # => :some_other_value

  SomeModule.some_setting # => :some_value

end

And it does not cascade to instances:

instance = SomeClass.new

instance.respond_to?( :some_setting ).should == false

:attr_local_configuration

:attr_local_configuration works like :attr_configuration but does not cascade. This is primarily useful for creating local configurations maintained in parallel with cascading configurations (for instance, with the same variable prefixes), for overriding the local configuration method, and for hiding the configuration variable (coming soon).

Define initial configuration in a module or class:

module SomeModule

  include CascadingConfiguration::Setting

  attr_local_configuration :some_setting

  some_setting # => nil

  self.some_setting = :some_value

  some_setting # => :some_value

end

Include initial module in a module or class:

class SomeClass

  include SomeModule

  respond_to?( :some_setting ).should == false

end

Additional Functionality

Cascading-configuration also provides several other convenience functions.

Method Redefinition

Any declared configuration is defined in order to support locally redefining the method and accessing the original by calling super.

module SomeModule

  include CascadingConfiguration

  attr_configuration :some_array_setting

  def some_array_setting=( value )
    puts 'Replacing configuration array!'
    super
  end

end

Causes configuration variable to be stored in external context so that it is not included in instance variables.

License

(The MIT License)

Copyright (c) Asher

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.