Sha256: 743e176b77429e6bf35706612f79289e489ba44b185c42916f5f80dedf3272bf

Contents?: true

Size: 1.61 KB

Versions: 2

Compression:

Stored size: 1.61 KB

Contents

# 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 Mumukit::Assistant::Narrator
  def initialize(seed)
    @seed = seed
  end

  # Generated a markdown explanation using the seeded phrases. Uses `I18n` to get
  # the appropriate locale.
  def compose_explanation(tips)
    "#{explanation_introduction_phrase}\n\n#{explanation_paragraphs(tips).join("\n\n")}\n\n#{retry_phrase}\n"
  end

  # Generates an html explantion.
  # See `compose_explanation`
  def compose_explanation_html(tips)
    Mumukit::ContentType::Markdown.to_html compose_explanation(tips)
  end

  def retry_phrase
    t :retry
  end

  def explanation_introduction_phrase
    t :introduction
  end

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

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

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

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

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

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

  private

  def t(key, args={})
    I18n.t "narrator.#{key}_#{@seed[key]}", args
  end

  def self.random_index
    (0..2).to_a.sample
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mumukit-assistant-0.3.0 lib/mumukit/assistant/narrator.rb
mumukit-assistant-0.2.1 lib/mumukit/assistant/narrator.rb