Class: MotionBindable::Strategy

Inherits:
Object
  • Object
show all
Defined in:
lib/motion_bindable/strategy.rb

Overview

Represents a binding strategy. Designed to be as flexible as possible.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, attr_name) ⇒ Strategy



23
24
25
26
# File 'lib/motion_bindable/strategy.rb', line 23

def initialize(object, attr_name)
  @attr_name = attr_name.to_sym
  self.object = object
end

Instance Attribute Details

#boundObject

Returns the value of attribute bound.



21
22
23
# File 'lib/motion_bindable/strategy.rb', line 21

def bound
  @bound
end

#objectObject

Returns the value of attribute object.



20
21
22
# File 'lib/motion_bindable/strategy.rb', line 20

def object
  @object
end

Class Method Details

.find_by_reference(object) ⇒ Object



14
15
16
17
18
# File 'lib/motion_bindable/strategy.rb', line 14

def self.find_by_reference(object)
  @strategies_map.reverse.find do |h|
    h[:candidates].one? { |e| object.is_a? e }
  end.fetch(:class)
end

.register_strategy(strategy, *objects) ⇒ Object



10
11
12
# File 'lib/motion_bindable/strategy.rb', line 10

def self.register_strategy(strategy, *objects)
  @strategies_map << { class: strategy, candidates: objects }
end

Instance Method Details

#attributeObject



46
47
48
# File 'lib/motion_bindable/strategy.rb', line 46

def attribute
  object.send(@attr_name)
end

#attribute=(value) ⇒ Object



50
51
52
# File 'lib/motion_bindable/strategy.rb', line 50

def attribute=(value)
  object.send(:"#{@attr_name.to_s}=", value)
end

#bind(bound) ⇒ Object



28
29
30
31
32
33
# File 'lib/motion_bindable/strategy.rb', line 28

def bind(bound)
  self.bound = bound
  on_bind

  self
end

#on_bindObject



42
43
44
# File 'lib/motion_bindable/strategy.rb', line 42

def on_bind
  refresh
end

#refreshObject

You can either choose to just override ‘#refresh` for objects that can’t be bound with callbacks. Or override ‘#on_bind` for objects that can be bound with a callback.



41
# File 'lib/motion_bindable/strategy.rb', line 41

def refresh; end

#unbindObject



35
36
# File 'lib/motion_bindable/strategy.rb', line 35

def unbind
end