Sha256: e902029f87e8dfe72f736fe8b4e04cf08b768d2f5c3a289f454af464055c3ed5

Contents?: true

Size: 1 KB

Versions: 5

Compression:

Stored size: 1 KB

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|
        if v.is_a?(Hash) then bind_attributes(v, object.send(k)) 
        else bind strategy_for(v).new(object, k).bind(v)
        end
      end
    end

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

    def refresh
      @bindings.each { |b| b.refresh }
      self
    end

    private

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

    def underscore(str)
      str.gsub(/::/, '/')
      .gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
      .gsub(/([a-z\d])([A-Z])/,'\1_\2')
      .tr("-", "_")
      .downcase
    end

  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
motion_bindable-0.0.6 lib/motion_bindable/bindable.rb
motion_bindable-0.0.5 lib/motion_bindable/bindable.rb
motion_bindable-0.0.4 lib/motion_bindable/bindable.rb
motion_bindable-0.0.3 lib/motion_bindable/bindable.rb
motion_bindable-0.0.2 lib/motion_bindable/bindable.rb