lib/dolly/collection.rb in dolly-1.1.2 vs lib/dolly/collection.rb in dolly-1.1.3

- old
+ new

@@ -1,23 +1,18 @@ module Dolly - class Collection - extend Forwardable + class Collection < DelegateClass(Set) attr_accessor :rows attr_writer :json, :docs_class - def_delegators :@set, :clear, :empty?, :length, :+, :- - def initialize str, docs_class - @set = Set.new @docs_class = docs_class @json = str + initial = [] + super(initial) + load end - def first - to_a.first - end - def last to_a.last end def update_properties! properties ={} @@ -35,51 +30,31 @@ raise Dolly::MissingPropertyError unless json.match regex json.gsub! regex, "\"#{key}\":\"#{value}\"" end BulkDocument.new(Dolly::Document.database, to_a).save + clear + load self end - def map &block - load if empty? - @set.collect &block - end - - alias_method :collect, :map - - def flat_map &block - map( &block ).flatten - end - def each &block load if empty? - @set.each &block + super &block #TODO: returning nil to avoid extra time serializing set. nil end - def to_a - load if empty? - @set.to_a - end - - def count - load if empty? - length - end - def rows= ary ary.each do |r| next unless r['doc'] properties = r['doc'] id = properties.delete '_id' rev = properties.delete '_rev' if properties['_rev'] document = (docs_class || doc_class(id)).new properties document.doc = properties.merge({'_id' => id, '_rev' => rev}) - @set << document + self << document end - @rows = ary end def load parsed = JSON::parse json self.rows = parsed['rows']