Module: MotionBindable::Bindable
- Defined in:
- lib/motion_bindable/bindable.rb
Overview
# Bindable Module
Allow attributes of an object to be bound to other arbitrary objects through unique strategies.
Instance Method Summary collapse
- #bind(strategy) ⇒ Object
- #bind_attributes(attrs, object = self) ⇒ Object
- #strategy_for(reference) ⇒ Object
- #unbind_all ⇒ Object
Instance Method Details
#bind(strategy) ⇒ Object
21 22 23 24 25 |
# File 'lib/motion_bindable/bindable.rb', line 21 def bind(strategy) @bindings ||= [] @bindings << strategy self end |
#bind_attributes(attrs, object = self) ⇒ Object
11 12 13 14 15 16 17 18 19 |
# File 'lib/motion_bindable/bindable.rb', line 11 def bind_attributes(attrs, object = self) attrs.each_pair do |k, v| case v when Hash then bind_attributes(v, object.send(k)) when Array then v.each { |v| bind strategy_for(v).new(object, k).bind(v) } else bind strategy_for(v).new(object, k).bind(v) end end end |
#strategy_for(reference) ⇒ Object
33 34 35 |
# File 'lib/motion_bindable/bindable.rb', line 33 def strategy_for(reference) Strategy.find_by_reference(reference) end |
#unbind_all ⇒ Object
27 28 29 30 31 |
# File 'lib/motion_bindable/bindable.rb', line 27 def unbind_all @bindings ||= [] @bindings.each { |b| b.unbind } @bindings = [] end |