Class: EasyJSONMatcher::Node

Inherits:
Validator show all
Includes:
ContentWrapper
Defined in:
lib/easy_json_matcher/node.rb

Instance Attribute Summary collapse

Attributes inherited from Validator

#content, #custom_validator, #errors, #key, #required

Instance Method Summary collapse

Methods included from ContentWrapper

#[], #keys, #method_missing

Methods inherited from Validator

#_check_content_type, #_check_required?, #_create_validator, #_custom_validator?, #_run_custom_validator, #valid?

Constructor Details

#initialize(opts: {}) ⇒ Node

Returns a new instance of Node.



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

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class EasyJSONMatcher::ContentWrapper

Instance Attribute Details

#strictObject (readonly)

Returns the value of attribute strict.



8
9
10
# File 'lib/easy_json_matcher/node.rb', line 8

def strict
  @strict
end

#validatorsObject (readonly)

Returns the value of attribute validators.



8
9
10
# File 'lib/easy_json_matcher/node.rb', line 8

def validators
  @validators
end

#validityObject (readonly)

Returns the value of attribute validity.



8
9
10
# File 'lib/easy_json_matcher/node.rb', line 8

def validity
  @validity
end

Instance Method Details

#_add_local_errors_to(child_errors) ⇒ Object



92
93
94
# File 'lib/easy_json_matcher/node.rb', line 92

def _add_local_errors_to(child_errors)
  child_errors.merge!({node_errors_: errors}) unless errors.empty?
end

#_collect_child_errorsObject



77
78
79
80
81
# File 'lib/easy_json_matcher/node.rb', line 77

def _collect_child_errors
  validators.each_with_object({}) do |val, container|
    container.merge!(val.get_errors)
  end
end

#_expected_keysObject



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

def _expected_keys
  validators.each_with_object([]) do |validator, keyset|
    keyset << validator.key
  end
end

#_get_content_for(key) ⇒ Object



56
57
58
# File 'lib/easy_json_matcher/node.rb', line 56

def _get_content_for(key)
  content[key]
end

#_get_validator_for(key) ⇒ Object



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

def _get_validator_for(key)
  validators[key]
end

#_is_root?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/easy_json_matcher/node.rb', line 68

def _is_root?
  key.nil?
end

#_no_errors?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/easy_json_matcher/node.rb', line 118

def _no_errors?
  validity && errors.empty?
end

#_parse_and_verify_json(json) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/easy_json_matcher/node.rb', line 110

def _parse_and_verify_json(json)
  begin
    JSON.parse(json)
  rescue JSON::ParserError
    errors << '#{json} is not a valid JSON String'
  end
end

#_post_initialise(options) ⇒ Object



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

def _post_initialise(options)
  @strict = options[:strict]
end

#_prep_root_content(candidate) ⇒ Object



106
107
108
# File 'lib/easy_json_matcher/node.rb', line 106

def _prep_root_content(candidate)
   candidate.is_a?(String) ? _parse_and_verify_json(candidate) : candidate
end

#_root_errors(child_errors) ⇒ Object



102
103
104
# File 'lib/easy_json_matcher/node.rb', line 102

def _root_errors(child_errors)
  errors.length > 0 ? {root: errors} : child_errors
end

#_set_content(candidate) ⇒ Object



64
65
66
# File 'lib/easy_json_matcher/node.rb', line 64

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

#_use_validator(validator) ⇒ Object



52
53
54
# File 'lib/easy_json_matcher/node.rb', line 52

def _use_validator(validator)
  validator.valid? self
end

#_validateObject



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

def _validate
  _validate_strict_keyset
  validators.each do |val|
    @validity = false unless _use_validator val
  end
end

#_validate_keysetObject



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

def _validate_keyset
  unexpected_keys = keys - _expected_keys
  errors << "#{unexpected_keys} found in addition to expected keys" unless unexpected_keys.empty?
end

#_validate_strict_keysetObject



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

def _validate_strict_keyset
  _validate_keyset if strict
end

#_wrap_child_errors(child_errors) ⇒ Object



96
97
98
99
100
# File 'lib/easy_json_matcher/node.rb', line 96

def _wrap_child_errors(child_errors)
  errors_wrapper = {}
  errors_wrapper[key] = child_errors
  errors_wrapper
end

#_wrap_errors(child_errors) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/easy_json_matcher/node.rb', line 83

def _wrap_errors(child_errors)
  _add_local_errors_to child_errors
  unless _is_root?
    _wrap_child_errors(child_errors)
  else
    _root_errors(child_errors)
  end
end

#add_validator(validator) ⇒ Object



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

def add_validator(validator)
  validators << validator
end

#get_errorsObject



72
73
74
75
# File 'lib/easy_json_matcher/node.rb', line 72

def get_errors
  child_errors = _collect_child_errors
  _wrap_errors(child_errors)
end

#reset!Object



31
32
33
34
35
# File 'lib/easy_json_matcher/node.rb', line 31

def reset!
  @validity = true
  errors.clear
  validators.each(&:reset!)
end