Sha256: 5249074dbfaef4b09ef6161b026ae08081d0659351fffbb5651ba445726bb042

Contents?: true

Size: 1.19 KB

Versions: 8

Compression:

Stored size: 1.19 KB

Contents

module Bowline
  module Binders
    class Collection < Base
      class ItemsProxy
        def initialize(&block)
          @callback = block
          @items = []
        end
        
        def real
          @items
        end
        
        def method_missing(*args, &block)
          diff = @items.hash
          res = @items.send(*args, &block)
          if diff != @items.hash
            @callback.call
          end
          res
        end
      end
      
      class << self
        def items=(args)
          if args
            items.replace(args)
          else
            items.clear
          end
        end
        
        def items
          @items ||= ItemsProxy.new {
            self.item_sync!
          }
        end
    
        def item_sync!
          return unless @items && @elements
          value = @items.real.map {|item|
            hash = item.to_js
            hash.merge!({:_id => item.__id__})
            hash.stringify_keys 
          }
          @elements.each {|i|
            i.updateCollection(value) 
          }
        end
    
        def find(id)
          @items.real.find {|item| 
            item.__id__ == id
          }
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
maccman-bowline-0.4.1 lib/bowline/binders/collection.rb
maccman-bowline-0.4.3 lib/bowline/binders/collection.rb
maccman-bowline-0.4.4 lib/bowline/binders/collection.rb
maccman-bowline-0.4.5 lib/bowline/binders/collection.rb
maccman-bowline-0.4.6 lib/bowline/binders/collection.rb
maccman-bowline-0.4.7 lib/bowline/binders/collection.rb
maccman-bowline-0.4.8 lib/bowline/binders/collection.rb
bowline-0.4.6 lib/bowline/binders/collection.rb