Class: Quickbooks::XSD::Sequence
- Includes:
- Validation
- Defined in:
- lib/quickbooks/xsd/sequence.rb
Instance Method Summary collapse
- #<<(something) ⇒ Object
- #children ⇒ Object
- #clone ⇒ Object
- #exists_in?(object) ⇒ Boolean
- #find(key) ⇒ Object
-
#include?(key_or_xsd) ⇒ Boolean
Report whether a named Quickbooks::XSD::Element or any xsd object is a descendent of self.
-
#index(key) ⇒ Object
Report the position of a named Quickbooks::XSD::Element.
-
#initialize(attributes = {}) ⇒ Sequence
constructor
A new instance of Sequence.
- #inspect ⇒ Object
- #items ⇒ Object
-
#repeatable?(key) ⇒ Boolean
Reports whether the named Quickbooks::XSD::Element or other xsd object is allowed more than once in its parent.
-
#required?(key_or_xsd) ⇒ Boolean
Reports whether the named Quickbooks::XSD::Element or other xsd object is required within its container.
- #validate(object, required = true) ⇒ Object
Methods included from Validation
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 |
#children ⇒ Object
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 |
#clone ⇒ Object
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
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.
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 |
#inspect ⇒ Object
21 22 23 |
# File 'lib/quickbooks/xsd/sequence.rb', line 21 def inspect items.map {|i|i.inspect}.join end |
#items ⇒ Object
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.
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.
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 |