README.md in configurations-1.1.0 vs README.md in configurations-1.3.0

- old
+ new

@@ -110,17 +110,85 @@ # c.foo = :not_so_foo # c.bar.baz = 'oh my cannot configure' end ``` +### Fourth way: Custom asserted or changed values + +If you need further assertions or you need to change a value before it gets stored in the configuration, consider passing a block + +``` +module MyGem + include Configurations + configurable :foo do |value| + + # The return value is what gets assigned, unless it is nil, + # in which case the original value persists + # + value + ' ooooh my' + end + configurable String, bar: :baz do |value| + + # value is guaranteed to be a string at this point + # + unless %w(bi ba bu).include?(value) + raise ArgumentError, 'baz needs to be one of bi, ba, bu' + end + end +end +``` + +Gives your users: + +``` +MyGem.configure do |c| + c.foo = 'FOO' + c.bar.baz = %w(bi) + + # This would raise the ArgumentError in the block + # c.bar.baz = %w(boooh) +end +``` + Gives you: ``` -MyGem.configuration.foo #=> 100% String -MyGem.configuration.bar.baz #=> 100% Array +MyGem.configuration.foo #=> 'FOO ooooh my' +MyGem.configuration.bar.baz #=> one of %w(bi ba bu) ``` +### Configuration Methods + +You might want to define methods on your configuration which use configuration values to bring out another value. +This is what `configuration_method` is here to help you with: + +``` +module MyGem + include Configurations + configurable :foo, :bar + configuration_method :foobar do |arg| + foo + bar + arg + end +end +``` + +Your users do: + +``` +MyGem.configure do |c| + c.foo = 'FOO' + c.bar = 'BAR' +end +``` + +You get: + +``` +MyGem.configuration.foobar('ARG') #=> 'FOOBARARG' +``` + + ### Defaults: ``` module MyGem include Configurations @@ -132,10 +200,9 @@ ### Get a hash if you need it ``` MyGem.configuration.to_h #=> a Hash - ``` ### Some caveats The `to_h` from above is along with `method_missing`, `object_id` and `initialize` the only purposely defined method which you can not overwrite with a configuration value.