Class: JSONSkooma::Result
- Inherits:
-
Object
- Object
- JSONSkooma::Result
- Defined in:
- lib/json_skooma/result.rb
Instance Attribute Summary collapse
-
#annotation ⇒ Object
readonly
Returns the value of attribute annotation.
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#instance ⇒ Object
readonly
Returns the value of attribute instance.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#ref_schema ⇒ Object
writeonly
Sets the attribute ref_schema.
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
Instance Method Summary collapse
- #absolute_uri ⇒ Object
- #annotate(value) ⇒ Object
- #call(instance, key, schema = nil) {|child| ... } ⇒ Object
- #children ⇒ Object
- #collect_annotations(instance, key: nil, keys: nil) ⇒ Object
- #collect_errors(instance, key: nil, keys: nil) ⇒ Object
- #discard ⇒ Object
- #discard? ⇒ Boolean
- #each_children ⇒ Object
- #failure(error = nil) ⇒ Object
-
#initialize(schema, instance, parent: nil, key: nil) ⇒ Result
constructor
A new instance of Result.
- #output(format, **options) ⇒ Object
- #passed? ⇒ Boolean
- #path ⇒ Object
- #relative_path ⇒ Object
- #reset_with(instance, key, parent, schema = nil) ⇒ Object
- #root ⇒ Object
- #schema_node ⇒ Object
- #sibling(instance, key) ⇒ Object
- #skip_assertion ⇒ Object
- #success ⇒ Object
- #to_s ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(schema, instance, parent: nil, key: nil) ⇒ Result
Returns a new instance of Result.
9 10 11 |
# File 'lib/json_skooma/result.rb', line 9 def initialize(schema, instance, parent: nil, key: nil) reset_with(instance, key, parent, schema) end |
Instance Attribute Details
#annotation ⇒ Object (readonly)
Returns the value of attribute annotation.
7 8 9 |
# File 'lib/json_skooma/result.rb', line 7 def annotation @annotation end |
#error ⇒ Object (readonly)
Returns the value of attribute error.
7 8 9 |
# File 'lib/json_skooma/result.rb', line 7 def error @error end |
#instance ⇒ Object (readonly)
Returns the value of attribute instance.
7 8 9 |
# File 'lib/json_skooma/result.rb', line 7 def instance @instance end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
7 8 9 |
# File 'lib/json_skooma/result.rb', line 7 def key @key end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
7 8 9 |
# File 'lib/json_skooma/result.rb', line 7 def parent @parent end |
#ref_schema=(value) ⇒ Object (writeonly)
Sets the attribute ref_schema
5 6 7 |
# File 'lib/json_skooma/result.rb', line 5 def ref_schema=(value) @ref_schema = value end |
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
7 8 9 |
# File 'lib/json_skooma/result.rb', line 7 def schema @schema end |
Instance Method Details
#absolute_uri ⇒ Object
113 114 115 116 117 118 119 |
# File 'lib/json_skooma/result.rb', line 113 def absolute_uri return @ref_schema.canonical_uri if @ref_schema return nil if schema.canonical_uri.nil? path = JSONPointer.new(schema.canonical_uri.fragment || "") << relative_path schema.canonical_uri.dup.tap { |u| u.fragment = path.to_s } end |
#annotate(value) ⇒ Object
79 80 81 |
# File 'lib/json_skooma/result.rb', line 79 def annotate(value) @annotation = value end |
#call(instance, key, schema = nil) {|child| ... } ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/json_skooma/result.rb', line 61 def call(instance, key, schema = nil) child = dup child.reset_with(instance, key, self, schema) yield child return if child.discard? (children[instance.path] ||= {})[key] = child end |
#children ⇒ Object
13 14 15 |
# File 'lib/json_skooma/result.rb', line 13 def children @children ||= {} end |
#collect_annotations(instance, key: nil, keys: nil) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/json_skooma/result.rb', line 121 def collect_annotations(instance, key: nil, keys: nil) return if !valid? || discard? if @annotation && (key.nil? || key == @key) && (keys.nil? || keys.include?(@key)) && (instance.path == @instance.path) yield self end children[instance.path]&.each_value do |child| child.collect_annotations(instance, key: key, keys: keys) do |node| yield node end end end |
#collect_errors(instance, key: nil, keys: nil) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/json_skooma/result.rb', line 138 def collect_errors(instance, key: nil, keys: nil) return if valid? || discard? if @error && (key.nil? || key == @key) && (keys.nil? || keys.include?(@key)) && (instance.path == @instance.path) yield self end children[instance.path]&.each_value do |child| child.collect_errors(instance, key: key, keys: keys) do |node| yield node end end end |
#discard ⇒ Object
97 98 99 |
# File 'lib/json_skooma/result.rb', line 97 def discard @discard = true end |
#discard? ⇒ Boolean
101 102 103 |
# File 'lib/json_skooma/result.rb', line 101 def discard? @discard end |
#each_children ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/json_skooma/result.rb', line 17 def each_children children.each_value do |child| child.each_value do |grandchild| yield grandchild end end end |
#failure(error = nil) ⇒ Object
83 84 85 86 |
# File 'lib/json_skooma/result.rb', line 83 def failure(error = nil) @valid = false @error = error end |
#output(format, **options) ⇒ Object
155 156 157 |
# File 'lib/json_skooma/result.rb', line 155 def output(format, **) Formatters[format].call(self, **) end |
#passed? ⇒ Boolean
109 110 111 |
# File 'lib/json_skooma/result.rb', line 109 def passed? @valid || @skip_assertion end |
#path ⇒ Object
25 26 27 |
# File 'lib/json_skooma/result.rb', line 25 def path @path ||= @parent.nil? ? JSONPointer.new([]) : parent.path.child(key) end |
#relative_path ⇒ Object
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/json_skooma/result.rb', line 29 def relative_path @relative_path ||= if @parent.nil? JSONPointer.new([]) elsif schema.equal?(parent.schema) parent.relative_path.child(key) else JSONPointer.new_root(key) end end |
#reset_with(instance, key, parent, schema = nil) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/json_skooma/result.rb', line 44 def reset_with(instance, key, parent, schema = nil) @schema = schema if schema @parent = parent @key = key @valid = true @instance = instance @children = nil @annotation = nil @discard = false @skip_assertion = false @error = nil @path = nil @ref_schema = nil @relative_path = nil end |
#root ⇒ Object
40 41 42 |
# File 'lib/json_skooma/result.rb', line 40 def root @root ||= parent&.root || self end |
#schema_node ⇒ Object
71 72 73 |
# File 'lib/json_skooma/result.rb', line 71 def schema_node relative_path.eval(schema) end |
#sibling(instance, key) ⇒ Object
75 76 77 |
# File 'lib/json_skooma/result.rb', line 75 def sibling(instance, key) @parent&.children&.dig(instance.path, key) end |
#skip_assertion ⇒ Object
93 94 95 |
# File 'lib/json_skooma/result.rb', line 93 def skip_assertion @skip_assertion = true end |
#success ⇒ Object
88 89 90 91 |
# File 'lib/json_skooma/result.rb', line 88 def success @valid = true @error = nil end |
#to_s ⇒ Object
159 160 161 |
# File 'lib/json_skooma/result.rb', line 159 def to_s output(:simple) end |
#valid? ⇒ Boolean
105 106 107 |
# File 'lib/json_skooma/result.rb', line 105 def valid? @valid end |