Class: ConjugateFrench::Tense

Inherits:
Object
  • Object
show all
Defined in:
lib/conjugate/tense.rb

Constant Summary collapse

TENSES =
%w[indicatif subjonctif conditionnel impératif participe].freeze
VERB_ERROR =
"The verb you've entered can not be conjugated".freeze
TENSE_ERROR =
"The verb can not be conjugated at the tense you've entered".freeze

Class Method Summary collapse

Class Method Details

.build_tables(full_verb, specific_tense) ⇒ Object

Raises:

  • (ArgumentError)


14
15
16
17
18
19
20
21
22
# File 'lib/conjugate/tense.rb', line 14

def self.build_tables(full_verb, specific_tense)
  raise ArgumentError, VERB_ERROR if full_verb.nil?

  if specific_tense.nil?
    TENSES.map { |tense| table(tense, full_verb) }
  else
    table(specific_tense, full_verb)
  end
end

.conjugate(verb, tense) ⇒ Object



10
11
12
# File 'lib/conjugate/tense.rb', line 10

def self.conjugate(verb, tense)
  build_tables(get_verbe(verb), tense)
end

.get_verbe(verb) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/conjugate/tense.rb', line 24

def self.get_verbe(verb)
  file = File.read('lib/verbs.json')
  parsed_file = JSON.parse(file)
  @get_verbe ||= parsed_file.select do |verb_list|
    verb_list['infinitif']['présent'][0] == verb
  end[0]
end

.headings(tense, full_verb) ⇒ Object

Raises:

  • (ArgumentError)


32
33
34
35
36
# File 'lib/conjugate/tense.rb', line 32

def self.headings(tense, full_verb)
  raise ArgumentError, TENSE_ERROR unless TENSES.include?(tense)

  [nil, full_verb[tense].keys]
end

.persons(tense) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/conjugate/tense.rb', line 53

def self.persons(tense)
  if tense == 'impératif'
    [[nil], [nil], [nil]]
  elsif tense == 'participe'
    [[nil], [nil]]
  else
    [['Je'], ['Tu'], ['Il'], ['Nous'], ['Vous'], ['Ils']]
  end
end

.table(tense, full_verb) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/conjugate/tense.rb', line 38

def self.table(tense, full_verb)
  tense_rows = persons(tense)
  verb = full_verb['infinitif']['présent'].first

  tense_rows.each_with_index do |person, index|
    full_verb[tense].each { |row| person << row[1][index] }
  end

  tense = Terminal::Table.new(
    title: "Verbe #{verb.capitalize} #{tense.capitalize}",
    rows: tense_rows,
    headings: headings(tense, full_verb).flatten
  )
end