Module: Curio::Methods

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

Overview

The collection methods

Instance Method Summary collapse

Instance Method Details

#[](key) ⇒ object

Get an item from the collection

Parameters:

  • key (undefined)

Returns:

  • (object)


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

Parameters:

  • item (object)

Returns:

  • (self)


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

Parameters:

  • key (undefined)
  • default (undefined)

Yields:

  • (undefined)

Returns:

  • (object)


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

#freezeself

Freeze map and items

Returns:

  • (self)


206
207
208
209
210
# File 'lib/curio.rb', line 206

def freeze
  @map.values.each(&:freeze)
  @map.freeze
  super
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)


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

Parameters:

  • key (undefined)

Returns:

  • (Boolean)


176
177
178
# File 'lib/curio.rb', line 176

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

#map=(hash) ⇒ Hash

Set map source

Parameters:

  • hash (Hash)

Returns:

  • (Hash)


197
198
199
# File 'lib/curio.rb', line 197

def map=(hash)
  @map = hash
end

#to_hHash

Returns a hash representation of the collection

Returns:

  • (Hash)


186
187
188
# File 'lib/curio.rb', line 186

def to_h
  @map
end

#valuesArray Also known as: all

Get all the items in collection

Returns:

  • (Array)


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

def_delegators :values, :each