Class: Jinx::Associative

Inherits:
Object show all
Defined in:
lib/jinx/helpers/associative.rb

Overview

An Associative object implements a #[] method.

Direct Known Subclasses

Writable

Instance Method Summary collapse

Constructor Details

#initialize {|key| ... } ⇒ Associative

Returns a new instance of Associative.

Yields:

  • (key)

    the associated oject

Yield Parameters:

  • key

    the key to find



7
8
9
# File 'lib/jinx/helpers/associative.rb', line 7

def initialize(&accessor)
  @accessor = accessor
end

Instance Method Details

#[](key) ⇒ Object

Returns the associated object.

Parameters:

  • key

    the key to find

Returns:

  • the associated object



13
14
15
# File 'lib/jinx/helpers/associative.rb', line 13

def [](key)
  @accessor.call(key)
end

#writer {|key| ... } ⇒ Associative

Returns a new Associative with a []= writer method.

Yields:

  • (key)

    the associated oject

Yield Parameters:

  • key

    the key to find

Returns:

  • (Associative)

    a new Associative with a []= writer method



20
21
22
# File 'lib/jinx/helpers/associative.rb', line 20

def writer(&writer)
  Writable.new(self, &writer)
end