Module: ArrayKeys::KeyAccess
- Defined in:
- lib/sequel/array_keys.rb
Overview
The KeyAccess provides a large part of the Hash API for arrays with keys.
Instance Method Summary collapse
-
#[](idx, *args) ⇒ Object
Returns a value referenced by an array index or a key.
-
#[]=(idx, *args) ⇒ Object
Sets the value referenced by an array index or a key.
-
#clone ⇒ Object
Creates a copy of self with a copy of the key set.
-
#delete(key, *args) ⇒ Object
Deletes a value by its key.
-
#delete_at(idx) ⇒ Object
Deletes a value by its index.
-
#dup ⇒ Object
Creates a copy of self with the same key set.
-
#each_key(&block) ⇒ Object
Iterates over the array’s associated keys.
-
#each_pair ⇒ Object
Iterates over each key-value pair in the array.
-
#each_value(&block) ⇒ Object
Iterates over the array’s values.
-
#fetch(k, *args, &block) ⇒ Object
Fetches a value by its key and optionally passes it through the given block:.
-
#has_key?(k) ⇒ Boolean
(also: #member?, #key?)
Returns true if the array’s key set contains the given key.
-
#has_value?(k) ⇒ Boolean
(also: #value?)
Returns true if the array contains the given value.
-
#include?(k) ⇒ Boolean
Returns true if the array’s key set contains the given key.
-
#merge(values, &block) ⇒ Object
Returns an array merged from self and the given array.
-
#merge!(values, &block) ⇒ Object
(also: #update, #update!)
Merges the given array with self, optionally passing the values from self through the given block:.
-
#slice(*args) ⇒ Object
Slices the array, and returns an array with its keys sliced accordingly.
-
#store(k, v) ⇒ Object
Stores a value by index or key.
-
#to_hash ⇒ Object
(also: #to_h)
Converts the array into a hash.
-
#values ⇒ Object
Returns self.
Instance Method Details
#[](idx, *args) ⇒ Object
Returns a value referenced by an array index or a key.
36 37 38 39 40 41 42 |
# File 'lib/sequel/array_keys.rb', line 36 def [](idx, *args) if String === idx or Symbol === idx (idx = @keys.key_pos(idx)) ? super(idx, *args) : nil else super end end |
#[]=(idx, *args) ⇒ Object
Sets the value referenced by an array index or a key.
45 46 47 48 49 50 |
# File 'lib/sequel/array_keys.rb', line 45 def []=(idx,*args) if String === idx or Symbol === idx idx = @keys.key_pos(idx) || @keys.add_key(idx.to_sym) end super(idx, *args) end |
#clone ⇒ Object
Creates a copy of self with a copy of the key set.
146 147 148 149 150 |
# File 'lib/sequel/array_keys.rb', line 146 def clone copy = super copy.keys = @keys.clone copy end |
#delete(key, *args) ⇒ Object
Deletes a value by its key.
86 87 88 89 90 |
# File 'lib/sequel/array_keys.rb', line 86 def delete(key, *args) if (idx = @keys.key_pos(key)) delete_at(idx) end end |
#delete_at(idx) ⇒ Object
Deletes a value by its index.
93 94 95 96 97 |
# File 'lib/sequel/array_keys.rb', line 93 def delete_at(idx) super(idx) @keys = @keys.clone @keys.del_key(idx) end |
#dup ⇒ Object
Creates a copy of self with the same key set.
139 140 141 142 143 |
# File 'lib/sequel/array_keys.rb', line 139 def dup copy = super copy.keys = @keys copy end |
#each_key(&block) ⇒ Object
Iterates over the array’s associated keys.
76 77 78 |
# File 'lib/sequel/array_keys.rb', line 76 def each_key(&block) @keys.each(&block) end |
#each_pair ⇒ Object
Iterates over each key-value pair in the array.
71 72 73 |
# File 'lib/sequel/array_keys.rb', line 71 def each_pair each_with_index {|v, i| yield @keys[i], v} end |
#each_value(&block) ⇒ Object
Iterates over the array’s values.
81 82 83 |
# File 'lib/sequel/array_keys.rb', line 81 def each_value(&block) each(&block) end |
#fetch(k, *args, &block) ⇒ Object
Fetches a value by its key and optionally passes it through the given block:
row.fetch(:name) {|v| v.to_sym}
You can also give a default value
row.fetch(:name, 'untitled')
124 125 126 127 128 129 130 131 |
# File 'lib/sequel/array_keys.rb', line 124 def fetch(k, *args, &block) if idx = @keys.key_pos(k) v = at idx else !args.empty? ? (v = args.first) : (raise IndexError, "key not found") end block ? block[v] : v end |
#has_key?(k) ⇒ Boolean Also known as: member?, key?
Returns true if the array’s key set contains the given key.
105 106 107 |
# File 'lib/sequel/array_keys.rb', line 105 def has_key?(k) @keys.include?(k) || @keys.include?(k.to_sym) || @keys.include?(k.to_s) end |
#has_value?(k) ⇒ Boolean Also known as: value?
Returns true if the array contains the given value.
112 |
# File 'lib/sequel/array_keys.rb', line 112 def has_value?(k); orig_include?(k); end |
#include?(k) ⇒ Boolean
Returns true if the array’s key set contains the given key.
100 101 102 |
# File 'lib/sequel/array_keys.rb', line 100 def include?(k) @keys.include?(k) || @keys.include?(k.to_sym) || @keys.include?(k.to_s) end |
#merge(values, &block) ⇒ Object
Returns an array merged from self and the given array.
153 154 155 |
# File 'lib/sequel/array_keys.rb', line 153 def merge(values, &block) clone.merge!(values, &block) end |
#merge!(values, &block) ⇒ Object Also known as: update, update!
Merges the given array with self, optionally passing the values from self through the given block:
row.merge!(new_values) {|k, old, new| (k == :name) ? old : new}
162 163 164 165 166 167 |
# File 'lib/sequel/array_keys.rb', line 162 def merge!(values, &block) values.each_pair do |k, v| self[k] = (has_key?(k) && block) ? block[k, self[k], v] : v end self end |
#slice(*args) ⇒ Object
Slices the array, and returns an array with its keys sliced accordingly.
56 57 58 59 60 |
# File 'lib/sequel/array_keys.rb', line 56 def slice(*args) s = super(*args) s.keys = @keys.slice(*args) s end |
#store(k, v) ⇒ Object
Stores a value by index or key.
53 |
# File 'lib/sequel/array_keys.rb', line 53 def store(k, v); self[k] = v; end |
#to_hash ⇒ Object Also known as: to_h
Converts the array into a hash.
63 64 65 66 67 |
# File 'lib/sequel/array_keys.rb', line 63 def to_hash h = {} each_with_index {|v, i| h[@keys[i].to_sym] = v} h end |
#values ⇒ Object
Returns self.
134 135 136 |
# File 'lib/sequel/array_keys.rb', line 134 def values self end |