Class: ElementCollection
- Includes:
- Enumerable
- Defined in:
- lib/quickbooks/element_collection.rb
Instance Attribute Summary collapse
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#[](index) ⇒ Object
Access items by index.
-
#add_error(msg) ⇒ Object
:nodoc:.
-
#build(item = {}) ⇒ Object
(also: #<<)
Adds an item to the collection.
-
#dirty? ⇒ Boolean
:nodoc:.
- #each ⇒ Object
-
#empty? ⇒ Boolean
:nodoc:.
- #errors ⇒ Object
-
#initialize(parent, type, items = []) ⇒ ElementCollection
constructor
A new instance of ElementCollection.
-
#inspect ⇒ Object
:nodoc:.
-
#length ⇒ Object
Returns the number of items in the collection.
-
#save_associations ⇒ Object
:nodoc:.
-
#to_a ⇒ Object
Returns this collection but as a regular Array.
-
#to_element ⇒ Object
TODO: Has a problem when an element is a new_record and a child isn’t, or vice-versa.
-
#to_model ⇒ Object
:nodoc:.
- #valid? ⇒ Boolean
Constructor Details
#initialize(parent, type, items = []) ⇒ ElementCollection
Returns a new instance of ElementCollection.
8 9 10 11 12 |
# File 'lib/quickbooks/element_collection.rb', line 8 def initialize(parent, type, items=[]) @parent = parent @type = QB[type.to_s] (items.is_a?(Enumerable) ? items : [items]).each { |item| add_item(item) } end |
Instance Attribute Details
#type ⇒ Object (readonly)
Returns the value of attribute type.
7 8 9 |
# File 'lib/quickbooks/element_collection.rb', line 7 def type @type end |
Instance Method Details
#[](index) ⇒ Object
Access items by index.
15 16 17 |
# File 'lib/quickbooks/element_collection.rb', line 15 def [](index) items[index] end |
#add_error(msg) ⇒ Object
:nodoc:
47 48 49 |
# File 'lib/quickbooks/element_collection.rb', line 47 def add_error(msg) #:nodoc: errors << [nil, msg] end |
#build(item = {}) ⇒ Object Also known as: <<
Adds an item to the collection. The item type must be the same as the registered type for this Collection.
20 21 22 23 24 25 |
# File 'lib/quickbooks/element_collection.rb', line 20 def build(item={}) raise RuntimeError, "cannot introduce an existing #{@type.to_s} to a new #{self.class.short_name}!" if item.is_a?(Quickbooks::Model) && !item.new_record? && @parent.new_record? item = add_item(item) item.instance_variable_set(:@collection_index, items.length-1) return item end |
#dirty? ⇒ Boolean
:nodoc:
51 52 53 |
# File 'lib/quickbooks/element_collection.rb', line 51 def dirty? #:nodoc: @dirty ||= false end |
#each ⇒ Object
3 4 5 |
# File 'lib/quickbooks/element_collection.rb', line 3 def each items.each {|e| yield e} end |
#empty? ⇒ Boolean
:nodoc:
33 34 35 |
# File 'lib/quickbooks/element_collection.rb', line 33 def empty? #:nodoc: items.empty? end |
#errors ⇒ Object
44 45 46 |
# File 'lib/quickbooks/element_collection.rb', line 44 def errors @errors ||= [] end |
#inspect ⇒ Object
:nodoc:
55 56 57 |
# File 'lib/quickbooks/element_collection.rb', line 55 def inspect #:nodoc: "[Collection of #{@type.short_name}: #{items.join(', ')}]" end |
#length ⇒ Object
Returns the number of items in the collection
29 30 31 |
# File 'lib/quickbooks/element_collection.rb', line 29 def length items.length end |
#save_associations ⇒ Object
:nodoc:
80 81 82 |
# File 'lib/quickbooks/element_collection.rb', line 80 def save_associations #:nodoc: each { |el| el.save_associations if el.respond_to?(:save_associations) } end |
#to_a ⇒ Object
Returns this collection but as a regular Array.
76 77 78 |
# File 'lib/quickbooks/element_collection.rb', line 76 def to_a items.dup end |
#to_element ⇒ Object
TODO: Has a problem when an element is a new_record and a child isn’t, or vice-versa.
60 61 62 63 64 65 66 |
# File 'lib/quickbooks/element_collection.rb', line 60 def to_element #:nodoc: element_klass = @type < Quickbooks::Model ? (@type.short_name + (@parent.new_record? ? 'Add' : 'Mod')) : @type.short_name puts "Converting to #{element_klass}" elements = ElementCollection.new(@parent, element_klass) items.each {|e| elements << (@type < Quickbooks::Model ? e.to_element : e)} return elements end |
#to_model ⇒ Object
:nodoc:
68 69 70 71 72 73 |
# File 'lib/quickbooks/element_collection.rb', line 68 def to_model #:nodoc: # Strips off Add, Mod, or Ret to turn whatever it is into a Model object models = ElementCollection.new(@parent, @type.short_name.gsub(/(Add|Mod|Ret)$/,'')) items.each {|e| models << e.to_model} return models end |