Module: JSI::PathedHashNode

Includes:
Hashlike
Defined in:
lib/jsi/pathed_node.rb

Overview

module extending a PathedNode object when its jsi_node_content is Hash-like (responds to #to_hash)

Constant Summary

Constants included from Hashlike

Hashlike::DESTRUCTIVE_METHODS, Hashlike::SAFE_KEY_ONLY_METHODS, Hashlike::SAFE_KEY_VALUE_METHODS, Hashlike::SAFE_METHODS

Instance Method Summary collapse

Methods included from Hashlike

#inspect, #merge, #pretty_print, #update

Instance Method Details

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

yields each hash key and value of this node.

each yielded key is the same as a key of the node content hash, and each yielded value is the result of selfkey.

returns an Enumerator if no block is given.

Parameters:

  • a

    arguments are passed to #[]

Yields:

  • (Object, Object)

    each key and value of this hash node

Returns:

  • (self, Enumerator)

    an Enumerator if invoked without a block; otherwise self


30
31
32
33
34
35
36
37
38
# File 'lib/jsi/pathed_node.rb', line 30

def each(*a, &block)
  return to_enum(__method__) { jsi_node_content_hash_pubsend(:size) } unless block
  if block.arity > 1
    jsi_node_content_hash_pubsend(:each_key) { |k| yield k, self[k, *a] }
  else
    jsi_node_content_hash_pubsend(:each_key) { |k| yield [k, self[k, *a]] }
  end
  self
end

#jsi_node_content_hash_pubsend(method_name, *a, &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

    arguments and block are 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


54
55
56
57
58
59
60
# File 'lib/jsi/pathed_node.rb', line 54

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(*a) ⇒ Hash

a hash in which each key is a key of the jsi_node_content hash and each value is the result of self[key]

Parameters:

  • a

    arguments are passed to #[]

Returns:

  • (Hash)

44
45
46
# File 'lib/jsi/pathed_node.rb', line 44

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