Class: Verbs::Verb

Inherits:
Object
  • Object
show all
Defined in:
lib/verbs/verb.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(infinitive, options = {}, &blk) ⇒ Verb

Returns a new instance of Verb.



5
6
7
8
9
10
11
12
13
14
# File 'lib/verbs/verb.rb', line 5

def initialize(infinitive, options = {}, &blk)
  @infinitive = infinitive
  @forms = {}
  if block_given?
    yield self
  else
    @preterite = options[:preterite]
    @past_participle = options[:past_participle]
  end
end

Instance Attribute Details

#infinitiveObject (readonly)

Returns the value of attribute infinitive.



3
4
5
# File 'lib/verbs/verb.rb', line 3

def infinitive
  @infinitive
end

#past_participleObject (readonly)

Returns the value of attribute past_participle.



3
4
5
# File 'lib/verbs/verb.rb', line 3

def past_participle
  @past_participle
end

#preteriteObject (readonly)

Returns the value of attribute preterite.



3
4
5
# File 'lib/verbs/verb.rb', line 3

def preterite
  @preterite
end

Instance Method Details

#[](options = {}) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/verbs/verb.rb', line 33

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

#form(word, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/verbs/verb.rb', line 16

def form(word, options = {})
  raise ArgumentError, 'Irregular verb specifications must identify tense and must identify either derivative, mood, or person and plurality' unless options[:tense] and (options[:derivative] or options[:mood] or (options[:person] and options[:plurality]))
  
  tense = options[:tense]

  @forms[:present] ||= {}
  @forms[:past] ||= {}
  if derivative = options[:derivative]
    @forms[tense][derivative] = word
  elsif mood = options[:mood]
    @forms[tense][mood] = word
  elsif person = options[:person]
    @forms[tense][person] ||= {}
    @forms[tense][person][options[:plurality]] = word
  end
end