Class: Babelfish::Phrase::PluralForms

Inherits:
Node
  • Object
show all
Defined in:
lib/babelfish/phrase/plural_forms.rb

Overview

Babelfish AST pluralization node.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Node

#initialize

Constructor Details

This class inherits a constructor from Babelfish::Phrase::Node

Class Attribute Details

.compilerObject

Returns the value of attribute compiler.



12
13
14
# File 'lib/babelfish/phrase/plural_forms.rb', line 12

def compiler
  @compiler
end

.sub_dataObject

Returns the value of attribute sub_data.



12
13
14
# File 'lib/babelfish/phrase/plural_forms.rb', line 12

def sub_data
  @sub_data
end

Instance Attribute Details

#compiledObject

Returns the value of attribute compiled.



18
19
20
# File 'lib/babelfish/phrase/plural_forms.rb', line 18

def compiled
  @compiled
end

#formsObject

Returns the value of attribute forms.



18
19
20
# File 'lib/babelfish/phrase/plural_forms.rb', line 18

def forms
  @forms
end

#localeObject

Returns the value of attribute locale.



18
19
20
# File 'lib/babelfish/phrase/plural_forms.rb', line 18

def locale
  @locale
end

#nameObject

Returns the value of attribute name.



18
19
20
# File 'lib/babelfish/phrase/plural_forms.rb', line 18

def name
  @name
end

Instance Method Details

#_to_ruby_method(name, index) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/babelfish/phrase/plural_forms.rb', line 45

def _to_ruby_method( name, index )
    lambda do |params|
        value = params[name].to_f
        rule, strict_forms, regular_forms = PluralForms.sub_data[index]
        r = nil
        if value.nan?
            warn "#{name} parameter is not numeric"
            r = regular_forms[-1]
        else
            r = strict_forms[value] || regular_forms[rule.call(value)] || regular_forms[-1];
        end
        return r  if r.kind_of?(String)
        return ''  if r.nil?
        r.call(params)
    end

end

#to_ruby_methodObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/babelfish/phrase/plural_forms.rb', line 20

def to_ruby_method
    unless compiled
        PluralForms.compiler ||= Babelfish::Phrase::Compiler.new
        forms[:regular].map! do |form|
            PluralForms.compiler.compile( form )
        end
        new_strict = {}
        forms[:strict].each_pair do |key, form|
            new_strict[key] = PluralForms.compiler.compile( form )
        end
        forms[:strict].replace(new_strict)
        self.compiled = true
    end

    rule = Babelfish::Phrase::Pluralizer::find_rule( locale )

    PluralForms.sub_data << [
        rule,
        forms[:strict],
        forms[:regular],
    ]

    return _to_ruby_method( name, PluralForms.sub_data.length - 1 );
end