module Curio::Methods
The collection methods
Public Class Methods
Setup the map and call parent initializer
@return [undefined]
@api private
# File lib/curio.rb, line 148 def initialize(*) @map = { } super end
Public Instance Methods
Get an item from the collection
@param [undefined] key
@return [object]
@api public
# File lib/curio.rb, line 202 def [](key) fetch key, nil end
Add an item to the collection
@param [object] item
@return [self]
@api public
# File lib/curio.rb, line 170 def add(item) call_add_hooks item key = coerce_key item.send(key_method) @map[key.freeze] = item self end
Fetch an item from the collection
@yield [undefined]
@param [undefined] key @param [undefined] default
@return [object]
@api public
# File lib/curio.rb, line 188 def fetch(key, *args, &block) args.unshift coerce_key(key) @map.fetch(*args, &block) rescue KeyError raise NotFoundError, key end
Freeze map and items
@return [self]
@api public
# File lib/curio.rb, line 243 def freeze @map.values.each(&:freeze) @map.freeze super end
Check if collection has an item with specified key
@param [undefined] key
@return [Boolean]
@api public
# File lib/curio.rb, line 213 def key?(key) @map.key? coerce_key(key) end
Set map source
@param [Hash] hash
@return [Hash]
@api public
# File lib/curio.rb, line 234 def map=(hash) @map = hash end
Returns a hash representation of the collection
@return [Hash]
@api public
# File lib/curio.rb, line 223 def to_h @map end
Get all the items in collection
@return [Array]
@api public
# File lib/curio.rb, line 158 def values @map.values end
Private Instance Methods
Run all attached hooks
Currently there is only the hook for auto increment if the collection was configured to use this.
@api private
# File lib/curio.rb, line 257 def call_add_hooks(item) hooks.each { |hook| hook.call(self, item) } end