Class: Mumukit::Assistant::Narrator

Inherits:
Object
  • Object
show all
Defined in:
lib/mumukit/assistant/narrator.rb

Overview

A narrator can turn tips - generated by ‘Mumukit::Assistant` - into a humanized text with the same information but a bit more friendly. This text is called explanation.

The narrator uses some internationalized random phrases, and it does provide a seed as a construction argument to allow its testing.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(seed) ⇒ Narrator

Returns a new instance of Narrator.



8
9
10
# File 'lib/mumukit/assistant/narrator.rb', line 8

def initialize(seed)
  @seed = seed
end

Class Method Details

.randomObject



50
51
52
# File 'lib/mumukit/assistant/narrator.rb', line 50

def self.random
  new seed(*5.times.map { random_index })
end

.seed(r, i, o, m, e) ⇒ Object



54
55
56
# File 'lib/mumukit/assistant/narrator.rb', line 54

def self.seed(r, i, o, m, e)
  { retry: r, introduction: i, opening: o, middle: m, ending: e }
end

Instance Method Details

#compose_explanation(tips) ⇒ Object

Generated a markdown explanation using the seeded phrases. Uses ‘I18n` to get the appropriate locale.



14
15
16
# File 'lib/mumukit/assistant/narrator.rb', line 14

def compose_explanation(tips)
  "#{explanation_introduction_phrase}\n\n#{explanation_paragraphs(tips).join("\n\n")}\n\n#{retry_phrase}\n"
end

#compose_explanation_html(tips) ⇒ Object

Generates an html explantion. See ‘compose_explanation`



20
21
22
# File 'lib/mumukit/assistant/narrator.rb', line 20

def compose_explanation_html(tips)
  Mumukit::ContentType::Markdown.to_html compose_explanation(tips)
end

#explanation_ending_paragraph(tip) ⇒ Object



46
47
48
# File 'lib/mumukit/assistant/narrator.rb', line 46

def explanation_ending_paragraph(tip)
  t :ending, tip: tip
end

#explanation_introduction_phraseObject



28
29
30
# File 'lib/mumukit/assistant/narrator.rb', line 28

def explanation_introduction_phrase
  t :introduction
end

#explanation_middle_paragraph(tip) ⇒ Object



42
43
44
# File 'lib/mumukit/assistant/narrator.rb', line 42

def explanation_middle_paragraph(tip)
  t :middle, tip: tip
end

#explanation_opening_paragraph(tip) ⇒ Object



38
39
40
# File 'lib/mumukit/assistant/narrator.rb', line 38

def explanation_opening_paragraph(tip)
  "#{tip.upcase_first}."
end

#explanation_paragraphs(tips) ⇒ Object



32
33
34
35
36
# File 'lib/mumukit/assistant/narrator.rb', line 32

def explanation_paragraphs(tips)
  tips.take(3).zip([:opening, :middle, :ending]).map do |tip, selector|
    send "explanation_#{selector}_paragraph", tip
  end
end

#retry_phraseObject



24
25
26
# File 'lib/mumukit/assistant/narrator.rb', line 24

def retry_phrase
  t :retry
end