Module: JSI::Util::Hashlike Private

Includes:
Enumerable
Included in:
Base::HashNode
Defined in:
lib/jsi/util/typelike.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

a module of methods for objects which behave like Hash but are not Hash.

this module is intended to be internal to JSI. no guarantees or API promises are made for non-JSI classes including this module.

Constant Summary collapse

SAFE_KEY_ONLY_METHODS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

methods which do not need to access the value.

%w(each_key empty? has_key? include? key? keys length member? size).map(&:freeze).freeze
SAFE_KEY_VALUE_METHODS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

%w(< <= > >= any? assoc compact dig each_pair each_value fetch fetch_values flatten has_value? invert key merge rassoc reject select filter to_h to_proc transform_values value? values values_at).map(&:freeze).freeze
DESTRUCTIVE_METHODS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

%w(clear delete delete_if filter! flatten! keep_if reject! replace select! shift).map(&:freeze).freeze
SAFE_METHODS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

SAFE_KEY_ONLY_METHODS | SAFE_KEY_VALUE_METHODS

Instance Method Summary collapse

Instance Method Details

#merge(other) {|key, oldval, newval| ... } ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

like Hash#merge

Parameters:

  • other (#to_hash)

    the other hash to merge into this

Yields:

  • (key, oldval, newval)

    for entries with duplicate keys, the value of each duplicate key is determined by calling the block with the key, its value in self and its value in other.

Returns:

  • duplicate of this hash with the other hash merged in

Raises:

  • (TypeError)

    when other does not respond to #to_hash



79
80
81
82
83
# File 'lib/jsi/util/typelike.rb', line 79

def merge(other, &block)
  jsi_modified_copy do |instance|
    instance.merge(other.is_a?(Base) ? other.jsi_node_content : other, &block)
  end
end

#pretty_print(q)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

pretty-prints a representation of this hashlike to the given printer



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/jsi/util/typelike.rb', line 87

def pretty_print(q)
  object_group_str = (respond_to?(:jsi_object_group_text, true) ? jsi_object_group_text : [self.class]).join(' ')
  q.text "\#{<#{object_group_str}>"
  q.group {
    q.nest(2) {
      q.breakable ' ' if !empty?
      q.seplist(self) { |k, v|
          q.pp k
          q.text ' => '
          q.pp v
      }
    }
    q.breakable('') if !empty?
  }
  q.text '}'
end

#update(other) {|key, oldval, newval| ... } ⇒ Object Also known as: merge!

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

like Hash#update

Parameters:

  • other (#to_hash)

    the other hash to update this hash from

Yields:

  • (key, oldval, newval)

    for entries with duplicate keys, the value of each duplicate key is determined by calling the block with the key, its value in self and its value in other.

Returns:

  • self, updated with other

Raises:

  • (TypeError)

    when other does not respond to #to_hash



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/jsi/util/typelike.rb', line 58

def update(other, &block)
  unless other.respond_to?(:to_hash)
    raise(TypeError, "cannot update with argument that does not respond to #to_hash: #{other.pretty_inspect.chomp}")
  end
  other.to_hash.each_pair do |key, value|
    if block && key?(key)
      value = yield(key, self[key], value)
    end
    self[key] = value
  end
  self
end