Class: Quickbooks::XSD::Group

Inherits:
Object
  • Object
show all
Defined in:
lib/quickbooks/xsd/group.rb

Overview

Group of elements, so that we can refer to them by nameā€¦

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Group

Returns a new instance of Group.



7
8
9
10
11
12
13
14
# File 'lib/quickbooks/xsd/group.rb', line 7

def initialize(attributes={})
  @name = attributes.delete('name')
  @ref = attributes.delete('ref')
  @minOccurs = attributes.delete('minOccurs')
  @maxOccurs = attributes.delete('maxOccurs')
  @items = attributes.delete(:items)
  puts "EG Attributes: #{attributes.inspect}" unless attributes.empty?
end

Instance Attribute Details

#maxOccursObject (readonly)

Returns the value of attribute maxOccurs.



5
6
7
# File 'lib/quickbooks/xsd/group.rb', line 5

def maxOccurs
  @maxOccurs
end

#minOccursObject (readonly)

Returns the value of attribute minOccurs.



5
6
7
# File 'lib/quickbooks/xsd/group.rb', line 5

def minOccurs
  @minOccurs
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/quickbooks/xsd/group.rb', line 5

def name
  @name
end

#refObject (readonly)

Returns the value of attribute ref.



5
6
7
# File 'lib/quickbooks/xsd/group.rb', line 5

def ref
  @ref
end

Instance Method Details

#<<(something) ⇒ Object



20
21
22
# File 'lib/quickbooks/xsd/group.rb', line 20

def <<(something)
  items << something
end

#childrenObject



87
88
89
90
91
# File 'lib/quickbooks/xsd/group.rb', line 87

def children
  children = []
  items.map {|i| i.is_a?(Quickbooks::XSD::Element) ? children << i : children.concat(i.children)}
  children
end

#cloneObject



40
41
42
# File 'lib/quickbooks/xsd/group.rb', line 40

def clone
  self.class.new('name' => @name, 'ref' => @ref, 'minOccurs' => @minOccurs, 'maxOccurs' => @maxOccurs, :items => items.map {|i| i.clone})
end

#exists_in?(object) ⇒ Boolean

Returns:



57
58
59
# File 'lib/quickbooks/xsd/group.rb', line 57

def exists_in?(object)
  items.any? {|i| i.exists_in?(object)}
end

#find(key) ⇒ Object



61
62
63
# File 'lib/quickbooks/xsd/group.rb', line 61

def find(key)
  children.select {|i| i.name == key}[0]
end

#include?(key_or_xsd) ⇒ Boolean

Returns:



65
66
67
68
69
# File 'lib/quickbooks/xsd/group.rb', line 65

def include?(key_or_xsd)
  key_or_xsd.is_a?(String) ?
    children.collect {|e| e.name}.include?(key_or_xsd) :
    (key_or_xsd == self || items.any? {|i| i.include?(key_or_xsd)})
end

#index(key) ⇒ Object



71
72
73
# File 'lib/quickbooks/xsd/group.rb', line 71

def index(key)
  children.collect {|e| e.name}.index(key.to_s)
end

#inspectObject



36
37
38
# File 'lib/quickbooks/xsd/group.rb', line 36

def inspect
  items.map {|i| i.inspect}.join
end

#itemsObject



16
17
18
# File 'lib/quickbooks/xsd/group.rb', line 16

def items
  @items ||= []
end

#merge(attributes) ⇒ Object



24
25
26
# File 'lib/quickbooks/xsd/group.rb', line 24

def merge(attributes)
  self.class.new({'name' => @name, 'ref' => @ref, 'minOccurs' => @minOccurs, 'maxOccurs' => @maxOccurs, :items => items}.merge(attributes))
end

#merge!(attributes) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/quickbooks/xsd/group.rb', line 28

def merge!(attributes)
  @name = attributes.delete('name') || @name
  @ref = attributes.delete('ref') || @ref
  @minOccurs = attributes.delete('minOccurs') || @minOccurs
  @maxOccurs = attributes.delete('maxOccurs') || @maxOccurs
  return self
end

#repeatable?(key) ⇒ Boolean

Reports whether the named Quickbooks::XSD::Element or other xsd object is allowed more than once in its parent.

Returns:



76
77
78
# File 'lib/quickbooks/xsd/group.rb', line 76

def repeatable?(key)
  items.any? {|i| i.include?(key) && i.repeatable?(key)}
end

#required?(key_or_xsd) ⇒ Boolean

Reports whether the named Quickbooks::XSD::Element or other xsd object is required within its container.

Returns:



81
82
83
84
85
# File 'lib/quickbooks/xsd/group.rb', line 81

def required?(key_or_xsd)
  key_or_xsd.is_a?(String) ?
    items.any? {|i| i.include?(key_or_xsd) && i.required?(key_or_xsd)} :
    (key_or_xsd == self || items.select {|i| i.include?(key_or_xsd)}.any? {|i| i.required?(key_or_xsd)})
end

#validate(object, required = true) ⇒ Object

include Validations



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/quickbooks/xsd/group.rb', line 45

def validate(object,required=true)
  # Validate the object's contents
  puts "#{$DEBUG<<"\t"}Validating CONFORMS TO Group[#{name}]" if $DEBUG
  # 1) is it required?
  # 2) do all items exist?
  # 3) are all items valid?
  r = items.inject(Valean.new(nil, required, :exists => true)) {|r,i| r << i.validate(object,required?(i)); r}
  $DEBUG.chop! if $DEBUG
  puts "#{$DEBUG}\t- #{r}" if $DEBUG
  r
end