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).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
(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)
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
97 98 99 100 |
# File 'lib/jsi/util/typelike.rb', line 97 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
88 89 90 91 92 |
# File 'lib/jsi/util/typelike.rb', line 88 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
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/jsi/util/typelike.rb', line 106 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 ' ' 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 |
#update(other) {|key, oldval, newval| ... } ⇒ Object Also known as: merge!
like Hash#update
67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/jsi/util/typelike.rb', line 67 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 |