Class: Quickbooks::XSD::Sequence

Inherits:
Object
  • Object
show all
Includes:
Validation
Defined in:
lib/quickbooks/xsd/sequence.rb

Instance Method Summary collapse

Methods included from Validation

#validate_these

Constructor Details

#initialize(attributes = {}) ⇒ Sequence

Returns a new instance of Sequence.



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

def initialize(attributes={})
  @items = attributes.delete(:items)
  puts "More attributes for Sequence: #{attributes.inspect}" unless attributes.empty?
end

Instance Method Details

#<<(something) ⇒ Object



13
14
15
# File 'lib/quickbooks/xsd/sequence.rb', line 13

def <<(something)
  self.items << something
end

#childrenObject



78
79
80
81
82
# File 'lib/quickbooks/xsd/sequence.rb', line 78

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

#cloneObject



17
18
19
# File 'lib/quickbooks/xsd/sequence.rb', line 17

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

#exists_in?(object) ⇒ Boolean

Returns:



46
47
48
# File 'lib/quickbooks/xsd/sequence.rb', line 46

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

#find(key) ⇒ Object



50
51
52
# File 'lib/quickbooks/xsd/sequence.rb', line 50

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

#include?(key_or_xsd) ⇒ Boolean

Report whether a named Quickbooks::XSD::Element or any xsd object is a descendent of self.

Returns:



55
56
57
58
59
# File 'lib/quickbooks/xsd/sequence.rb', line 55

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

Report the position of a named Quickbooks::XSD::Element



62
63
64
# File 'lib/quickbooks/xsd/sequence.rb', line 62

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

#inspectObject



21
22
23
# File 'lib/quickbooks/xsd/sequence.rb', line 21

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

#itemsObject



9
10
11
# File 'lib/quickbooks/xsd/sequence.rb', line 9

def items
  @items ||= []
end

#repeatable?(key) ⇒ Boolean

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

Returns:



67
68
69
# File 'lib/quickbooks/xsd/sequence.rb', line 67

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:



72
73
74
75
76
# File 'lib/quickbooks/xsd/sequence.rb', line 72

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



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/quickbooks/xsd/sequence.rb', line 26

def validate(object,required=true)
  # Validate the object's contents
  puts "#{$DEBUG<<"\t"}Validating CONFORMS TO #Sequence (#{required ? 'REQUIRED' : 'OPTIONAL'})" if $DEBUG
  # 1) is it required?
  # 2) do all required children exist?
  # 3) are all existing children valid?
  r = Valean.new(nil, required, :exists => true)
  missings = []
  items.each do |i|
    v = i.validate(object,required?(i))
    r << v
    missings << (i.respond_to?(:name) ? i.name : i) if v.required? && v.missing?
  end
  # (a sequence is also invalid if any are missing)
  r.invalid!("missing required element#{'s' if missings.length > 1} #{missings.join(', ')}") if r.missing?
  $DEBUG.chop! if $DEBUG
  puts "#{$DEBUG}\t- #{r}" if $DEBUG
  r
end