Class: JSONSkooma::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/json_skooma/result.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#annotationObject (readonly)

Returns the value of attribute annotation.



7
8
9
# File 'lib/json_skooma/result.rb', line 7

def annotation
  @annotation
end

#errorObject (readonly)

Returns the value of attribute error.



7
8
9
# File 'lib/json_skooma/result.rb', line 7

def error
  @error
end

#instanceObject (readonly)

Returns the value of attribute instance.



7
8
9
# File 'lib/json_skooma/result.rb', line 7

def instance
  @instance
end

#keyObject (readonly)

Returns the value of attribute key.



7
8
9
# File 'lib/json_skooma/result.rb', line 7

def key
  @key
end

#parentObject (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

Parameters:

  • value

    the value to set the attribute ref_schema to.



5
6
7
# File 'lib/json_skooma/result.rb', line 5

def ref_schema=(value)
  @ref_schema = value
end

#schemaObject (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_uriObject



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

Yields:

  • (child)


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

#childrenObject



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

#discardObject



97
98
99
# File 'lib/json_skooma/result.rb', line 97

def discard
  @discard = true
end

#discard?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/json_skooma/result.rb', line 101

def discard?
  @discard
end

#each_childrenObject



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, **options)
  Formatters[format].call(self, **options)
end

#passed?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/json_skooma/result.rb', line 109

def passed?
  @valid || @skip_assertion
end

#pathObject



25
26
27
# File 'lib/json_skooma/result.rb', line 25

def path
  @path ||= @parent.nil? ? JSONPointer.new([]) : parent.path.child(key)
end

#relative_pathObject



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

#rootObject



40
41
42
# File 'lib/json_skooma/result.rb', line 40

def root
  @root ||= parent&.root || self
end

#schema_nodeObject



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_assertionObject



93
94
95
# File 'lib/json_skooma/result.rb', line 93

def skip_assertion
  @skip_assertion = true
end

#successObject



88
89
90
91
# File 'lib/json_skooma/result.rb', line 88

def success
  @valid = true
  @error = nil
end

#to_sObject



159
160
161
# File 'lib/json_skooma/result.rb', line 159

def to_s
  output(:simple)
end

#valid?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/json_skooma/result.rb', line 105

def valid?
  @valid
end