Class: JSI::JSON::HashNode

Inherits:
Node
  • Object
show all
Includes:
Enumerable, Hashlike
Defined in:
lib/jsi/json/node.rb

Overview

a JSI::JSON::Node whose content is Hash-like (responds to #to_hash) and includes Hash methods from Hashlike

Constant Summary

Constants included from Hashlike

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

Instance Attribute Summary

Attributes inherited from Node

#document, #path, #pointer

Instance Method Summary collapse

Methods included from Hashlike

#inspect, #pretty_print, #to_s

Methods inherited from Node

#[], #[]=, #content, #deref, #document_node, #fingerprint, #fragment, #initialize, #inspect, #modified_copy, new_by_type, new_doc, #object_group_text, #parent_node, #pointer_path, #pretty_print

Methods included from FingerprintHash

#==, #hash

Constructor Details

This class inherits a constructor from JSI::JSON::Node

Instance Method Details

#as_json(*opt) ⇒ Object

returns a jsonifiable representation of this node's content



317
318
319
# File 'lib/jsi/json/node.rb', line 317

def as_json(*opt) # needs redefined after including Enumerable
  Typelike.as_json(content, *opt)
end

#each(&block) ⇒ Object

iterates over each element in the same manner as Array#each



298
299
300
301
302
303
304
305
306
# File 'lib/jsi/json/node.rb', line 298

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

#to_hashObject

the content of this HashNode, as a Hash



309
310
311
# File 'lib/jsi/json/node.rb', line 309

def to_hash
  inject({}) { |h, (k, v)| h[k] = v; h }
end