Class: Valean

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil, required = nil, others = {}) ⇒ Valean

Valean.new => (required, exists, valid) Valean.new(true) => (required, missing, valid) Valean.new(false) => (optional, missing, valid) Valean.new(false, :exists => true) => (optional, exists, valid)



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/quickbooks/xsd/validation.rb', line 25

def initialize(name=nil, required=nil, others={})
  @name = name
  if required.nil?
    @required = true
    @exists = true
  else
    @required = required
  end
  @valid = true
  others.each do |k,v|
    instance_variable_set("@#{k}", v)
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



20
21
22
# File 'lib/quickbooks/xsd/validation.rb', line 20

def name
  @name
end

Class Method Details

.perfect!(name, required = false) ⇒ Object



14
15
16
# File 'lib/quickbooks/xsd/validation.rb', line 14

def perfect!(name, required=false)
  Valean.new(name, required, :exists => true)
end

Instance Method Details

#<<(other) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/quickbooks/xsd/validation.rb', line 39

def <<(other)
  puts "Importing #{other} into #{self} (#{required?}):" if $DEBUG
  @exists = false if other.missing? && other.required?
  @valid  = false if other.invalid?
  errors.concat(other.errors.map {|i| name ? "#{name}: #{i}" : i})
  puts "#{self}" if $DEBUG
  self
end

#ensure(*bits) ⇒ Object



52
53
54
# File 'lib/quickbooks/xsd/validation.rb', line 52

def ensure(*bits)
  bits.all? {|bit| send("#{bit}?")}
end

#errorsObject



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

def errors
  @errors ||= []
end

#perfect?Boolean

Returns:



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

def perfect?
  v = valid? ? true : false
  required? ?
    (exists? ? v : false) :
    (exists? ? v : true)
end

#to_sObject Also known as: inspect



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/quickbooks/xsd/validation.rb', line 63

def to_s
  re = required? ? 'required' : 'optional'
  ex = exists?   ? 'exists' : 'missing'
  va = valid?    ? 'valid'  : 'invalid'
  # (invalid)           - required, exists but invalid
  # (perfect)           - required, exists and valid
  # (missing)           - required but missing
  # (invalid)           - optional, exists but invalid
  # (great)             - optional, exists and valid
  # (fine)              - optional but missing
  required? ?
    (exists? ? (valid? ? '(perfect)' : '(invalid)') : '(missing)') :
    (exists? ? (valid? ? '(great)' : '(invalid)') : '(fine)')
  # valid? ? ((required? && exists?) ? "(perfect)" : "(fine)") : (exists? ? "(#{re}, invalid)" : "(#{re}, missing)")
end