lib/cts/mpx/entries.rb in cts-mpx-1.0.1 vs lib/cts/mpx/entries.rb in cts-mpx-1.0.2

- old
+ new

@@ -4,43 +4,51 @@ # @attribute collection storage for the individual entry # @return [Entry[]] class Entries include Enumerable include Driver - extend Creatable + include Creatable + extend Forwardable attribute name: 'collection', kind_of: Array # Create a new entries collection from a page # @param [Page] page the page object to process # @raise [ArgumentError] if :page is not available # @return [Entries] a new entries collection def self.create_from_page(page) Exceptions.raise_unless_argument_error? page, Page - entries = new - page.entries.each do |e| - entry = Entry.new - entry.fields = Fields.create_from_data(data: e, xmlns: page.xmlns) + entries = page.entries.each do |e| + entry = Entry.create(fields: Fields.create_from_data(data: e, xmlns: page.xmlns)) entry.id = entry.fields['id'] if entry.fields['id'] - entries.add entry end - entries + Entries.create(collection: entries) end # Addressable method, indexed by entry object # @param [Entry] key the entry to return # @return [Self.collection,Entry,nil] Can return the collection, a single entry, or nil if nothing found def [](key = nil) return @collection unless key + @collection.find { |e| e.id == key } end + def +(other) + Entries.create collection: @collection += other.collection + end + + def -(other) + Entries.create collection: @collection += other.collection + end + # Add an entry object to the collection # @param [Entry] entry instantiated Entry to include # @raise [ArgumentError] if entry is not an Entry # @return [Self] def add(entry) return self if @collection.include? entry + Exceptions.raise_unless_argument_error? entry, Entry @collection.push entry self end