Cascading Configuration Array
http://rubygems.org/gems/cascading-configuration-array
Description
Adds methods for cascading configuration arrays. Support package for cascading-configuration.
Summary
Cascading configuration methods for arrays, 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
end
Extending the module will enable support for singleton only.
module AnyModuleOrClass
extend CascadingConfiguration
end
Including or extending CascadingConfiguration includes or extends:
- CascadingConfiguration::Variable
:attr_configuration_sorted_unique_array
:attr_configuration_sorted_unique_array provides inheritable array configurations that cascade downward. A composite sorted and unique array will be returned (merging downward from most distant ancestor to self).
An internal cache is kept, and any configuration updates that occur to higher-level ancestors cascade immediately downward.
The array maintained by :attr_configuration_sorted_unique_array is kept ordered and unique.
Define initial configuration in a module or class:
module SomeModule
include CascadingConfiguration::Array
attr_array_configuration :some_array_setting
some_array_setting # => nil
some_array_setting.push( :some_value )
some_array_setting # => [ :some_value ]
end
Include initial module in a module or class:
class SomeClass
include SomeModule
some_array_setting # => [ :some_value ]
self.some_array_setting = [ :some_other_value ]
some_array_setting # => [ :some_other_value ]
some_array_setting.push( :another_value ) # => [ :another_value, :some_other_value ]
SomeModule.some_array_setting # => [ :some_value ]
end
And it cascades to instances:
instance = SomeClass.new
instance.some_array_setting.should == [ :another_value, :some_other_value ]
instance.some_array_setting.delete( :some_other_value )
instance.some_array_setting.should == [ :another_value ]
:attr_module_configuration_sorted_unique_array, :attr_class_configuration_sorted_unique_array
:attr_class_configuration_sorted_unique_array works like :attr_configuration_sorted_unique_array but does not cascade to instances.
module SomeModule
include CascadingConfiguration::Array
attr_array_configuration :some_array_setting
some_array_setting # => nil
some_array_setting.push( :some_value )
some_array_setting # => [ :some_value ]
end
Include initial module in a module or class:
class SomeClass
include SomeModule
some_array_setting # => [ :some_value ]
self.some_array_setting = [ :some_other_value ]
some_array_setting # => [ :some_other_value ]
some_array_setting.push( :another_value ) # => [ :another_value, :some_other_value ]
SomeModule.some_array_setting # => [ :some_value ]
end
And it does not cascade to instances:
instance = SomeClass.new
instance.respond_to?( :some_array_setting ).should == false
:attr_local_configuration_sorted_unique_array
:attr_local_configuration_sorted_unique_array works like :attr_configuration_sorted_unique_array 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).
module SomeModule
include CascadingConfiguration::Array
attr_array_configuration :some_array_setting
some_array_setting # => nil
some_array_setting.push( :some_value )
some_array_setting # => [ :some_value ]
end
Include initial module in a module or class:
class SomeClass
include SomeModule
respond_to?( :some_array_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
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.