Sha256: cca48ee6f25d5d5f8c9a6071b61cacd8449da1d7c77b9b0de148765c03397f8b

Contents?: true

Size: 1.28 KB

Versions: 1

Compression:

Stored size: 1.28 KB

Contents

module Verbs
  class Verb
    attr_reader :infinitive, :preterite, :past_participle
    
    def initialize(infinitive, options = {}, &blk)
      @infinitive = infinitive
      @forms = {}
      if block_given?
        yield self
      else
        @preterite = options[:preterite]
        @past_participle = options[:past_participle]
      end
    end
    
    def form(word, options = {})
      raise ArgumentError, 'Irregular verb specifications must identify tense and must identify either derivative or person and plurality' unless options[:tense] and (options[:derivative] or (options[:person] and options[:plurality]))
      
      tense = options[:tense]

      @forms[:present] ||= {}
      @forms[:past] ||= {}
      if derivative = options[:derivative]
        @forms[tense][derivative] = word
      elsif person = options[:person]
        @forms[tense][person] ||= {}
        @forms[tense][person][options[:plurality]] = word
      end
    end
    
    def [](options = {})
      tense, person, plurality, derivative = options[:tense], options[:person], options[:plurality], options[:derivative]
      if tense and person and plurality
        @forms[tense].try(:[], person).try(:[], plurality)
      elsif tense and derivative
        @forms[tense].try(:[], derivative)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
verbs-2.0.10 lib/verbs/verb.rb