Module: JSI::Hashlike

Included in:
PathedHashNode
Defined in:
lib/jsi/typelike_modules.rb

Overview

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 =

methods which do not need to access the value.

%w(each_key empty? has_key? include? key? keys length member? size)
SAFE_KEY_VALUE_METHODS =
%w(< <= > >= any? assoc compact dig each_pair each_value fetch fetch_values has_value? invert key merge rassoc reject select to_h to_proc transform_values value? values values_at)
DESTRUCTIVE_METHODS =
%w(clear delete delete_if keep_if reject! replace select! shift)
SAFE_METHODS =
SAFE_KEY_ONLY_METHODS | SAFE_KEY_VALUE_METHODS

Instance Method Summary collapse

Instance Method Details

#inspectString Also known as: to_s

basically the same #inspect as Hash, but has the class name and, if responsive, self's #jsi_object_group_text

Returns:

  • (String)

140
141
142
143
# File 'lib/jsi/typelike_modules.rb', line 140

def inspect
  object_group_str = (respond_to?(:jsi_object_group_text) ? self.jsi_object_group_text : [self.class]).join(' ')
  "\#{<#{object_group_str}>#{self.map { |k, v| " #{k.inspect} => #{v.inspect}" }.join(',')}}"
end

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

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


133
134
135
# File 'lib/jsi/typelike_modules.rb', line 133

def merge(other, &block)
  dup.update(other, &block)
end

#pretty_print(q) ⇒ void

This method returns an undefined value.

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


149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/jsi/typelike_modules.rb', line 149

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

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

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


111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/jsi/typelike_modules.rb', line 111

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
  self_respondingto_key = self.respond_to?(:key?) ? self : to_hash
  other.to_hash.each_pair do |key, value|
    if block && self_respondingto_key.key?(key)
      value = yield(key, self[key], value)
    end
    self[key] = value
  end
  self
end