Class: Zenlish::Inflect::InflectionTable

Inherits:
Object
  • Object
show all
Defined in:
lib/zenlish/inflect/inflection_table.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(aName) ⇒ InflectionTable

Returns a new instance of InflectionTable.



9
10
11
12
13
# File 'lib/zenlish/inflect/inflection_table.rb', line 9

def initialize(aName)
  @name = aName
  @headings = []
  @rules = []
end

Instance Attribute Details

#headingsObject (readonly)

Returns the value of attribute headings.



6
7
8
# File 'lib/zenlish/inflect/inflection_table.rb', line 6

def headings
  @headings
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/zenlish/inflect/inflection_table.rb', line 5

def name
  @name
end

#rulesObject (readonly)

Returns the value of attribute rules.



7
8
9
# File 'lib/zenlish/inflect/inflection_table.rb', line 7

def rules
  @rules
end

Instance Method Details

#add_heading(aHeading) ⇒ Object



15
16
17
# File 'lib/zenlish/inflect/inflection_table.rb', line 15

def add_heading(aHeading)
  @headings << aHeading
end

#add_rule(aRule) ⇒ Object



19
20
21
# File 'lib/zenlish/inflect/inflection_table.rb', line 19

def add_rule(aRule)
  @rules << aRule
end

#inflect(aLexeme, theConstraints) ⇒ Object

Raises:

  • (StandardError)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/zenlish/inflect/inflection_table.rb', line 23

def inflect(aLexeme, theConstraints)
  constraints = if theConstraints.nil? || theConstraints.empty?
    Array.new(headings.size) { |_i| nil }
  else
    theConstraints
  end
  err_msg = "Table has #{headings.size} headings, instead of #{constraints.size}"
  raise StandardError, err_msg if constraints.size != headings.size
  actuals = []
  headings.each_with_index do |hd, idx|
    if constraints[idx]
      actuals << constraints[idx]
    else
      actuals << hd.evaluate_for(aLexeme)
    end
  end
  # Hit policy: first
  matching_rule = rules.find do |rule| 
    rule.success?(headings, aLexeme, actuals) 
  end
  unless matching_rule
    err_msg = "No rule in table covers case: lexeme #{aLexeme}, actuals: #{p actuals}."
    raise StandardError, err_msg
  end
  matching_rule.apply(headings, aLexeme, actuals)
end