Class: Quickbooks::XSD::Choice
Instance Attribute Summary collapse
-
#sequence ⇒ Object
Returns the value of attribute sequence.
Instance Method Summary collapse
- #<<(something) ⇒ Object
- #children ⇒ Object
- #clone ⇒ Object
- #exists_in?(other) ⇒ Boolean
- #find(key) ⇒ Object
- #include?(key_or_xsd) ⇒ Boolean
- #index(key) ⇒ Object
-
#initialize(attributes) ⇒ Choice
constructor
A new instance of Choice.
- #inspect ⇒ Object
- #items ⇒ Object
- #name ⇒ Object
-
#repeatable?(key = nil) ⇒ Boolean
Reports whether the named Quickbooks::XSD::Element or other xsd object is allowed more than once in its parent.
-
#required?(key_or_xsd = nil) ⇒ Boolean
Reports whether the named Quickbooks::XSD::Element or other xsd object is required within its container.
-
#validate(object, required = true) ⇒ Object
true if present and valid, or optional and missing false if required and either missing or invalid content :invalid if optional and invalid content.
Constructor Details
#initialize(attributes) ⇒ Choice
Returns a new instance of Choice.
6 7 8 9 10 11 |
# File 'lib/quickbooks/xsd/choice.rb', line 6 def initialize(attributes) @minOccurs = (attributes.delete('minOccurs') || 1).to_i @maxOccurs = attributes.delete('maxOccurs') @items = attributes.delete(:items) puts "More attributes for Choice: #{attributes.inspect}" unless attributes.empty? end |
Instance Attribute Details
#sequence ⇒ Object
Returns the value of attribute sequence.
4 5 6 |
# File 'lib/quickbooks/xsd/choice.rb', line 4 def sequence @sequence end |
Instance Method Details
#<<(something) ⇒ Object
21 22 23 |
# File 'lib/quickbooks/xsd/choice.rb', line 21 def <<(something) self.items << something end |
#children ⇒ Object
87 88 89 90 91 |
# File 'lib/quickbooks/xsd/choice.rb', line 87 def children children = [] items.map {|i| i.is_a?(Quickbooks::XSD::Element) ? children << i : children.concat(i.children)} children end |
#clone ⇒ Object
29 30 31 |
# File 'lib/quickbooks/xsd/choice.rb', line 29 def clone self.class.new('minOccurs' => @minOccurs, 'maxOccurs' => @maxOccurs, :items => items.map {|i| i.clone}) end |
#exists_in?(other) ⇒ Boolean
54 55 56 |
# File 'lib/quickbooks/xsd/choice.rb', line 54 def exists_in?(other) items.any? {|i| i.exists_in?(other)} end |
#find(key) ⇒ Object
58 59 60 |
# File 'lib/quickbooks/xsd/choice.rb', line 58 def find(key) children.select {|i| i.name == key}[0] end |
#include?(key_or_xsd) ⇒ Boolean
62 63 64 65 66 |
# File 'lib/quickbooks/xsd/choice.rb', line 62 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
68 69 70 |
# File 'lib/quickbooks/xsd/choice.rb', line 68 def index(key) children.collect {|e| e.name}.index(key.to_s) end |
#inspect ⇒ Object
25 26 27 |
# File 'lib/quickbooks/xsd/choice.rb', line 25 def inspect sprintf("\n%s", items.map{|i|i.inspect.gsub(/\n/,"\n ")}.join("\n OR")) end |
#items ⇒ Object
17 18 19 |
# File 'lib/quickbooks/xsd/choice.rb', line 17 def items @items ||= [] end |
#name ⇒ Object
13 14 15 |
# File 'lib/quickbooks/xsd/choice.rb', line 13 def name "either of (#{children.collect {|e| e.name}.join(', ')})" end |
#repeatable?(key = nil) ⇒ Boolean
Reports whether the named Quickbooks::XSD::Element or other xsd object is allowed more than once in its parent.
73 74 75 76 77 78 79 80 |
# File 'lib/quickbooks/xsd/choice.rb', line 73 def repeatable?(key=nil) if key # puts "Checking repeatable: #{items.inspect}" repeatable? || items.any? {|i| i.include?(key) && i.repeatable?(key)} else @maxOccurs == 'unbounded' end end |
#required?(key_or_xsd = nil) ⇒ Boolean
Reports whether the named Quickbooks::XSD::Element or other xsd object is required within its container.
83 84 85 |
# File 'lib/quickbooks/xsd/choice.rb', line 83 def required?(key_or_xsd=nil) (key_or_xsd && @minOccurs > 0) || false end |
#validate(object, required = true) ⇒ Object
true if present and valid, or optional and missing false if required and either missing or invalid content :invalid if optional and invalid content
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/quickbooks/xsd/choice.rb', line 36 def validate(object,required=true) # Validate the object's contents puts "#{$DEBUG<<"\t"}Validating CONFORMS TO #Choice (#{required ? 'REQUIRED' : 'OPTIONAL'})" if $DEBUG # 1) Is it required? r = Valean.new(nil,required) # 2) Do any of the choices exist? (if not, choice is missing) # TODO: PROBLEM WITH THIS LINE -- DOESN'T ALWAYS FIND EXISTENCE!!! existing = items.select {|i| i.exists_in?(object)} r.exists! if existing.length > 0 # 3) Does ONLY ONE of the choices exist? (if not, choice itself is invalid) r.invalid!("Must only include 1 of #{children.collect {|e| e.name}.inspect}") if existing.length > 1 # 4) Is the existing choice valid? (if not, the item is invalid) r << existing[0].validate(object) if !existing.empty? && r.perfect? $DEBUG.chop! if $DEBUG puts "#{$DEBUG}\t- #{r}" if $DEBUG r end |