Class: Tash
- Inherits:
-
Object
- Object
- Tash
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/tash.rb,
lib/tash/version.rb
Overview
A Tash is a hash with transformed keys.
Constant Summary collapse
- VERSION =
'1.0.0'
Class Method Summary collapse
-
.[](*objects, &transform) ⇒ Tash
Returns a new Tash object populated with the given objects, if any.
Instance Method Summary collapse
-
#<(other) ⇒ true or false
Returns
trueif tash is a proper subset of other,falseotherwise. -
#<=(other) ⇒ true or false
Returns
trueif tash is a subset of other,falseotherwise. -
#==(other) ⇒ true or false
Returns
trueif all of the following are true:. -
#>(other) ⇒ true or false
Returns
trueif tash is a proper superset of other,falseotherwise. -
#>=(other) ⇒ true or false
Returns
trueif tash is a superset of other,falseotherwise. -
#[](key) ⇒ value
Returns the value associated with the given
keyafter transformation, if found. -
#[]=(key, value) ⇒ value
(also: #store)
Associates the given
valuewith the givenkeyafter transformation. -
#assoc(key) ⇒ Array<K,V> or nil
If the given transformed
keyis found, returns a 2-element Array containing that key and its value. -
#clear ⇒ self
Removes all tash entries.
-
#compact ⇒ Tash
Returns a copy of
selfwith allnil-valued entries removed. -
#compact! ⇒ self or nil
Returns
selfwith all itsnil-valued entries removed. -
#compare_by_identity ⇒ self
Sets
selfto consider only identity in comparing keys; two keys are considered the same only if they are the same object. -
#compare_by_identity? ⇒ Boolean
Returns
trueif #compare_by_identity has been called,falseotherwise. -
#deconstruct_keys(keys) ⇒ Hash
Provides support for pattern matching using a Tash.
-
#default(*key) ⇒ Object
Returns the default value for the given transformed
key. -
#default=(value) ⇒ Object
Sets the default value to
value. -
#default_proc ⇒ Proc or nil
Returns the default proc for
self. -
#default_proc=(proc) ⇒ Proc
Sets the default proc for
selftoproc. -
#delete(key, &block) ⇒ value or nil, Object
Deletes the entry for the given transformed
keyand returns its associated value. -
#delete_if(&block) ⇒ Enumerator, self
If a block given, calls the block with each key-value pair; deletes each entry for which the block returns a truthy value.
-
#dig(key, *identifiers) ⇒ Object
Finds and returns the object in nested objects that is specified by transformed
keyandidentifiers. -
#each(&block) ⇒ Enumerator, self
(also: #each_pair)
Calls the given block with each key-value pair.
-
#each_key(&block) ⇒ Enumerator, self
Calls the given block with each key.
-
#each_value(&block) ⇒ Enumerator, self
Calls the given block with each value.
-
#empty? ⇒ true or false
Returns
trueif there are no tash entries,falseotherwise. -
#eql?(other) ⇒ true or false
Returns
trueif all of the following are true:. -
#except(*keys) ⇒ Tash
Returns a new Tash excluding entries for the given
keys. -
#fetch(key, *default_value, &block) ⇒ Object
Returns the value for the given
key, if found. -
#fetch_values(*keys, &block) ⇒ Array
Returns a new Array containing the values associated with the given keys *keys.
-
#flatten ⇒ Array
Returns a new Array object that is a 1-dimensional flattening of
self. -
#hash ⇒ Integer
Returns the Integer hash-code for the hash.
-
#initialize(&transform) ⇒ Tash
constructor
Returns a new empty Tash object.
-
#inspect ⇒ String
(also: #to_s)
Returns a new String containing the tash entries.
-
#invert ⇒ Tash
Returns a new Tash object with the each key-value pair inverted.
-
#keep_if(&block) ⇒ Enumerator, self
Calls the block for each key-value pair; retains the entry if the block returns a truthy value; otherwise deletes the entry.
-
#key(value) ⇒ key or nil
Returns the transformed key for the first-found entry with the given
value. -
#key?(key) ⇒ true or false
(also: #has_key?, #include?, #member?)
Returns
trueifkeyafter transformation is a key inself, otherwisefalse. -
#keys ⇒ Array
Returns a new Array containing all keys in
self. -
#merge(*others, &block) ⇒ Tash
Returns the new Tash formed by merging each of
other_tashes_or_hashesinto a copy ofself. -
#merge!(*others, &block) ⇒ self
(also: #update)
Merges each of
other_tashes_or_hashesintoself. -
#rassoc(value) ⇒ Array<K,V> or nil
Returns a new 2-element Array consisting of the key and value of the first-found entry whose value is
==to value. -
#rehash ⇒ self
Rebuilds the hash table by recomputing the hash index for each key.
-
#reject(&block) ⇒ Enumerator, Tash
Returns a new Tash object whose entries are all those from
selffor which the block returnsfalseornil. -
#reject!(&block) ⇒ Enumerator, self or nil
Returns
self, whose remaining entries are those for which the block returnsfalseornil. -
#replace(other) ⇒ self
Replaces the entire contents of
selfwith the contents ofother. -
#select(&block) ⇒ Enumerator, Tash
(also: #filter)
Returns a new Tash object whose entries are those for which the block returns a truthy value.
-
#select!(&block) ⇒ Enumerator, self or nil
(also: #filter!)
Returns
self, whose entries are those for which the block returns a truthy value. -
#shift ⇒ [key, value] or default value
Removes the first tash entry and returns a 2-element Array containing the removed key and value.
-
#size ⇒ Integer
(also: #length)
Returns the count of entries in
self. -
#slice(*keys) ⇒ Tash
Returns a new Tash object containing the entries for the given transformed
keys. -
#to_a ⇒ Array
Returns a new Array of 2-element Array objects; each nested Array contains a key-value pair from
self. -
#to_h(&block) ⇒ Hash
Returns a Hash containing the content of
self. -
#to_hash ⇒ Hash
Returns tash as a Hash.
-
#to_proc ⇒ Proc
Returns a Proc object that maps a key to its value.
-
#transform_proc ⇒ Proc or nil
Returns the transform proc for
self. -
#transform_values(&block) ⇒ Tash
Returns a new Tash object; each entry has:.
-
#transform_values!(&block) ⇒ self
Returns
self, whose keys are unchanged, and whose values are determined by the given block. -
#value? ⇒ Boolean
(also: #has_value?)
Returns
trueifvalueis a value inself, otherwisefalse. -
#values ⇒ Array
Returns a new Array containing all values in
self. -
#values_at(*keys) ⇒ Array
Returns a new Array containing values for the given transformed
keys.
Constructor Details
#initialize(&transform) ⇒ Tash
Returns a new empty Tash object.
77 78 79 80 81 |
# File 'lib/tash.rb', line 77 def initialize(&transform) @transform = transform @ir = {} # internal representation - @ir[transformed key] = value @default_proc = nil end |
Class Method Details
.[](*objects, &transform) ⇒ Tash
Returns a new Tash object populated with the given objects, if any. If a Tash is passed with no block it returns a duplicate with the transform block from the original. If a Tash is passed with a block it is treated as a Hash.
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/tash.rb', line 40 def self.[](*objects, &transform) # rubocop:disable Metrics/PerceivedComplexity if objects.empty? new(&transform) elsif objects.size == 1 && !transform && objects.first.is_a?(self) objects.first.dup elsif objects.size == 1 && objects.first.respond_to?(:to_hash) from_hash(objects.first.to_hash, &transform) elsif objects.size.even? from_array(objects, &transform) else raise ArgumentError, "odd number of arguments for #{name}" end end |
Instance Method Details
#<(other) ⇒ true or false
Returns true if tash is a proper subset of other, false otherwise.
|
|
# File 'lib/tash.rb', line 108
|
#<=(other) ⇒ true or false
Returns true if tash is a subset of other, false otherwise.
|
|
# File 'lib/tash.rb', line 122
|
#==(other) ⇒ true or false
Returns true if all of the following are true:
- object is a Tash object.
- tash and object have the same keys (regardless of order).
- For each key
key,tash[key] == other[key].
Otherwise, returns false.
154 155 156 157 158 |
# File 'lib/tash.rb', line 154 def ==(other) return false unless other.is_a?(self.class) @ir == other.to_hash end |
#>(other) ⇒ true or false
Returns true if tash is a proper superset of other, false otherwise.
|
|
# File 'lib/tash.rb', line 160
|
#>=(other) ⇒ true or false
Returns true if tash is a superset of other, false otherwise.
|
|
# File 'lib/tash.rb', line 174
|
#[](key) ⇒ value
Returns the value associated with the given key after transformation, if
found.
203 204 205 |
# File 'lib/tash.rb', line 203 def [](key) @ir[transform(key)] end |
#[]=(key, value) ⇒ value Also known as: store
Associates the given value with the given key after transformation. If
the given post transformation key exists, replaces its value with the
given value; the ordering is not affected. If post transformation key
does not exist, adds the transformed key and value; the new entry is
last in the order.
224 225 226 |
# File 'lib/tash.rb', line 224 def []=(key, value) @ir[transform(key)] = value end |
#assoc(key) ⇒ Array<K,V> or nil
If the given transformed key is found, returns a 2-element Array
containing that key and its value. Returns nil if the tranformed key
key is not found.
240 241 242 |
# File 'lib/tash.rb', line 240 def assoc(key) @ir.assoc(transform(key)) end |
#clear ⇒ self
Removes all tash entries.
247 248 249 250 |
# File 'lib/tash.rb', line 247 def clear @ir.clear self end |
#compact ⇒ Tash
Returns a copy of self with all nil-valued entries removed.
260 261 262 |
# File 'lib/tash.rb', line 260 def compact new_from_self(@ir.compact) end |
#compact! ⇒ self or nil
Returns self with all its nil-valued entries removed.
271 272 273 |
# File 'lib/tash.rb', line 271 def compact! self if @ir.compact! end |
#compare_by_identity ⇒ self
Sets self to consider only identity in comparing keys; two keys are
considered the same only if they are the same object.
296 297 298 299 |
# File 'lib/tash.rb', line 296 def compare_by_identity @ir.compare_by_identity self end |
#compare_by_identity? ⇒ Boolean
Returns true if #compare_by_identity has been called, false
otherwise.
|
|
# File 'lib/tash.rb', line 301
|
#deconstruct_keys(keys) ⇒ Hash
Provides support for pattern matching using a Tash. Pattern keys will be transformed before matching.
320 321 322 323 324 |
# File 'lib/tash.rb', line 320 def deconstruct_keys(keys) return {} if keys.nil? keys.to_h { |key| [key, self[key]] } end |
#default ⇒ Object #default(key) ⇒ Object
Returns the default value for the given transformed key. The returned
value will be determined either by the default proc or by the default
value.
346 347 348 349 350 351 352 353 354 355 |
# File 'lib/tash.rb', line 346 def default(*key) case key.size when 0 @ir.default when 1 @ir.default(transform(key.first)) else @ir.default(*key) end end |
#default=(value) ⇒ Object
Sets the default value to value.
|
|
# File 'lib/tash.rb', line 357
|
#default_proc ⇒ Proc or nil
Returns the default proc for self.
379 380 381 |
# File 'lib/tash.rb', line 379 def default_proc # rubocop:disable Style/TrivialAccessors (I want it to show as a method in the docs.) @default_proc end |
#default_proc=(proc) ⇒ Proc
Sets the default proc for self to proc.
398 399 400 401 402 |
# File 'lib/tash.rb', line 398 def default_proc=(prok) @default_proc = prok @ir.default_proc = proc { |_, k| prok.call(self, transform(k)) } end |
#delete(key, &block) ⇒ value or nil, Object
Deletes the entry for the given transformed key and returns its
associated value.
427 428 429 |
# File 'lib/tash.rb', line 427 def delete(key, &block) @ir.delete(transform(key), &block) end |
#delete_if(&block) ⇒ Enumerator, self
If a block given, calls the block with each key-value pair; deletes each entry for which the block returns a truthy value.
446 447 448 449 450 451 |
# File 'lib/tash.rb', line 446 def delete_if(&block) return to_enum(:delete_if) unless block @ir.delete_if(&block) self end |
#dig(key, *identifiers) ⇒ Object
Finds and returns the object in nested objects that is specified by
transformed key and identifiers. The nested objects may be instances of
various classes. This method will use the default values for keys that are
not present.
479 480 481 |
# File 'lib/tash.rb', line 479 def dig(key, *identifiers) @ir.dig(transform(key), *identifiers) end |
#each(&block) ⇒ Enumerator, self Also known as: each_pair
Calls the given block with each key-value pair. Returns a new Enumerator if no block is given.
500 501 502 503 504 505 |
# File 'lib/tash.rb', line 500 def each(&block) return to_enum(:each) unless block @ir.each(&block) self end |
#each_key(&block) ⇒ Enumerator, self
Calls the given block with each key.
524 525 526 527 528 529 |
# File 'lib/tash.rb', line 524 def each_key(&block) return to_enum(:each_key) unless block @ir.each_key(&block) self end |
#each_value(&block) ⇒ Enumerator, self
Calls the given block with each value.
547 548 549 550 551 552 |
# File 'lib/tash.rb', line 547 def each_value(&block) return to_enum(:each_value) unless block @ir.each_value(&block) self end |
#empty? ⇒ true or false
Returns true if there are no tash entries, false otherwise.
|
|
# File 'lib/tash.rb', line 554
|
#eql?(other) ⇒ true or false
Returns true if all of the following are true:
- object is a Tash object.
- tash and object have the same keys (regardless of order).
- For each key
key,tash[key] eql? other[key].
Otherwise, returns false.
581 582 583 584 585 |
# File 'lib/tash.rb', line 581 def eql?(other) return false unless other.is_a?(self.class) @ir.eql?(other.to_hash) end |
#except(*keys) ⇒ Tash
Returns a new Tash excluding entries for the given keys. Any given keys
that are not found are ignored. The transform proc is copied to the new
Tash.
598 599 600 |
# File 'lib/tash.rb', line 598 def except(*keys) new_from_self(@ir.except(*keys)) end |
#fetch(key) ⇒ Object #fetch(key, default_value) ⇒ Object
This method does not use the values of either default or
default_proc.
Returns the value for the given key, if found. Raises KeyError if
neither default_value nor a block was given.
628 629 630 631 632 633 634 |
# File 'lib/tash.rb', line 628 def fetch(key, *default_value, &block) if block @ir.fetch(transform(key), &block) else @ir.fetch(transform(key), *default_value) end end |
#fetch_values(*keys, &block) ⇒ Array
Returns a new Array containing the values associated with the given keys *keys. When a block is given, calls the block with each missing transformed key, treating the block's return value as the value for that key.
653 654 655 |
# File 'lib/tash.rb', line 653 def fetch_values(*keys, &block) @ir.fetch_values(*keys.map { |k| transform(k) }, &block) end |
#flatten ⇒ Array #flatten(level) ⇒ Array
Returns a new Array object that is a 1-dimensional flattening of self.
|
|
# File 'lib/tash.rb', line 657
|
#hash ⇒ Integer
Returns the Integer hash-code for the hash. Two Hash objects have the same hash-code if their content is the same (regardless or order).
|
|
# File 'lib/tash.rb', line 688
|
#inspect ⇒ String Also known as: to_s
Returns a new String containing the tash entries.
|
|
# File 'lib/tash.rb', line 700
|
#invert ⇒ Tash
Returns a new Tash object with the each key-value pair inverted. The values will be processed using the key transformation.
718 719 720 721 722 723 |
# File 'lib/tash.rb', line 718 def invert new_ir = @ir.invert new_ir.transform_keys! { |k| transform(k) } new_from_self(new_ir) end |
#keep_if(&block) ⇒ Enumerator, self
Calls the block for each key-value pair; retains the entry if the block returns a truthy value; otherwise deletes the entry.
740 741 742 743 744 745 |
# File 'lib/tash.rb', line 740 def keep_if(&block) return to_enum(:keep_if) unless block @ir.keep_if(&block) self end |
#key(value) ⇒ key or nil
Returns the transformed key for the first-found entry with the given
value. Returns nil if the key is not found.
|
|
# File 'lib/tash.rb', line 747
|
#key?(key) ⇒ true or false Also known as: has_key?, include?, member?
Returns true if key after transformation is a key in self, otherwise
false.
771 772 773 |
# File 'lib/tash.rb', line 771 def key?(key) @ir.key?(transform(key)) end |
#keys ⇒ Array
Returns a new Array containing all keys in self.
|
|
# File 'lib/tash.rb', line 778
|
#merge(*others, &block) ⇒ Tash
Returns the new Tash formed by merging each of other_tashes_or_hashes
into a copy of self.
Each argument in other_tashes_or_hashes must be a Tash or Hash.
With arguments and no block:
- Returns the new Tash object formed by merging each successive item in other_tashes_or_hashes into self.
- Each new-key entry is added at the end.
- Each duplicate-key entry's value overwrites the previous value.
With arguments and a block:
- Returns a new Tash object that is the merge of self and each given tash or hash.
- The given tashes or hashes are merged left to right.
- Each new-key entry is added at the end.
- For each duplicate key:
- Calls the block with the transformed key and the old and new values.
- The block's return value becomes the new value for the entry.
With no arguments:
- Returns a copy of self.
- The block, if given, is ignored.
838 839 840 841 842 843 |
# File 'lib/tash.rb', line 838 def merge(*others, &block) new_ir = others.each_with_object(@ir.dup) do |other, ir| ir.merge!(other.to_hash.transform_keys { |k| transform(k) }, &block) end new_from_self(new_ir) end |
#merge!(*others, &block) ⇒ self Also known as: update
Merges each of other_tashes_or_hashes into self.
Each argument in other_tashes_or_hashes must be a Tash or Hash.
With arguments and no block:
- Returns
self, after the given tashes and hashes are merged into it. - The given tashes and hashes are merged left to right.
- Each new entry is added at the end.
- Each duplicate-key entry's value overwrites the previous value.
With arguments and a block:
- Returns
self, after the given tashes and hashes are merged. - The given tashes and hashes are merged left to right.
- Each new-key entry is added at the end.
- For each duplicate key:
- Calls the block with the transformed key and the old and new values.
- The block's return value becomes the new value for the entry.
With no arguments:
- Returns
self, unmodified. - The block, if given, is ignored.
894 895 896 897 898 899 |
# File 'lib/tash.rb', line 894 def merge!(*others, &block) others.each do |other| @ir.merge!(other.to_hash.transform_keys { |k| transform(k) }, &block) end self end |
#rassoc(value) ⇒ Array<K,V> or nil
Returns a new 2-element Array consisting of the key and value of the
first-found entry whose value is == to value. Returns nil if no such
value found.
|
|
# File 'lib/tash.rb', line 902
|
#rehash ⇒ self
Rebuilds the hash table by recomputing the hash index for each key. The hash table becomes invalid if the hash value of a key has changed after the entry was created.
|
|
# File 'lib/tash.rb', line 915
|
#reject(&block) ⇒ Enumerator, Tash
Returns a new Tash object whose entries are all those from self for which
the block returns false or nil. Returns a new Enumerator if no block
given.
940 941 942 943 944 |
# File 'lib/tash.rb', line 940 def reject(&block) return to_enum(:reject) unless block new_from_self(@ir.reject(&block)) end |
#reject!(&block) ⇒ Enumerator, self or nil
Returns self, whose remaining entries are those for which the block
returns false or nil. Returns nil if no entries are removed.
961 962 963 964 965 |
# File 'lib/tash.rb', line 961 def reject!(&block) return to_enum(:reject!) unless block self if @ir.reject!(&block) end |
#replace(other) ⇒ self
Replaces the entire contents of self with the contents of other. If
other is a tash it also replaces the transform block.
981 982 983 984 985 986 987 988 989 990 |
# File 'lib/tash.rb', line 981 def replace(other) if other.is_a?(self.class) @ir.replace(other.to_hash) @transform = other.transform_proc else @ir.replace(other.to_hash.transform_keys { |k| transform(k) }) end self end |
#select(&block) ⇒ Enumerator, Tash Also known as: filter
Returns a new Tash object whose entries are those for which the block returns a truthy value. Returns a new Enumerator if no block given.
1007 1008 1009 1010 1011 |
# File 'lib/tash.rb', line 1007 def select(&block) return to_enum(:select) unless block new_from_self(@ir.select(&block)) end |
#select!(&block) ⇒ Enumerator, self or nil Also known as: filter!
Returns self, whose entries are those for which the block returns a truthy
value. When given a block, it returns nil if no entries are removed.
1029 1030 1031 1032 1033 |
# File 'lib/tash.rb', line 1029 def select!(&block) return to_enum(:select!) unless block self if @ir.select!(&block) end |
#shift ⇒ [key, value] or default value
Removes the first tash entry and returns a 2-element Array containing the removed key and value. Returns the default value if the hash is empty.
|
|
# File 'lib/tash.rb', line 1036
|
#size ⇒ Integer Also known as: length
Returns the count of entries in self.
|
|
# File 'lib/tash.rb', line 1047
|
#slice(*keys) ⇒ Tash
Returns a new Tash object containing the entries for the given transformed
keys. Any given keys that are not found are ignored.
1065 1066 1067 |
# File 'lib/tash.rb', line 1065 def slice(*keys) new_from_self(@ir.slice(*keys.map { |k| transform(k) })) end |
#to_a ⇒ Array
Returns a new Array of 2-element Array objects; each nested Array
contains a key-value pair from self.
|
|
# File 'lib/tash.rb', line 1069
|
#to_h(&block) ⇒ Hash
Returns a Hash containing the content of self. When a block is given,
returns a Hash object whose content is based on the block; the block should
return a 2-element Array object specifying the key-value pair to be
included in the returned Array.
1092 1093 1094 1095 1096 1097 1098 |
# File 'lib/tash.rb', line 1092 def to_h(&block) if block @ir.to_h(&block) else to_hash end end |
#to_hash ⇒ Hash
Returns tash as a Hash.
1103 1104 1105 |
# File 'lib/tash.rb', line 1103 def to_hash @ir.dup end |
#to_proc ⇒ Proc
Returns a Proc object that maps a key to its value.
1118 1119 1120 1121 1122 1123 |
# File 'lib/tash.rb', line 1118 def to_proc p = @ir.to_proc lambda do |key| p.call(transform(key)) end end |
#transform_proc ⇒ Proc or nil
Returns the transform proc for self.
1135 1136 1137 |
# File 'lib/tash.rb', line 1135 def transform_proc @transform end |
#transform_values(&block) ⇒ Tash
Returns a new Tash object; each entry has:
- A key from
self. - A value provided by the block.
1156 1157 1158 1159 1160 |
# File 'lib/tash.rb', line 1156 def transform_values(&block) return to_enum(:transform_values) unless block new_from_self(@ir.transform_values(&block)) end |
#transform_values!(&block) ⇒ self
Returns self, whose keys are unchanged, and whose values are determined by
the given block.
1176 1177 1178 1179 1180 |
# File 'lib/tash.rb', line 1176 def transform_values!(&block) return to_enum(:transform_values!) unless block self if @ir.transform_values!(&block) end |
#value? ⇒ Boolean Also known as: has_value?
Returns true if value is a value in self, otherwise false.
|
|
# File 'lib/tash.rb', line 1182
|
#values ⇒ Array
Returns a new Array containing all values in self.
|
|
# File 'lib/tash.rb', line 1187
|
#values_at(*keys) ⇒ Array
Returns a new Array containing values for the given transformed keys. The
default values are returned for any keys that are not found.
1207 1208 1209 |
# File 'lib/tash.rb', line 1207 def values_at(*keys) @ir.values_at(*keys.map { |k| transform(k) }) end |