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 = {}) ⇒ Verb

Returns a new instance of Verb.



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

def initialize(infinitive, options = {})
  @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.



5
6
7
# File 'lib/verbs/verb.rb', line 5

def infinitive
  @infinitive
end

#past_participleObject (readonly)

Returns the value of attribute past_participle.



5
6
7
# File 'lib/verbs/verb.rb', line 5

def past_participle
  @past_participle
end

#preteriteObject (readonly)

Returns the value of attribute preterite.



5
6
7
# File 'lib/verbs/verb.rb', line 5

def preterite
  @preterite
end

Instance Method Details

#[](options = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/verbs/verb.rb', line 38

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

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



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/verbs/verb.rb', line 18

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

  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