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
-
#inspect ⇒ String
Basically the same #inspect as Hash, but has the class name and, if responsive, self's #object_group_text.
-
#merge(other) {|key, oldval, newval| ... } ⇒ Object
the same as Hash#merge.
-
#pretty_print(q) ⇒ void
pretty-prints a representation this node to the given printer.
-
#to_s ⇒ String
See #inspect.
-
#update(other) {|key, oldval, newval| ... } ⇒ Object
(also: #merge!)
the same as Hash#update.
Instance Method Details
#inspect ⇒ String
Returns basically the same #inspect as Hash, but has the class name and, if responsive, self's #object_group_text.
138 139 140 141 |
# File 'lib/jsi/typelike_modules.rb', line 138 def inspect object_group_str = JSI.object_group_str(respond_to?(:object_group_text) ? self.object_group_text : []) "\#{<#{self.class}#{object_group_str}>#{empty? ? '' : ' '}#{self.map { |k, v| "#{k.inspect} => #{v.inspect}" }.join(', ')}}" end |
#merge(other) {|key, oldval, newval| ... } ⇒ Object
the same as Hash#merge
132 133 134 |
# File 'lib/jsi/typelike_modules.rb', line 132 def merge(other, &block) dup.update(other, &block) end |
#pretty_print(q) ⇒ void
This method returns an undefined value.
pretty-prints a representation this node to the given printer
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/jsi/typelike_modules.rb', line 150 def pretty_print(q) q.instance_exec(self) do |obj| object_group_str = JSI.object_group_str(obj.respond_to?(:object_group_text) ? obj.object_group_text : []) text "\#{<#{obj.class}#{object_group_str}>" group_sub { nest(2) { breakable(obj.any? { true } ? ' ' : '') seplist(obj, nil, :each_pair) { |k, v| group { pp k text ' => ' pp v } } } } breakable '' text '}' end end |
#to_s ⇒ String
Returns see #inspect.
144 145 146 |
# File 'lib/jsi/typelike_modules.rb', line 144 def to_s inspect end |
#update(other) {|key, oldval, newval| ... } ⇒ Object Also known as: merge!
the same as Hash#update
110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/jsi/typelike_modules.rb', line 110 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_given? && self_respondingto_key.key?(key) value = yield(key, self[key], value) end self[key] = value end self end |