Class: LLT::DeclinableBuilder

Inherits:
FormBuilder show all
Defined in:
lib/llt/form_builder/declinable_builder.rb

Constant Summary

Constants inherited from FormBuilder

FormBuilder::VERSION

Instance Attribute Summary collapse

Attributes inherited from FormBuilder

#impersonalium

Instance Method Summary collapse

Methods inherited from FormBuilder

#attributes_by_index, build, #corrections, #cross_indices, #default_args, #downcase_all_stems, #endings, #endings_attributes, #endings_container, #endings_namespace, #endings_path, #extensions_and_other_signs, #form_class, #indices_by_ending, #init_keys, #keep_given_value, lookup_class, #lookup_indices, #needs_validation?, #new_form, #prefix, #reverse_lookup, #stays_capitalized, #stem_copy, #valid?, #validation_rule, #validations, validations, #value_as_index

Constructor Details

#initialize(stem) ⇒ DeclinableBuilder

Returns a new instance of DeclinableBuilder.



5
6
7
8
9
# File 'lib/llt/form_builder/declinable_builder.rb', line 5

def initialize(stem)
  super
  @irregular_forms = extended_special_forms(irregulars_with_nominative(stem))
  @additional_forms = extended_special_forms(stem[:additional_forms])
end

Instance Attribute Details

#comparatioObject (readonly)

Returns the value of attribute comparatio.



3
4
5
# File 'lib/llt/form_builder/declinable_builder.rb', line 3

def comparatio
  @comparatio
end

#sexusObject (readonly)

Returns the value of attribute sexus.



3
4
5
# File 'lib/llt/form_builder/declinable_builder.rb', line 3

def sexus
  @sexus
end

Instance Method Details

#additional_formsObject



102
103
104
105
106
107
108
109
110
# File 'lib/llt/form_builder/declinable_builder.rb', line 102

def additional_forms
  @additional_forms.map do |string, *attrs|
    casus, numerus, sexus = *attrs
    i = ioe(casus, numerus, sexus)
    if @lookup_indices.include?(i)
      new_special_form(string: string, casus: casus, numerus: numerus, sexus: sexus, index: i)
    end
  end.compact
end

#casus_numerus_sexus_by_index(index, sexus = @sexus) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/llt/form_builder/declinable_builder.rb', line 26

def casus_numerus_sexus_by_index(index, sexus = @sexus)
  # superflous method - could be solved through
  # attributes_by_index, however this is noticably faster and thus stays

  if index < 6
    [index + 1, 1, sexus]
  else
    [index - 5, 2, sexus]
  end
end

#computeObject



11
12
13
# File 'lib/llt/form_builder/declinable_builder.rb', line 11

def compute
  with_replacements(regular_forms) + additional_forms
end

#endings_lookup(ending, x) ⇒ Object

