Module: JSI::Util::Hashlike
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).map(&:freeze).freeze
- 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).map(&:freeze).freeze
- DESTRUCTIVE_METHODS =
%w(clear delete delete_if keep_if reject! replace select! shift).map(&:freeze).freeze
- 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 #jsi_object_group_text.
-
#merge(other) {|key, oldval, newval| ... } ⇒ Object
like Hash#merge.
-
#pretty_print(q)
pretty-prints a representation of this hashlike to the given printer.
- #to_s ⇒ Object
-
#update(other) {|key, oldval, newval| ... } ⇒ Object
(also: #merge!)
like Hash#update.
Instance Method Details
#inspect ⇒ String
basically the same #inspect as Hash, but has the class name and, if responsive, self's #jsi_object_group_text
99 100 101 102 |
# File 'lib/jsi/util/typelike.rb', line 99 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
90 91 92 93 94 |
# File 'lib/jsi/util/typelike.rb', line 90 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 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 |
# 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(2) { q.breakable ' ' if !empty? q.seplist(self, nil, :each_pair) { |k, v| q.group { q.pp k q.text ' => ' q.pp v } } } q.breakable '' if !empty? q.text '}' end |
#to_s ⇒ Object
104 105 106 |
# File 'lib/jsi/util/typelike.rb', line 104 def to_s inspect end |
#update(other) {|key, oldval, newval| ... } ⇒ Object Also known as: merge!
like Hash#update
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/jsi/util/typelike.rb', line 69 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 |