Module: Curio::Methods
- Extended by:
- Forwardable
- Defined in:
- lib/curio.rb
Overview
The collection methods
Instance Method Summary collapse
-
#[](key) ⇒ object
Get an item from the collection.
-
#add(item) ⇒ self
(also: #<<)
Add an item to the collection.
-
#fetch(key, *args) {|undefined| ... } ⇒ object
Fetch an item from the collection.
-
#freeze ⇒ self
Freeze map and items.
-
#initialize ⇒ undefined
private
Setup the map and call parent initializer.
-
#key?(key) ⇒ Boolean
(also: #has?)
Check if collection has an item with specified key.
-
#map=(hash) ⇒ Hash
Set map source.
-
#to_h ⇒ Hash
Returns a hash representation of the collection.
-
#values ⇒ Array
(also: #all)
Get all the items in collection.
Instance Method Details
#[](key) ⇒ object
Get an item from the collection
165 166 167 |
# File 'lib/curio.rb', line 165 def [](key) fetch key, nil end |
#add(item) ⇒ self Also known as: <<
Add an item to the collection
134 135 136 137 138 |
# File 'lib/curio.rb', line 134 def add(item) key = coerce_key item.send(key_method) @map[key.freeze] = item self end |
#fetch(key, *args) {|undefined| ... } ⇒ object
Fetch an item from the collection
151 152 153 154 155 156 |
# File 'lib/curio.rb', line 151 def fetch(key, *args, &block) args.unshift coerce_key(key) @map.fetch(*args, &block) rescue KeyError raise NotFoundError, key end |
#freeze ⇒ self
Freeze map and items
206 207 208 209 210 |
# File 'lib/curio.rb', line 206 def freeze @map.values.each(&:freeze) @map.freeze super end |
#initialize ⇒ undefined
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Setup the map and call parent initializer
112 113 114 115 |
# File 'lib/curio.rb', line 112 def initialize(*) @map = { } super end |
#key?(key) ⇒ Boolean Also known as: has?
Check if collection has an item with specified key
176 177 178 |
# File 'lib/curio.rb', line 176 def key?(key) @map.key? coerce_key(key) end |
#map=(hash) ⇒ Hash
Set map source
197 198 199 |
# File 'lib/curio.rb', line 197 def map=(hash) @map = hash end |
#to_h ⇒ Hash
Returns a hash representation of the collection
186 187 188 |
# File 'lib/curio.rb', line 186 def to_h @map end |
#values ⇒ Array Also known as: all
Get all the items in collection
98 |
# File 'lib/curio.rb', line 98 def_delegators :values, :each |