this is a slight hack to handle O Declension on er. the Endings constants for noun has US at its first position, this guarantees a match even when the ending is empty (which means that the passsed along nominative value shall be used. similar conditions will need to be written for A decl on as.



152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/llt/form_builder/declinable_builder.rb', line 152

def endings_lookup(ending, x)
  if ending.empty?
    if er_nominative_possible?
      # not sure if this needs further safeties - asking explicitly for er?
      # well it does! 2013-10-08 - fili!
      return x.to_s =~ /^(us|e)$/
    elsif o_declension_on_ius?
      return x.to_s =~ /^e$/
    end
  end
  super
end

#er_nominative_possible?Boolean

Returns:

  • (Boolean)


141
142
143
144
145
# File 'lib/llt/form_builder/declinable_builder.rb', line 141

def er_nominative_possible?
  # only Noun and Adjective override this, as they can have a nominative on er.
  # other Declinables like Gerundives don't need this
  false
end

#extended_special_forms(specials) ⇒ Object



50
51
52
53
54
55
# File 'lib/llt/form_builder/declinable_builder.rb', line 50

def extended_special_forms(specials)
  specials = specials || []
  specials + specials.each_with_object([]) do |(string, *attr), arr|
    new_attributes(*attr).each { |new_attr| arr << [string,  *new_attr] }
  end
end

#index_of_ending(casus, numerus, sexus = nil) ⇒ Object Also known as: ioe



21
22
23
# File 'lib/llt/form_builder/declinable_builder.rb', line 21

def index_of_ending(casus, numerus, sexus = nil)
  casus + (numerus == 1 ? -1 : 5)
end

#indicesObject



15
16
17
18
19
# File 'lib/llt/form_builder/declinable_builder.rb', line 15

def indices
  { casus:   [0, 1, 2, 3, 4, 5],
    numerus: [0, 6],
    sexus:   [0, 12, 24] }
end

#irregulars_with_nominative(stem) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/llt/form_builder/declinable_builder.rb', line 57

def irregulars_with_nominative(stem)
  i = if @nom
        irreg = nominatives
        if irregs = stem[:irregular_forms]
          irregs + irreg
        else
          irreg
        end
      else
        stem[:irregular_forms] || []
      end

  i + proper_vocative
end

#new_attributes(*attrs) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/llt/form_builder/declinable_builder.rb', line 72

def new_attributes(*attrs)
  casus, numerus, sexus = *attrs
  sexus = sexus || @sexus
  new_attrs = []

  if ioe(casus, numerus) == 0
    new_attrs << [5, 1, sexus] unless regular_o_declension? || pronominal_declension?
    if sexus == :n
      new_attrs << [4, 1, sexus]
    end
  end

  new_attrs
end

#new_form_through_index(ending, i) ⇒ Object



44
45
46
47
48
# File 'lib/llt/form_builder/declinable_builder.rb', line 44

def new_form_through_index(ending, i)
  casus, numerus, sexus = casus_numerus_sexus_by_index(i)
  ending = ending.to_s
  new_form(ending: ending, casus: casus, numerus: numerus, sexus: sexus, index: i)
end

#new_special_form(args) ⇒ Object



124
125
126
127
128
129
130
# File 'lib/llt/form_builder/declinable_builder.rb', line 124

def new_special_form(args)
  args = args.merge(default_args)
  string = args[:string]
  i = index_of_ending(args[:casus], args[:numerus], args[:sexus])
  ending = special_ending(string, i).to_s
  form_class.new(args.merge(stem: string.chomp(ending), ending: ending))
end

#o_declension_on_ius?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/llt/form_builder/declinable_builder.rb', line 98

def o_declension_on_ius?
  @inflection_class == 2 && @stem.end_with?("i") && @sexus == :m # is there a neutrum like that?
end

#pronominal_declension?Boolean

Returns:

  • (Boolean)


95
96
# File 'lib/llt/form_builder/declinable_builder.rb', line 95

def pronominal_declension?
end

#proper_vocativeObject



87
88
89
# File 'lib/llt/form_builder/declinable_builder.rb', line 87

def proper_vocative
  o_declension_on_ius? ? [[@stem, 5, 1]] : []
end

#regular_formsObject



37
38
39
40
41
42
# File 'lib/llt/form_builder/declinable_builder.rb', line 37

def regular_forms
  endings.each_with_index.map do |ending, i|
    next unless @lookup_indices.include?(i)
    new_form_through_index(ending, i)
  end.compact # compact because of next steps
end

#regular_o_declension?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/llt/form_builder/declinable_builder.rb', line 91

def regular_o_declension?
  @inflection_class == 2 && @nom.to_s =~ /us$/ # to_s because it might be nil
end

#special_ending(word, index) ⇒ Object



132
133
134
135
136
137
138
139
# File 'lib/llt/form_builder/declinable_builder.rb', line 132

def special_ending(word, index)
  endings_path.constants.each do |const|
    # the rescue clause is a primitive jruby hack - needs more work
    ending = endings_path.const_get(const)[index] rescue nil
    return ending if word.end_with?(ending.to_s)
  end
  nil
end

#with_replacements(regular_forms) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/llt/form_builder/declinable_builder.rb', line 112

def with_replacements(regular_forms)
  @irregular_forms.each do |string, *attrs|
    casus, numerus, sexus = *attrs
    i = ioe(casus, numerus, sexus)
    if @lookup_indices.include?(i)
      next unless ri = regular_forms.find_index { |f| f.index == i }
      regular_forms[ri] = new_special_form(string: string, casus: casus, numerus: numerus, sexus: sexus, index: i)
    end
  end
  regular_forms
end