Sha256: 910e45fe16513b2e1afb889eeaebd4463d0a598cff98631aadf53079304e6e41

Contents?: true

Size: 1.41 KB

Versions: 7

Compression:

Stored size: 1.41 KB

Contents

class Sinbotra::Bot
  class Conversation
    Step = Struct.new(:id, :callback)

    attr_reader :id
    attr_reader :steps

    def initialize(id, store={})
      @id           = id
      @steps        = []
      @store        = store
      update_current_step(0)
      store!(:is_active, false)
    end

    def start
      store!(:is_active, true)
    end

    def active?
      recall(:is_active)
    end

    def current_step
      recall(:_current_step)
    end

    def step(id, &blk)
      @steps << Step.new(id, blk)
    end

    def store!(k, v)
      @store[k] = v
    end

    def recall(k)
      @store[k]
    end

    def say(text)
      puts text
    end

    def skip_to_next_step!
      puts "NOOP skip_to_next_step!"
    end

    def next_step!
      new_step = current_step + 1
      update_current_step(new_step)
      done! if completed_last_step?
    end

    def next_step_now!(bot)
      next_step!
      perform_current_step(bot)
    end

    def repeat_step!
      puts "Repeating conversation step. Nothing to do here."
    end

    def done!
      update_current_step(0)
      store!(:is_active, false)
    end

    def perform_current_step(bot)
      current_action.(bot)
    end

    private

    def completed_last_step?
      current_step == @steps.size
    end

    def update_current_step(n)
      store!(:_current_step, n)
    end

    def current_action
      @steps[current_step].callback
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
sinbotra-0.1.6 lib/sinbotra/bot/conversation.rb
sinbotra-0.1.5 lib/sinbotra/bot/conversation.rb
sinbotra-0.1.4 lib/sinbotra/bot/conversation.rb
sinbotra-0.1.3 lib/sinbotra/bot/conversation.rb
sinbotra-0.1.2 lib/sinbotra/bot/conversation.rb
sinbotra-0.1.1 lib/sinbotra/bot/conversation.rb
sinbotra-0.1.0 lib/sinbotra/bot/conversation.rb