Module: Common

Included in:
Conjugate::French, Conjugate::Spanish
Defined in:
lib/conjugate/common.rb

Constant Summary collapse

@@dividing_infinitive_regex =
/\{{3}\d+\}{3}([[:alpha:]]+)/

Instance Method Summary collapse

Instance Method Details

#conjugate(opts = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/conjugate/common.rb', line 5

def conjugate(opts ={})
  template = template(opts)
  tense = tense(opts[:tense])
  verb = opts[:verb]
  infinitive = template[:infinitive].dup

  verb_parts = divide_infinitive(infinitive, verb)

  return nil if (tense != :passe_compose) && (template[tense].nil? || template[tense][opts[:pronoun].to_sym].nil?)

  conjugation_template = nil

  if defined? conjugation_template_finder
    conjugation_template = conjugation_template_finder(template, tense, opts).dup
  else
    conjugation_template = template[tense][opts[:pronoun].to_sym].dup
  end

  positions = conjugation_template.scan(/\{{3}(\d+)\}{3}/).flatten

  positions.each do |p|
    if opts[:highlight]
      conjugation_template.gsub!(/\{{3}#{p}\}{3}/, "<span class='regular'>#{ verb_parts[p.to_i - 1] }</span>")
    else
      conjugation_template.gsub!(/\{{3}#{p}\}{3}/, verb_parts[p.to_i - 1])
    end
  end
  conjugation_template
end

#divide_infinitive(infinitive, verb) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/conjugate/common.rb', line 47

def divide_infinitive(infinitive, verb)
  inserts = infinitive.scan(@@dividing_infinitive_regex).flatten

  word_parts = []
  word_copy = verb.dup

  inserts.each do |letters|
    sub_word = ""
    if letters.length <= 1
      sub_word = word_copy.scan(/(.[^#{letters}]*)#{letters}/).flatten.first
    else
      sub_word = word_copy.scan(/(.+)#{letters}/).flatten.first
    end
    sub_word ||= ""

    word_parts << sub_word
    word_copy = word_copy.gsub(/^#{sub_word}#{letters}/, '')
  end
  word_parts << word_copy unless word_copy == ""
  word_parts
end

#regular_ending(verb) ⇒ Object

def conjugation_template_finder(template, tense, opts)

end



39
40
41
# File 'lib/conjugate/common.rb', line 39

def regular_ending(verb)
  verb[-2..-1]
end

#tense(t) ⇒ Object



43
44
45
# File 'lib/conjugate/common.rb', line 43

def tense(t)
  (common_name(t) || :present).to_sym
end