Class: Quickbooks::XSD::ComplexType

Inherits:
Type show all
Defined in:
lib/quickbooks/xsd/complex_type.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ ComplexType

Returns a new instance of ComplexType.



9
10
11
12
13
# File 'lib/quickbooks/xsd/complex_type.rb', line 9

def initialize(attributes={})
  @name = attributes.delete('name')
  @items = attributes.delete(:items)
  warn "!! Unhandled complex type attributes: #{attributes.keys.join(', ')}" if !attributes.empty?
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/quickbooks/xsd/complex_type.rb', line 6

def name
  @name
end

#restrictionObject

Returns the value of attribute restriction.



7
8
9
# File 'lib/quickbooks/xsd/complex_type.rb', line 7

def restriction
  @restriction
end

Instance Method Details

#<<(something) ⇒ Object



19
20
21
# File 'lib/quickbooks/xsd/complex_type.rb', line 19

def <<(something)
  items << something
end

#childrenObject



70
71
72
73
74
# File 'lib/quickbooks/xsd/complex_type.rb', line 70

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

#cloneObject



27
28
29
# File 'lib/quickbooks/xsd/complex_type.rb', line 27

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

#find(key) ⇒ Object



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

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

#include?(key_or_xsd) ⇒ Boolean

Returns:



48
49
50
51
52
# File 'lib/quickbooks/xsd/complex_type.rb', line 48

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



54
55
56
# File 'lib/quickbooks/xsd/complex_type.rb', line 54

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

#inspectObject



31
32
33
# File 'lib/quickbooks/xsd/complex_type.rb', line 31

def inspect
  sprintf("... >%s", items.map{|i|i.inspect.gsub(/\n/,"\n  ")})
end

#itemsObject



15
16
17
# File 'lib/quickbooks/xsd/complex_type.rb', line 15

def items
  @items ||= []
end

#merge(attributes) ⇒ Object



23
24
25
# File 'lib/quickbooks/xsd/complex_type.rb', line 23

def merge(attributes)
  self.class.new({'name' => @name}.merge(attributes))
end

#repeatable?(key) ⇒ Boolean

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

Returns:



59
60
61
# File 'lib/quickbooks/xsd/complex_type.rb', line 59

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:



64
65
66
67
68
# File 'lib/quickbooks/xsd/complex_type.rb', line 64

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



35
36
37
38
39
40
41
42
# File 'lib/quickbooks/xsd/complex_type.rb', line 35

def validate(object,required=true)
  # Validate the object's contents
  puts "#{$DEBUG<<"\t"}Validating CONFORMS TO #ComplexType #{name} (#{required ? 'REQUIRED' : 'OPTIONAL'})" if $DEBUG
  r = items.inject(Valean.new) {|r,i| r << i.validate(object,required?(i)); r}
  $DEBUG.chop! if $DEBUG
  puts "#{$DEBUG}\t- #{r}" if $DEBUG
  r
end