Class: Babelfish::Phrase::PluralFormsParser
- Inherits:
-
Object
- Object
- Babelfish::Phrase::PluralFormsParser
- Defined in:
- lib/babelfish/phrase/plural_forms_parser.rb
Overview
Every plural form represented as AST.
Class Attribute Summary collapse
-
.phrase_parser ⇒ Object
Returns the value of attribute phrase_parser.
Instance Attribute Summary collapse
-
#phrase ⇒ Object
Returns the value of attribute phrase.
-
#regular_forms ⇒ Object
Returns the value of attribute regular_forms.
-
#strict_forms ⇒ Object
Returns the value of attribute strict_forms.
Instance Method Summary collapse
-
#init(phrase) ⇒ Object
Initializes parser.
-
#initialize(phrase = nil) ⇒ PluralFormsParser
constructor
Instantiates parser.
-
#parse(phrase) ⇒ Object
Parses specified phrase.
- #phrase_parser ⇒ Object
Constructor Details
#initialize(phrase = nil) ⇒ PluralFormsParser
Instantiates parser.
25 26 27 |
# File 'lib/babelfish/phrase/plural_forms_parser.rb', line 25 def initialize( phrase = nil ) init( phrase ) unless phrase.nil? end |
Class Attribute Details
.phrase_parser ⇒ Object
Returns the value of attribute phrase_parser.
16 17 18 |
# File 'lib/babelfish/phrase/plural_forms_parser.rb', line 16 def phrase_parser @phrase_parser end |
Instance Attribute Details
#phrase ⇒ Object
Returns the value of attribute phrase.
13 14 15 |
# File 'lib/babelfish/phrase/plural_forms_parser.rb', line 13 def phrase @phrase end |
#regular_forms ⇒ Object
Returns the value of attribute regular_forms.
13 14 15 |
# File 'lib/babelfish/phrase/plural_forms_parser.rb', line 13 def regular_forms @regular_forms end |
#strict_forms ⇒ Object
Returns the value of attribute strict_forms.
13 14 15 |
# File 'lib/babelfish/phrase/plural_forms_parser.rb', line 13 def strict_forms @strict_forms end |
Instance Method Details
#init(phrase) ⇒ Object
Initializes parser. Should not be called directly.
30 31 32 33 34 |
# File 'lib/babelfish/phrase/plural_forms_parser.rb', line 30 def init( phrase ) self.phrase = phrase self.regular_forms = [] self.strict_forms = {} end |
#parse(phrase) ⇒ Object
Parses specified phrase.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/babelfish/phrase/plural_forms_parser.rb', line 37 def parse( phrase ) init( $phrase ) unless phrase.nil? # тут проще регуляркой forms = phrase.split( /(?<!\\)\|/ ) forms.each do |form| value = nil if form =~ /^=([0-9]+)\s*(.+)$/ value, form = $1, $2 end form = phrase_parser.parse( form ) if value.nil? regular_forms.push form else strict_forms[value] = form end end return { strict: strict_forms, regular: regular_forms, } end |
#phrase_parser ⇒ Object
19 20 21 |
# File 'lib/babelfish/phrase/plural_forms_parser.rb', line 19 def phrase_parser PluralFormsParser.phrase_parser ||= Babelfish::Phrase::Parser.new end |