Module: JSI::Base::HashNode

Includes:
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

Methods included from Util::Hashlike

#inspect, #merge, #pretty_print, #to_s, #update

Instance Method Details

#[](token, as_jsi: jsi_child_as_jsi_default, use_default: jsi_child_use_default_default) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/jsi/base/node.rb', line 55

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



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/jsi/base/node.rb', line 94

def as_json(options = {})
  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_node(k).as_json(**options)
  end
  hash
end

#each(**kw) {|Object, Object| ... } ⇒ self, Enumerator

yields each Hash key (JSON object property name) 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#[].

Parameters:

Yields:

  • (Object, Object)

    each key and value of this hash node

Returns:

  • (self, Enumerator)

    an Enumerator if invoked without a block; otherwise self



74
75
76
77
78
79
80
81
82
# File 'lib/jsi/base/node.rb', line 74

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

Returns:

  • (Boolean)


41
42
43
# File 'lib/jsi/base/node.rb', line 41

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



34
35
36
37
38
# File 'lib/jsi/base/node.rb', line 34

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.

Yields:

Returns:

  • (nil, Enumerator)

    an Enumerator if invoked without a block; otherwise nil



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/jsi/base/node.rb', line 11

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.

Returns:

  • (Boolean)


29
30
31
# File 'lib/jsi/base/node.rb', line 29

def jsi_hash?
  true
end

#jsi_node_content_child(token) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/jsi/base/node.rb', line 46

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

Parameters:

  • method_name (String, Symbol)
  • a

    positional arguments are passed to the invocation of method_name

  • kw

    keyword arguments are passed to the invocation of method_name

  • b

    block is passed to the invocation of method_name

Returns:

  • (Object)

    the result of calling method method_name on the jsi_node_content or its #to_hash



114
115
116
117
118
119
120
# File 'lib/jsi/base/node.rb', line 114

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#[]

Parameters:

Returns:

  • (Hash)


87
88
89
90
91
# File 'lib/jsi/base/node.rb', line 87

def to_hash(**kw)
  hash = {}
  jsi_node_content_hash_pubsend(:each_key) { |k| hash[k] = self[k, **kw] }
  hash.freeze
end