Class: Quickbooks::Option

Inherits:
Object show all
Defined in:
lib/quickbooks/option.rb

Overview

An Option is a tag attribute. Not all Elements have them. They might include things like :iterator or :iteratorID, or :onError instructions. Can validate that a given object conforms to the Option’s spec.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, default, use) ⇒ Option

Returns a new instance of Option.



56
57
58
59
60
61
# File 'lib/quickbooks/option.rb', line 56

def initialize(name, type, default, use)
  @name = name
  @type = type
  @default = default
  @use = use
end

Instance Attribute Details

#defaultObject

This way we can set our own defaults



49
50
51
# File 'lib/quickbooks/option.rb', line 49

def default
  @default
end

#nameObject (readonly)

Returns the value of attribute name.



48
49
50
# File 'lib/quickbooks/option.rb', line 48

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



48
49
50
# File 'lib/quickbooks/option.rb', line 48

def type
  @type
end

Class Method Details

.from_xsd(xsd) ⇒ Object



51
52
53
54
# File 'lib/quickbooks/option.rb', line 51

def self.from_xsd(xsd)
  #<Quickbooks::XSD::Attribute:0x18005ec @use=nil, @default=nil, @type="MACROTYPE", @name="defMacro">
  new(xsd.name, xsd.type, xsd.default, xsd.use)
end

Instance Method Details

#inspectObject

:nodoc:



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

def inspect #:nodoc:
  "<Option #{@name}#{@default ? " default="+@default.inspect : ''}#{@use ? ' '+@use.upcase : ''} #{@type.inspect}>"
end

#required?Boolean

Returns:



63
64
65
# File 'lib/quickbooks/option.rb', line 63

def required?
  @use == 'required'
end

#validate(object) ⇒ Object

Validates that the given object conforms to the Option’s spec.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/quickbooks/option.rb', line 72

def validate(object)
  errors = (object.respond_to?(:errors) ? object.errors : [])
  if required? && object[@name].nil?
    errors << "#{object.class.short_name} requires the #{@name} option."
    return false
  end
  # True if there is a value and it's valid, or if there is no value.
  if object[@name].nil?
    true
  else
    if @type
      if @type.validate({object[@name] => true})
        true
      else
        object.errors << "#{object.class.short_name}[#{@name}] is not valid. It must conform to #{@type.inspect}"
        false
      end
    else
      true
    end
  end
end