Class: Localization

Inherits:
Object
  • Object
show all
Defined in:
lib/localization.rb,
lib/localization/main.rb,
lib/localization/railtie.rb,
lib/localization/version.rb

Defined Under Namespace

Modules: Main, Rails Classes: Railtie

Constant Summary collapse

VERSION =
"3.1.0"
@@sources =
[]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tree = nil, assignments = nil, parent = nil, error = nil) ⇒ Localization

Returns a new instance of Localization.



15
16
17
18
19
20
21
22
23
# File 'lib/localization.rb', line 15

def initialize(tree = nil, assignments = nil, parent = nil, error = nil)
  unless error
    @assignments = assignments || {}
    @parent = parent
    @tree = tree || default_tree
  else
    @error = error
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *arguments, &block) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/localization.rb', line 25

def method_missing(name, *arguments, &block)
  case
    when @tree.respond_to?(:key?) && @tree.key?(name.to_s)
      Localization.new(@tree[name.to_s], arguments.first, self)
    when !@parent.nil?
      @parent.send(name, arguments.first)
    else Localization.new(nil, nil, nil, @error || "Localized text missing for #{name}")
  end
end

Class Method Details

.sourcesObject



11
12
13
# File 'lib/localization.rb', line 11

def self.sources
  @@sources
end

.sources=(items) ⇒ Object



7
8
9
# File 'lib/localization.rb', line 7

def self.sources=(items)
  @@sources = items
end

Instance Method Details

#to_sObject



35
36
37
# File 'lib/localization.rb', line 35

def to_s
  @error || Mustache.render(@tree, @assignments)
end