Sha256: 1562649207a07504a82b67110458844aba788b8985aa801a1cf880c279f28f1e

Contents?: true

Size: 1.09 KB

Versions: 3

Compression:

Stored size: 1.09 KB

Contents

module MotionBindable

  #
  # Represents a binding strategy. Designed to be as flexible as possible.
  #
  class Strategy

    @strategies_map = [{ class: Strategy, candidates: [Object] }]

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

    def self.find_by_reference(object)
      @strategies_map.reverse.find do |h|
        h[:candidates].include? object.class
      end.fetch(:class)
    end

    attr_accessor :object
    attr_accessor :bound

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

    def bind(bound)
      self.bound = bound
      on_bind

      self
    end

    # 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.
    def refresh; end
    def on_bind
      refresh
    end

    def attribute
      object.send(@attribute)
    end

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

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
motion_bindable-0.0.3 lib/motion_bindable/strategy.rb
motion_bindable-0.0.2 lib/motion_bindable/strategy.rb
motion_bindable-0.0.1 lib/motion_bindable/strategy.rb