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

Methods included from Util::Hashlike

#inspect, #merge, #pretty_print, #update

Methods included from Enumerable

#as_json, #to_a

Instance Method Details

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

returns an Enumerator if no block is given.

Parameters:

Yields:

  • (Object, Object)

    each key and value of this hash node

Returns:

  • (self, Enumerator)

    an Enumerator if invoked without a block; otherwise self



48
49
50
51
52
53
54
55
56
# File 'lib/jsi/base/node.rb', line 48

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_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



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

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)


61
62
63
# File 'lib/jsi/base/node.rb', line 61

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