ConditionalDelegate
ConditionalDelegate allows delegates to be conditional.
Installation
Add this line to your application's Gemfile:
gem 'conditional_delegate'
And then execute:
$ bundle
Or install it yourself as:
$ gem install conditional_delegate
Usage
Here is a short example with a parent and child class. Calls to the child object are delegated to its parent if the condition for that delegation is met.
Simple parent class with four accessors:
class Parent
attr_accessor :foo, :bar, :baz, :qux
endChild class with the same accessors as the parent, and some conditional delegates defined:
class Child
extend ConditionalDelegate
attr_accessor :parent, :foo, :bar, :baz, :qux
conditional_delegate :foo, to: :parent, if: :empty?
conditional_delegate :bar, to: :parent, if: lambda { |value| value == "something" }
conditional_delegate :baz, :qux, to: :parent, if: :empty?
endThe field foo is delegated to the parent if the value of foo of the child is empty?.
The field bar is delegated to the parent if the given lambda returns true for the child's foo value.
Both the fields baz and qux are delegated to the parent if the child's field values are empty?.
parent = Parent.new
parent.foo = "parent foo"
parent.bar = "parent bar"
parent.baz = "parent baz"
parent.qux = "parent qux"
child = Child.new
child.foo = ""
child.bar = "something"
child.baz = ""
child.qux = "child qux"
child.parent = parent
child.foo # => "parent foo"
child.bar # => "parent bar"
child.baz # => "parent baz"
child.qux # => "child qux"Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Added some feature') - Push to the branch (
git push origin my-new-feature) - Create new Pull Request