Class: J2119::HasFieldConstraint

Inherits:
Constraint show all
Defined in:
lib/j2119/constraints.rb

Overview

Verify node has the named field, or one of the named fields

Instance Method Summary collapse

Methods inherited from Constraint

#add_condition, #applies

Constructor Details

#initialize(name) ⇒ HasFieldConstraint

Returns a new instance of HasFieldConstraint.



79
80
81
82
83
84
85
86
# File 'lib/j2119/constraints.rb', line 79

def initialize(name)
  super()
  if name.is_a?(String)
    @names = [ name ]
  else
    @names = name
  end
end

Instance Method Details

#check(node, path, problems) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/j2119/constraints.rb', line 93

def check(node, path, problems)
  if (node.keys & @names).empty?
    if @names.size == 1
      problems <<
        "#{path} does not have required field \"#{@names[0]}\""
    else
      problems <<
        "#{path} does not have required field from #{@names}"
    end
  end
end

#to_sObject



88
89
90
91
# File 'lib/j2119/constraints.rb', line 88

def to_s
  conds = (@conditions.empty?) ? '' : " #{@conditions.size} conditions"
  "<Field #{@names} should be present#{conds}>"
end