Class: NOSJ::Lazy
- Inherits:
-
Object
- Object
- NOSJ::Lazy
- 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
-
#[](token) ⇒ NOSJ::Lazy, ...
Resolves one child.
-
#array? ⇒ Boolean
Whether this node is a JSON array.
-
#at_pointer(pointer) ⇒ NOSJ::Lazy, ...
Resolves an RFC 6901 JSON Pointer within this node's subtree, with the same lazy/materialize behavior as #[].
-
#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.
-
#each {|arg0| ... } ⇒ Enumerator
Iterates direct children, resolved in one walk: object nodes yield
[key, child]pairs (like Hash), array nodes yield children. - #empty? ⇒ Boolean
-
#inspect ⇒ String
(also: #to_s)
Kind and span size, without touching content.
-
#keys ⇒ Array<String>
The object's keys (always Strings), read in one walk without materializing any values.
-
#object? ⇒ Boolean
Whether this node is a JSON object.
-
#size ⇒ Integer
(also: #length, #count)
Entry count (object pairs or array elements), read in one walk without materializing anything.
-
#to_a ⇒ Array
The materialized subtree.
-
#to_h ⇒ Hash
The materialized subtree.
-
#value ⇒ Hash, Array
(also: #materialize)
Materializes this node's whole subtree as plain Ruby values, under the options given to lazy (+symbolize_names+,
freeze, ...).
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.
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.
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.
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.
48 49 50 |
# File 'lib/nosj/lazy.rb', line 48 def dig(first, *rest) __dig([first, *rest]) end |
#each ⇒ self #each ⇒ Enumerator[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.
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
117 118 119 |
# File 'lib/nosj/lazy.rb', line 117 def empty? size.zero? end |
#inspect ⇒ String Also known as: to_s
Returns 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 |
#keys ⇒ Array<String>
The object's keys (always Strings), read in one walk without materializing any values.
102 103 104 |
# File 'lib/nosj/lazy.rb', line 102 def keys __keys end |
#object? ⇒ Boolean
Returns whether this node is a JSON object.
88 89 90 |
# File 'lib/nosj/lazy.rb', line 88 def object? __kind == :object end |
#size ⇒ Integer Also known as: length, count
Entry count (object pairs or array elements), read in one walk without materializing anything.
110 111 112 |
# File 'lib/nosj/lazy.rb', line 110 def size __size end |
#to_a ⇒ Array
Returns the materialized subtree.
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_h ⇒ Hash
Returns the materialized subtree.
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 |
#value ⇒ Hash, 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, ...).
66 67 68 |
# File 'lib/nosj/lazy.rb', line 66 def value __materialize end |