Class: Quickbooks::XSD::Restriction

Inherits:
Object
  • Object
show all
Defined in:
lib/quickbooks/xsd/restriction.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Restriction

Returns a new instance of Restriction.



13
14
15
16
17
18
19
# File 'lib/quickbooks/xsd/restriction.rb', line 13

def initialize(attributes)
  @base = attributes.delete('base')
  @enum_values = attributes.delete(:enum_values)
  @pattern = attributes.delete(:pattern)
  @maxLength = attributes.delete(:maxLength)
  			puts "More attributes for Restriction: #{attributes.inspect}" unless attributes.empty?
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



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

def base
  @base
end

#maxLengthObject

Returns the value of attribute maxLength.



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

def maxLength
  @maxLength
end

#patternObject

Returns the value of attribute pattern.



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

def pattern
  @pattern
end

Instance Method Details

#cloneObject



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

def clone
  self.class.new('base' => @base.clone, :maxLength => @maxLength, :enum_values => enum_values.map {|i|i})
end

#enum_valuesObject



6
7
8
# File 'lib/quickbooks/xsd/restriction.rb', line 6

def enum_values
  @enum_values || []
end

#enum_values!Object



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

def enum_values!
  @enum_values ||= []
end

#inspectObject



21
22
23
24
25
26
27
28
# File 'lib/quickbooks/xsd/restriction.rb', line 21

def inspect
  insp = []
  insp << "#{@base.gsub(/(xsd:|TYPE)/,'').capitalize}" if @base
  insp << "max-length:#{@maxLength}" if @maxLength
  insp << "must-match:/#{@pattern}/" if @pattern
  insp << "choice-of:#{@enum_values.inspect}" if @enum_values && !@enum_values.empty?
  insp.join(', ')
end

#validate(object, required = true) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/quickbooks/xsd/restriction.rb', line 34

def validate(object,required=true)
  # Validate the object's contents
  puts "#{$DEBUG<<"\t"}Validating #{object.to_s.inspect} FOLLOWS #{inspect}" if $DEBUG
  r = Valean.new(nil, required, :exists => true)
  r.invalid!("exceeds the max length #{maxLength}") if maxLength && object.to_s.length > maxLength.to_i
  r.invalid!("doesn't match regexp #{pattern}") if r.perfect? && pattern && object.to_s !~ pattern
  r.invalid!("is not one of (#{enum_values.join(', ')})") if r.perfect? && !enum_values.empty? && !enum_values.include?(object.to_s)
  $DEBUG.chop! if $DEBUG
  puts "#{$DEBUG}\t- #{r}" if $DEBUG
  r
end