Class: Multilang::Slot

Inherits:
Object
  • Object
show all
Defined in:
lib/multilang/slot.rb

Defined Under Namespace

Modules: Access

Instance Method Summary collapse

Constructor Details

#initializeSlot

Returns a new instance of Slot.



5
6
7
# File 'lib/multilang/slot.rb', line 5

def initialize
  @items = {}
end

Instance Method Details

#[](language_spec) ⇒ Object

Get the object by the language specifier. And call once the block that was given at registration the object.

Parameters:

  • language_spec (String, Symbol)

    the language specifier

Returns:

  • (Object)

    registered object in the slot

Raises:

  • (ArgumentError)

See Also:



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/multilang/slot.rb', line 37

def [](language_spec)
  language_name = LanguageNames[language_spec]
  raise ArgumentError, "#{language_spec.inspect} does not register" unless @items.key?(language_name)
  item = @items[language_name]

  if item[:first]
    item[:first].call
    item.delete(:first)
  end

  item[:object]
end

#exists?(language_spec) ⇒ Boolean

Determine if registered with the language specifier in the slot.

Parameters:

  • language_spec (String, Symbol)

    the language specifier

Returns:

  • (Boolean)

    whether registered with the language specifier in the slot



26
27
28
# File 'lib/multilang/slot.rb', line 26

def exists?(language_spec)
  !!@items[LanguageNames[language_spec]]
end

#register(language_spec, object) { ... } ⇒ Object

Register the object to the slot.

Parameters:

  • language_spec (String, Symbol)

    the language specifier

  • object (Object)

    object to register

Yields:

  • called once at getting the object first

See Also:



16
17
18
19
20
# File 'lib/multilang/slot.rb', line 16

def register(language_spec, object, &first)
  item = {:object => object}
  item[:first] = first if first
  @items[LanguageNames[language_spec]] = item
end