Class: Quickbooks::XSD::ComplexType
- Defined in:
- lib/quickbooks/xsd/complex_type.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#restriction ⇒ Object
Returns the value of attribute restriction.
Instance Method Summary collapse
- #<<(something) ⇒ Object
- #children ⇒ Object
- #clone ⇒ Object
- #find(key) ⇒ Object
- #include?(key_or_xsd) ⇒ Boolean
- #index(key) ⇒ Object
-
#initialize(attributes = {}) ⇒ ComplexType
constructor
A new instance of ComplexType.
- #inspect ⇒ Object
- #items ⇒ Object
- #merge(attributes) ⇒ 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
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
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/quickbooks/xsd/complex_type.rb', line 6 def name @name end |
#restriction ⇒ Object
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 |
#children ⇒ Object
78 79 80 81 82 |
# File 'lib/quickbooks/xsd/complex_type.rb', line 78 def children children = [] items.each {|i| i.is_a?(Quickbooks::XSD::Element) ? children << i : children.concat(i.children)} children end |
#clone ⇒ Object
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
52 53 54 |
# File 'lib/quickbooks/xsd/complex_type.rb', line 52 def find(key) children.select {|i| i.name == key}[0] end |
#include?(key_or_xsd) ⇒ Boolean
56 57 58 59 60 |
# File 'lib/quickbooks/xsd/complex_type.rb', line 56 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
62 63 64 |
# File 'lib/quickbooks/xsd/complex_type.rb', line 62 def index(key) children.collect {|e| e.name}.index(key.to_s) end |
#inspect ⇒ Object
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 |
#items ⇒ Object
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.
67 68 69 |
# File 'lib/quickbooks/xsd/complex_type.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/complex_type.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
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# 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 # If the object's contents are Enumerable (multiple values), we'll have to validate each of them individually # and return the combined result of all of their validations. r = if object.is_a?(Enumerable) && !object.is_a?(Hash) object.inject(Valean.new) do |r,obj| items.inject(r) {|r,i| r << i.validate(obj,required?(i)); r} end else items.inject(Valean.new) {|r,i| r << i.validate(object,required?(i)); r} end $DEBUG.chop! if $DEBUG puts "#{$DEBUG}\t- #{r}" if $DEBUG r end |