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.

## One-way binding

Currently bindings are only one-way, i.e change in the arbitrary object affects the bindable object but not vice-versa.

Instance Method Summary collapse

Instance Method Details

#bind(strategy) ⇒ Object



26
27
28
29
30
# File 'lib/motion_bindable/bindable.rb', line 26

def bind(strategy)
  @bindings ||= []
  @bindings << strategy
  self
end

#bind_attributes(attrs, object = self) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/motion_bindable/bindable.rb', line 16

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



37
38
39
# File 'lib/motion_bindable/bindable.rb', line 37

def strategy_for(reference)
  Strategy.find_by_reference(reference)
end

#unbind_allObject



32
33
34
35
# File 'lib/motion_bindable/bindable.rb', line 32

def unbind_all
  @bindings.each { |b| b.unbind }
  @bindings = []
end