Module: MotionBindable::StrategyHelpers

Included in:
MotionBindable::Strategies::UILabel, MotionBindable::Strategies::UITextField
Defined in:
lib/motion_bindable/strategy_helpers.rb

Instance Method Summary collapse

Instance Method Details

#observe_object(&block) ⇒ Object

A generic method for observing an object’s attribute that is being bound to.

Where strategies deal with observing a large variety of possible bound objects, this method will only have to worry about a small set of possible objects being binded to, KVO should actually cover most cases.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/motion_bindable/strategy_helpers.rb', line 8

def observe_object(&block)
  @_observe_object_cb = block

  if defined?(MotionModel::Model) && object.is_a?(MotionModel::Model)
    @_observe_object_mode = :motion_model
    start_observing_motion_model
  else
    @_observe_object_mode = :kvo
    start_observing_kvo
  end
end

#observeValueForKeyPath(_, ofObject: _, change: change, context: _) ⇒ Object

NSKeyValueObserving Protocol



31
32
33
# File 'lib/motion_bindable/strategy_helpers.rb', line 31

def observeValueForKeyPath(_, ofObject: _, change: change, context: _)
  @_observe_object_cb.call(change[:old], change[:new])
end

#stop_observe_objectObject



20
21
22
23
24
25
26
27
# File 'lib/motion_bindable/strategy_helpers.rb', line 20

def stop_observe_object
  case @_observe_object_mode
  when :kvo then stop_observing_kvo
  end

  @_observe_object_mode,
  @_observe_object_block = nil
end