module Curio::Methods

The collection methods

Public Class Methods

new(*) click to toggle source

Setup the map and call parent initializer

@return [undefined]

@api private

Calls superclass method
# File lib/curio.rb, line 148
def initialize(*)
  @map = { }
  super
end

Public Instance Methods

<<(item)
Alias for: add
[](key) click to toggle source

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(item) click to toggle source

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
Also aliased as: <<
all()
Alias for: values
fetch(key, *args, &block) click to toggle source

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() click to toggle source

Freeze map and items

@return [self]

@api public

Calls superclass method
# File lib/curio.rb, line 243
def freeze
  @map.values.each(&:freeze)
  @map.freeze
  super
end
has?(key)
Alias for: key?
key?(key) click to toggle source

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
Also aliased as: has?
map=(hash) click to toggle source

Set map source

@param [Hash] hash

@return [Hash]

@api public

# File lib/curio.rb, line 234
def map=(hash)
  @map = hash
end
to_h() click to toggle source

Returns a hash representation of the collection

@return [Hash]

@api public

# File lib/curio.rb, line 223
def to_h
  @map
end
values() click to toggle source

Get all the items in collection

@return [Array]

@api public

# File lib/curio.rb, line 158
def values
  @map.values
end
Also aliased as: all

Private Instance Methods

call_add_hooks(item) click to toggle source

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