Class: EasyJSONMatcher::Node

Inherits:
Validator show all
Extended by:
Forwardable
Defined in:
lib/easy_json_matcher/node.rb

Instance Attribute Summary collapse

Attributes inherited from Validator

#content, #key

Instance Method Summary collapse

Methods inherited from Validator

#valid?

Constructor Details

#initialize(opts: {}) ⇒ Node

Returns a new instance of Node.



14
15
16
17
# File 'lib/easy_json_matcher/node.rb', line 14

def initialize(opts: {})
  super(options: opts)
  @validators = []
end

Instance Attribute Details

#validatorsObject (readonly)

Returns the value of attribute validators.



10
11
12
# File 'lib/easy_json_matcher/node.rb', line 10

def validators
  @validators
end

Instance Method Details

#_get_content_for(key) ⇒ Object



34
35
36
# File 'lib/easy_json_matcher/node.rb', line 34

def _get_content_for(key)
  content[key.to_s]
end

#_get_validator_for(key) ⇒ Object



38
39
40
# File 'lib/easy_json_matcher/node.rb', line 38

def _get_validator_for(key)
  validators[key]
end

#_is_root?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/easy_json_matcher/node.rb', line 47

def _is_root?
  key.nil?
end

#_prep_candidate(json) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/easy_json_matcher/node.rb', line 51

def _prep_candidate(json)
  if json.is_a? String
    wrap_content(JSON.parse(json))
  else
    raise "Content for validation must be either a Hash or a String - you supplied #{json}, which is a #{json.class.name}" unless json.respond_to? :[]
    wrap_content(json)
  end
end

#_set_content(candidate) ⇒ Object



42
43
44
45
# File 'lib/easy_json_matcher/node.rb', line 42

def _set_content(candidate)
  candidate = _prep_candidate(candidate)
  @content = _is_root? ? candidate : candidate[key]
end

#_use_validator(validator) ⇒ Object



30
31
32
# File 'lib/easy_json_matcher/node.rb', line 30

def _use_validator(validator)
  validator.valid? self
end

#_validateObject



23
24
25
26
27
28
# File 'lib/easy_json_matcher/node.rb', line 23

def _validate
  validators.each do |val|
    return false unless _use_validator val
  end
  true
end

#add_validator(validator) ⇒ Object



19
20
21
# File 'lib/easy_json_matcher/node.rb', line 19

def add_validator(validator)
  validators << validator
end

#wrap_content(json) ⇒ Object



60
61
62
# File 'lib/easy_json_matcher/node.rb', line 60

def wrap_content(json)
  ContentWrapper.new(json)
end