Sha256: d92610f9f2406d8270c3b69070595e1a5736ea829669658ba1ac35bf980c94e6

Contents?: true

Size: 1.2 KB

Versions: 6

Compression:

Stored size: 1.2 KB

Contents

module Mumukit::Assistant::Message
  # Fixed messages are independent of the number
  # of attemps
  class Fixed
    def initialize(text)
      @text = text
    end

    def call(_)
      @text
    end
  end

  # Progressive messages depend on the number of attemps
  # They work with exactly two or three messages:
  #
  #  * the first message will be displayed in the first three attemps
  #  * the second message will be displayed in the fourth, fifth and sixth attemps
  #  * the third message will be displayed starting at the seventh attemp
  #
  class Progressive
    def initialize(alternatives)
      raise 'You need two or three alternatives' unless alternatives.size.between?(2, 3)
      @alternatives = alternatives
    end

    def call(attemps_count)
      case attemps_count
        when (1..3) then @alternatives.first
        when (4..6) then @alternatives.second
        else @alternatives.last
      end
    end
  end

  def self.parse(text_or_alternatives)
    if text_or_alternatives.is_a? String
      Fixed.new text_or_alternatives
    elsif text_or_alternatives.is_a? Array
      Progressive.new text_or_alternatives
    else
      raise "Wrong message format #{text_or_alternatives}"
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
mumukit-assistant-0.4.0 lib/mumukit/assistant/message.rb
mumukit-assistant-0.3.0 lib/mumukit/assistant/message.rb
mumukit-assistant-0.2.1 lib/mumukit/assistant/message.rb
mumukit-assistant-0.2.0 lib/mumukit/assistant/message.rb
mumukit-assistant-0.1.1 lib/mumukit/assistant/message.rb
mumukit-assistant-0.1.0 lib/mumukit/assistant/message.rb