Module: Curio::Methods

Extended by:
Forwardable
Defined in:
lib/curio.rb

Overview

The collection methods

Instance Method Summary collapse

Instance Method Details

#<<(item) ⇒ self

Add an item to the collection

Parameters:

  • item (object)

Returns:

  • (self)


131
132
133
134
135
# File 'lib/curio.rb', line 131

def <<(item)
  key = coerce_key item.send(key_method)
  @map[key] = item
  self
end

#[](key) ⇒ object

Get an item from the collection

Parameters:

  • key (undefined)

Returns:

  • (object)


161
162
163
# File 'lib/curio.rb', line 161

def [](key)
  fetch key, nil
end

#fetch(key, *args) {|undefined| ... } ⇒ object

Fetch an item from the collection

Parameters:

  • key (undefined)
  • default (undefined)

Yields:

  • (undefined)

Returns:

  • (object)


147
148
149
150
151
152
# File 'lib/curio.rb', line 147

def fetch(key, *args, &block)
  args.unshift coerce_key(key)
  @map.fetch(*args, &block)
rescue KeyError
  raise NotFoundError, key
end

#initializeundefined

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

Returns:

  • (undefined)


109
110
111
112
# File 'lib/curio.rb', line 109

def initialize(*)
  @map = { }
  super
end

#key?(key) ⇒ Boolean Also known as: has?

Check if collection has an item with specified key

Parameters:

  • key (undefined)

Returns:

  • (Boolean)


172
173
174
# File 'lib/curio.rb', line 172

def key?(key)
  @map.key? coerce_key(key)
end

#to_hHash

Returns a hash representation of the collection

Returns:

  • (Hash)


182
183
184
# File 'lib/curio.rb', line 182

def to_h
  @map
end

#valuesArray Also known as: all

Get all the items in collection

Returns:

  • (Array)


95
# File 'lib/curio.rb', line 95

def_delegators :values, :each