Class: NOSJ::Lazy

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Enumerable[untyped]
Defined in:
lib/nosj/lazy.rb,
sig/nosj.rbs

Overview

A lazy view of one JSON container: children resolve on first access (containers as further Lazy nodes, scalars as values, misses as nil) and are cached on the node.

Instance Method Summary collapse

Instance Method Details

#[](token) ⇒ NOSJ::Lazy, ...

Resolves one child. String and Symbol keys index objects; Integer indices index arrays (negative indices resolve to nil, as in NOSJ.dig). Containers come back as further Lazy nodes, scalars as plain Ruby values, misses as nil. Results are cached on the node.

Parameters:

  • token (String, Symbol, Integer)

Returns:



36
37
38
39
# File 'lib/nosj/lazy.rb', line 36

def [](token)
  cache = (@children ||= {})
  cache.fetch(token) { cache[token] = __get(token) }
end

#array?Boolean

Returns whether this node is a JSON array.

Returns:

  • (Boolean)

    whether this node is a JSON array



93
94
95
# File 'lib/nosj/lazy.rb', line 93

def array?
  __kind == :array
end

#at_pointer(pointer) ⇒ NOSJ::Lazy, ...

Resolves an RFC 6901 JSON Pointer within this node's subtree, with the same lazy/materialize behavior as #[]. Not cached.

Parameters:

  • pointer (String)

    e.g. "/users/3/name"

Returns:



57
58
59
# File 'lib/nosj/lazy.rb', line 57

def at_pointer(pointer)
  __at_pointer(pointer)
end

#dig(first, *rest) ⇒ NOSJ::Lazy, ...

Hash#dig-shaped access, fused into a single resolution: the whole path resolves in one walk of this node's bytes, no intermediate nodes. Semantics match NOSJ.dig: misses, negative indices, and steps into scalars resolve to nil. Not cached.

Parameters:

  • path (Array<String, Symbol, Integer>)
  • first (path_element)
  • rest (path_element)

Returns:



48
49
50
# File 'lib/nosj/lazy.rb', line 48

def dig(first, *rest)
  __dig([first, *rest])
end

#eachself #eachEnumerator[untyped, self]

Iterates direct children, resolved in one walk: object nodes yield [key, child] pairs (like Hash), array nodes yield children. Containers arrive as Lazy nodes, scalars as values.

Overloads:

  • #eachself

    Returns:

    • (self)
  • #eachEnumerator[untyped, self]

    Returns:

    • (Enumerator[untyped, self])

Yields:

Yield Parameters:

  • arg0 (Object)

Yield Returns:

  • (void)

Returns:

  • (Enumerator)

    when no block is given



126
127
128
129
130
131
132
133
134
135
# File 'lib/nosj/lazy.rb', line 126

def each(&block)
  return enum_for(:each) unless block

  if object?
    __children.each { |pair| yield pair }
  else
    __children.each { |child| yield child }
  end
  self
end

#empty?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/nosj/lazy.rb', line 117

def empty?
  size.zero?
end

#inspectString Also known as: to_s

Returns kind and span size, without touching content.

Returns:

  • (String)

    kind and span size, without touching content



138
139
140
# File 'lib/nosj/lazy.rb', line 138

def inspect
  "#<NOSJ::Lazy #{__kind} (#{__byte_size} bytes)>"
end

#keysArray<String>

The object's keys (always Strings), read in one walk without materializing any values.

Returns:

  • (Array<String>)

Raises:

  • (TypeError)

    on an array node



102
103
104
# File 'lib/nosj/lazy.rb', line 102

def keys
  __keys
end

#object?Boolean

Returns whether this node is a JSON object.

Returns:

  • (Boolean)

    whether this node is a JSON object



88
89
90
# File 'lib/nosj/lazy.rb', line 88

def object?
  __kind == :object
end

#sizeInteger Also known as: length, count

Entry count (object pairs or array elements), read in one walk without materializing anything.

Returns:

  • (Integer)


110
111
112
# File 'lib/nosj/lazy.rb', line 110

def size
  __size
end

#to_aArray

Returns the materialized subtree.

Returns:

  • (Array)

    the materialized subtree

Raises:

  • (TypeError)

    on an object node



81
82
83
84
85
# File 'lib/nosj/lazy.rb', line 81

def to_a
  raise TypeError, "to_a on a JSON object" unless array?

  __materialize
end

#to_hHash

Returns the materialized subtree.

Returns:

  • (Hash)

    the materialized subtree

Raises:

  • (TypeError)

    on an array node



73
74
75
76
77
# File 'lib/nosj/lazy.rb', line 73

def to_h
  raise TypeError, "to_h on a JSON array" unless object?

  __materialize
end

#valueHash, Array Also known as: materialize

Materializes this node's whole subtree as plain Ruby values, under the options given to NOSJ.lazy (+symbolize_names+, freeze, ...).

Returns:

  • (Hash, Array)


66
67
68
# File 'lib/nosj/lazy.rb', line 66

def value
  __materialize
end