Class: OrbipayPaymentsapiClient::Multimap

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/orbipay_paymentsapi_client/multimap.rb

Overview

Multimap is a generalization of a map or associative array abstract data type in which more than one value may be associated with and returned for a given key.

Example

map = Multimap.new
map["a"] = 100
map["b"] = 200
map["a"] = 300
map["a"]                              # -> [100, 300]
map["b"]                              # -> [200]

Instance Method Summary collapse

Constructor Details

#initialize(default = []) ⇒ Multimap

call-seq:

Multimap.new           => multimap
Multimap.new(default)  => multimap

Returns a new, empty multimap.



26
27
28
# File 'lib/orbipay_paymentsapi_client/multimap.rb', line 26

def initialize(default = [])
  @hash = Hash.new(default)
end

Instance Method Details

#[](key) ⇒ Object

Retrieves the value object corresponding to the *keys object.



42
43
44
# File 'lib/orbipay_paymentsapi_client/multimap.rb', line 42

def [](key)
  @hash[key]
end

#initialize_copy(original) ⇒ Object

:nodoc:



30
31
32
33
34
35
# File 'lib/orbipay_paymentsapi_client/multimap.rb', line 30

def initialize_copy(original) #:nodoc:
  @hash = Hash.new(original.default.dup)
  original._internal_hash.each_pair do |key, container|
    @hash[key] = container.dup
  end
end

#store(key, value) ⇒ Object Also known as: []=

call-seq:

map[key] = value        => value
map.store(key, value)   => value

Associates the value given by value with the key given by key. Unlike a regular hash, multiple can be assoicated with the same value.

map = Multimap["a" => 100, "b" => 200]
map["a"] = 9
map["c"] = 4
map   #=> {"a" => [100, 9], "b" => [200], "c" => [4]}


58
59
60
61
62
63
# File 'lib/orbipay_paymentsapi_client/multimap.rb', line 58

def store(key, value)
  update_container(key) do |container|
    container << value
    container
  end
end