Module: JSI::Util::Hashlike
- Included in:
- Base::HashNode
- Defined in:
- lib/jsi/util/typelike.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
(also: #to_s)
basically the same #inspect as Hash, but has the class name and, if responsive, self's #jsi_object_group_text.
-
#merge(other) {|key, oldval, newval| ... } ⇒ Object
like Hash#merge.
-
#pretty_print(q) ⇒ void
pretty-prints a representation of this hashlike to the given printer.
-
#update(other) {|key, oldval, newval| ... } ⇒ Object
(also: #merge!)
like Hash#update.
Instance Method Details
#inspect ⇒ String 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
101 102 103 104 |
# File 'lib/jsi/util/typelike.rb', line 101 def inspect object_group_str = (respond_to?(:jsi_object_group_text, true) ? jsi_object_group_text : [self.class]).join(' ') "\#{<#{object_group_str}>#{map { |k, v| " #{k.inspect} => #{v.inspect}" }.join(',')}}" end |
#merge(other) {|key, oldval, newval| ... } ⇒ Object
like Hash#merge
94 95 96 |
# File 'lib/jsi/util/typelike.rb', line 94 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
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/jsi/util/typelike.rb', line 110 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_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!
like Hash#update
72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/jsi/util/typelike.rb', line 72 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 = 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 |