Module: JSI::Base::HashNode
- Includes:
- Enumerable, Util::Hashlike
- Defined in:
- lib/jsi/base/node.rb
Overview
module extending a JSI::Base object when its instance (its #jsi_node_content)
is a Hash (or responds to #to_hash
)
Constant Summary
Constants included from Util::Hashlike
Util::Hashlike::DESTRUCTIVE_METHODS, Util::Hashlike::SAFE_KEY_ONLY_METHODS, Util::Hashlike::SAFE_KEY_VALUE_METHODS, Util::Hashlike::SAFE_METHODS
Instance Method Summary collapse
-
#[](token, as_jsi: jsi_child_as_jsi_default, use_default: jsi_child_use_default_default) ⇒ Object
See #[].
-
#as_json(options = {}) ⇒ Object
See #as_json.
-
#each(**kw) {|Object, Object| ... } ⇒ self, Enumerator
yields each hash key and value of this node.
- #jsi_child_token_in_range?(token) ⇒ Boolean
-
#jsi_each_child_token(&block) ⇒ Object
Yields each key - see #jsi_each_child_token.
-
#jsi_each_propertyName {|JSI::Base| ... } ⇒ nil, Enumerator
instantiates and yields each property name (hash key) as a JSI described by any
propertyNames
schemas. -
#jsi_hash? ⇒ Boolean
See #jsi_hash?.
- #jsi_node_content_child(token) ⇒ Object
-
#jsi_node_content_hash_pubsend(method_name, *a, **kw, &b) ⇒ Object
invokes the method with the given name on the jsi_node_content (if defined) or its #to_hash.
-
#to_hash(**kw) ⇒ Hash
a hash in which each key is a key of the instance hash and each value is the result of #[].
Methods included from Util::Hashlike
#inspect, #merge, #pretty_print, #update
Methods included from Enumerable
Instance Method Details
#[](token, as_jsi: jsi_child_as_jsi_default, use_default: jsi_child_use_default_default) ⇒ Object
See JSI::Base#[]
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/jsi/base/node.rb', line 78 def [](token, as_jsi: jsi_child_as_jsi_default, use_default: jsi_child_use_default_default) if jsi_node_content_hash_pubsend(:key?, token) jsi_child(token, as_jsi: as_jsi) else if use_default jsi_default_child(token, as_jsi: as_jsi) else nil end end end |
#as_json(options = {}) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/jsi/base/node.rb', line 117 def as_json( = {}) hash = {} each_key do |k| ks = k.is_a?(String) ? k : k.is_a?(Symbol) ? k.to_s : k.respond_to?(:to_str) && (kstr = k.to_str).is_a?(String) ? kstr : raise(TypeError, "JSON object (Hash) cannot be keyed with: #{k.pretty_inspect.chomp}") hash[ks] = jsi_child(k, as_jsi: true).as_json(**) end hash end |
#each(**kw) {|Object, Object| ... } ⇒ self, Enumerator
yields each hash key and value of this node.
each yielded key is a key of the instance hash, and each yielded value is the result of JSI::Base#[].
97 98 99 100 101 102 103 104 105 |
# File 'lib/jsi/base/node.rb', line 97 def each(**kw, &block) return to_enum(__method__, **kw) { jsi_node_content_hash_pubsend(:size) } unless block if block.arity > 1 jsi_node_content_hash_pubsend(:each_key) { |k| yield k, self[k, **kw] } else jsi_node_content_hash_pubsend(:each_key) { |k| yield [k, self[k, **kw]] } end self end |
#jsi_child_token_in_range?(token) ⇒ Boolean
64 65 66 |
# File 'lib/jsi/base/node.rb', line 64 def jsi_child_token_in_range?(token) jsi_node_content_hash_pubsend(:key?, token) end |
#jsi_each_child_token(&block) ⇒ Object
Yields each key - see JSI::Base#jsi_each_child_token
57 58 59 60 61 |
# File 'lib/jsi/base/node.rb', line 57 def jsi_each_child_token(&block) return to_enum(__method__) { jsi_node_content_hash_pubsend(:size) } unless block jsi_node_content_hash_pubsend(:each_key, &block) nil end |
#jsi_each_propertyName {|JSI::Base| ... } ⇒ nil, Enumerator
instantiates and yields each property name (hash key) as a JSI described by any propertyNames
schemas.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/jsi/base/node.rb', line 34 def jsi_each_propertyName return to_enum(__method__) { jsi_node_content_hash_pubsend(:size) } unless block_given? property_schemas = SchemaSet.build do |schemas| jsi_schemas.each do |s| if s.keyword?('propertyNames') && s['propertyNames'].is_a?(Schema) schemas << s['propertyNames'] end end end jsi_node_content_hash_pubsend(:each_key) do |key| yield property_schemas.new_jsi(key) end nil end |
#jsi_hash? ⇒ Boolean
See JSI::Base#jsi_hash?. Always true for HashNode.
52 53 54 |
# File 'lib/jsi/base/node.rb', line 52 def jsi_hash? true end |
#jsi_node_content_child(token) ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/jsi/base/node.rb', line 69 def jsi_node_content_child(token) # I could check token_in_range? and return nil here (as ArrayNode does). # without that check, if the instance defines Hash#default or #default_proc, that result is returned. # the preferred mechanism for a JSI's default value should be its schema. # but there's no compelling reason not to support both, so I'll return what #[] returns. jsi_node_content_hash_pubsend(:[], token) end |
#jsi_node_content_hash_pubsend(method_name, *a, **kw, &b) ⇒ Object
invokes the method with the given name on the jsi_node_content (if defined) or its #to_hash
137 138 139 140 141 142 143 |
# File 'lib/jsi/base/node.rb', line 137 def jsi_node_content_hash_pubsend(method_name, *a, &b) if jsi_node_content.respond_to?(method_name) jsi_node_content.public_send(method_name, *a, &b) else jsi_node_content.to_hash.public_send(method_name, *a, &b) end end |
#to_hash(**kw) ⇒ Hash
a hash in which each key is a key of the instance hash and each value is the result of JSI::Base#[]
110 111 112 113 114 |
# File 'lib/jsi/base/node.rb', line 110 def to_hash(**kw) hash = {} jsi_node_content_hash_pubsend(:each_key) { |k| hash[k] = self[k, **kw] } hash.freeze end |