Module: JSI::PathedHashNode

Includes:
Hashlike
Included in:
BaseHash, 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, #to_s, #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



34
35
36
37
38
39
40
41
42
# File 'lib/jsi/pathed_node.rb', line 34

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



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

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



46
47
48
# File 'lib/jsi/pathed_node.rb', line 46

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