Sha256: e500486dda8c864d16d4b7e35b74613dd5f57808246e63cddbc3634d9053463e

Contents?: true

Size: 938 Bytes

Versions: 5

Compression:

Stored size: 938 Bytes

Contents

module MotionBindable

  #
  # # 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.
  #
  module Bindable

    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

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

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

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

  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
motion_bindable-0.2.4 lib/motion_bindable/bindable.rb
motion_bindable-0.2.3 lib/motion_bindable/bindable.rb
motion_bindable-0.2.2 lib/motion_bindable/bindable.rb
motion_bindable-0.2.1 lib/motion_bindable/bindable.rb
motion_bindable-0.2.0 lib/motion_bindable/bindable.rb