Sha256: dd54b964397ba81967cd47b4e8c8990f16e3ffeccb3bac2582f1f4ed31b28c45

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

module Zenlish
  module Inflect
    class InflectionTable
      attr_reader :name
      attr_reader :headings
      attr_reader :rules

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

      def add_heading(aHeading)
        @headings << aHeading
      end

      def add_rule(aRule)
        @rules << aRule
      end

      def inflect(aLexeme, theConstraints)
        err_msg = "Table has #{headings.size} headings."
        raise StandardError, err_msg if theConstraints.size != headings.size
        actuals = []
        headings.each_with_index do |hd, idx|
          if theConstraints[idx]
            actuals << theConstraints[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
    end # class
  end # module
end # module

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
zenlish-0.2.00 lib/zenlish/inflect/inflection_table.rb