lib/motion_bindable/strategy.rb in motion_bindable-0.2.5 vs lib/motion_bindable/strategy.rb in motion_bindable-0.3.0

- old
+ new

@@ -1,11 +1,7 @@ module MotionBindable - class Strategy - - WATCH_TICK = 0.2 - @strategies_map = [{ class: Strategy, candidates: [Object] }] def self.register_strategy(strategy, *objects) @strategies_map << { class: strategy, candidates: objects } end @@ -23,29 +19,38 @@ def initialize(object, attr_name) @attr_name = attr_name.to_sym self.object = object end - public # Methods to override + public # (Optional) Methods to override + # def start_observing; end # def start_observing_bound; end # def start_observing_object; end - # def refresh_bound; end # def on_object_change; end # def on_bound_change; end + def bound_value + end + + def object_value + end + def bind(bound) self.bound = bound initial_state start_listen self end def unbind - @watching = false end + # Deprecation support purposes + alias_method :refresh_bound, :bound_value + alias_method :refresh_object, :object_value + private # Methods to leave alone def attribute object.send(attr_name) end @@ -53,53 +58,20 @@ def attribute=(value) object.send(:"#{attr_name.to_s}=", value) end def initial_state + # We try to find an existing value and fill it up if attribute.nil? && respond_to?(:on_bound_change) - if respond_to?(:refresh_bound) then on_bound_change(refresh_bound) - else on_bound_change - end + on_bound_change(bound_value) elsif respond_to?(:on_object_change) - if respond_to?(:refresh_object) then on_object_change(refresh_object) - else on_object_change(attribute) - end + on_object_change(object_value) end end def start_listen - sides = [] - - if respond_to?(:start_observing_bound) then start_observing_bound - elsif respond_to?(:refresh_bound) && respond_to?(:on_bound_change) - sides << :bound - end - if respond_to?(:start_observing_object) then start_observing_object - elsif respond_to?(:refresh_object) && respond_to?(:on_object_change) - sides << :object - end - - @watching = true - watch(sides) + start_observing if respond_to?(:start_observing) + start_observing_bound if respond_to?(:start_observing_bound) + start_observing_object if respond_to?(:start_observing_object) end - - def watch(sides) - dispatcher.async do - if @watching - bound_result = refresh_bound if sides.include?(:bound) - object_result = refresh_object if sides.include?(:object) - on_bound_change(bound_result) if bound_result - on_object_change(object_result) if object_result - dispatcher.after(WATCH_TICK) { watch(sides) } - end - end - end - - def dispatcher - @dispatcher ||= begin - Dispatch::Queue.concurrent 'motion.bindable' - end - end - end - end