Sha256: 03d85d60bf447aeb23c0bb17cd1f4b3524ebee532d6bc50020c1861c8677e3ce

Contents?: true

Size: 1.12 KB

Versions: 4

Compression:

Stored size: 1.12 KB

Contents

require 'erb'

module Pushpop

  class Step

    TEMPLATES_DIRECTORY = File.expand_path('../../../templates', __FILE__)

    class ERBContext
      attr_accessor :response
      attr_accessor :step_responses

      def initialize(response, step_responses)
        self.response = response
        self.step_responses = step_responses
      end

      def get_binding
        binding
      end
    end

    attr_accessor :name
    attr_accessor :plugin
    attr_accessor :block

    def initialize(name=nil, plugin=nil, &block)
      self.name = name || plugin || Pushpop.random_name
      self.plugin = plugin
      self.block = block
    end

    def template(filename, response, step_responses={}, directory=TEMPLATES_DIRECTORY)
      erb_context = ERBContext.new(response, step_responses)
      ERB.new(get_template_contents(filename, directory)).result(erb_context.get_binding)
    end

    def run(last_response=nil, step_responses=nil)
      self.instance_exec(last_response, step_responses, &block)
    end

    private

    def get_template_contents(filename, directory)
      File.read(File.join(directory, filename))
    end

  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
pushpop-0.2 lib/pushpop/step.rb
pushpop-0.1.2 lib/pushpop/step.rb
pushpop-0.1.1 lib/pushpop/step.rb
pushpop-0.1.0 lib/pushpop/step.rb