Class: JSONSkooma::JSONNode

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

Direct Known Subclasses

JSONSchema

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, key: nil, parent: nil, item_class: JSONNode, **item_params) ⇒ JSONNode

Returns a new instance of JSONNode.



9
10
11
12
13
14
15
16
17
# File 'lib/json_skooma/json_node.rb', line 9

def initialize(value, key: nil, parent: nil, item_class: JSONNode, **item_params)
  @key = key
  @parent = parent
  @item_class = item_class
  @item_params = item_params
  @type, data = parse_value(value)

  super(data)
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



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

def key
  @key
end

#parentObject (readonly)

Returns the value of attribute parent.



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

def parent
  @parent
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#!=(other) ⇒ Object



59
60
61
62
# File 'lib/json_skooma/json_node.rb', line 59

def !=(other)
  return super(other.__getobj__) if other.is_a?(self.class)
  super
end

#==(other) ⇒ Object



54
55
56
57
# File 'lib/json_skooma/json_node.rb', line 54

def ==(other)
  return super(other.__getobj__) if other.is_a?(self.class)
  super
end

#[](key) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/json_skooma/json_node.rb', line 23

def [](key)
  case type
  when "array"
    super(key.to_i)
  when "object"
    super(key.to_s)
  else
    raise Error, "Cannot get key #{key} from #{__getobj__.class} in #{path}"
  end
end

#pathObject



48
49
50
51
52
# File 'lib/json_skooma/json_node.rb', line 48

def path
  return @path if instance_variable_defined?(:@path)

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

#rootObject



19
20
21
# File 'lib/json_skooma/json_node.rb', line 19

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

#valueObject



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/json_skooma/json_node.rb', line 34

def value
  return @value if instance_variable_defined?(:@value)

  @value =
    case type
    when "array"
      map(&:value)
    when "object"
      transform_values(&:value)
    else
      __getobj__
    end
end