Class: ElementCollection

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/quickbooks/element_collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#typeObject (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.

Raises:

  • (RuntimeError)


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:

Returns:



51
52
53
# File 'lib/quickbooks/element_collection.rb', line 51

def dirty? #:nodoc:
  @dirty ||= false
end

#eachObject



3
4
5
# File 'lib/quickbooks/element_collection.rb', line 3

def each
  items.each {|e| yield e}
end

#empty?Boolean

:nodoc:

Returns:



33
34
35
# File 'lib/quickbooks/element_collection.rb', line 33

def empty? #:nodoc:
  items.empty?
end

#errorsObject



44
45
46
# File 'lib/quickbooks/element_collection.rb', line 44

def errors
  @errors ||= []
end

#inspectObject

:nodoc:



55
56
57
# File 'lib/quickbooks/element_collection.rb', line 55

def inspect #:nodoc:
  "[Collection of #{@type.short_name}: #{items.join(', ')}]"
end

#lengthObject

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_associationsObject

: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_aObject

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_elementObject

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_modelObject

: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

#valid?Boolean

Returns:



37
38
39
40
41
42
# File 'lib/quickbooks/element_collection.rb', line 37

def valid?
  errors.clear
  r = items.inject(Valean.new) {|r,e| r << e.validate; r}
  errors.concat(r.errors) if !r.errors.empty?
  r.perfect?
end