Module: JSI::PathedHashNode

Includes:
Hashlike
Included in:
JSON::HashNode
Defined in:
lib/jsi/pathed_node.rb

Overview

module extending a PathedNode object when its 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 {|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.

Yields:

  • (Object, Object)

    each key and value of this hash node

Returns:

  • (self, Enumerator)


39
40
41
42
43
44
45
46
47
# File 'lib/jsi/pathed_node.rb', line 39

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

#node_content_hash_pubsend(method_name, *a, &b) ⇒ Object

Returns the result of calling method method_name on the node_content or its #to_hash.

Parameters:

  • method_name (String, Symbol)
  • *a,

    &b are passed to the invocation of method_name

Returns:

  • (Object)

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



60
61
62
63
64
65
66
# File 'lib/jsi/pathed_node.rb', line 60

def node_content_hash_pubsend(method_name, *a, &b)
  if node_content.respond_to?(method_name)
    node_content.public_send(method_name, *a, &b)
  else
    node_content.to_hash.public_send(method_name, *a, &b)
  end
end

#to_hashHash

Returns a hash in which each key is a key of the node_content hash and each value is the result of selfkey.

Returns:

  • (Hash)

    a hash in which each key is a key of the node_content hash and each value is the result of selfkey.



51
52
53
# File 'lib/jsi/pathed_node.rb', line 51

def to_hash
  {}.tap { |h| each_key { |k| h[k] = self[k] } }
end