Sha256: 2d7fdd27a69912c71eb56d121d224871162e94a0a7ec2180ef2721f478ce6f2e

Contents?: true

Size: 1.55 KB

Versions: 4

Compression:

Stored size: 1.55 KB

Contents

module Hypercuke

  # The step driver serves as the entry point from Cucumber step
  # definition bodies to the Hypercuke API.  An instance of this object
  # will be made available to Cucumber::World via the #step_driver
  # method.  
  #
  # The StepDriver will interpret any message sent to it as a topic
  # name, combine that with the current_layer name from Hypercuke, and
  # use that to instantitate a StepAdapter for the appropriate
  # topic/layer combo.
  class StepDriver
    def layer_name
      Hypercuke.current_layer
    end

    def method_missing(method, *_O) # No arguments for you, Mister Bond! *adjusts monocle*
      topic_name = method.to_sym

      # Define a method for the topic name so that future requests for
      # the same step adapter don't pay the method_missing tax.
      self.class.send(:define_method, topic_name) do
        # Within the defined method, memoize the step adapter so that
        # future requests also don't pay the GC tax.
        key = [topic_name, layer_name] # key on both names in case someone changes the layer on us
        __step_adapters__[key] ||=
          begin
            klass = Hypercuke.step_adapter_class(*key)
            klass.new(__context__, self)
          end
      end

      # And don't forget to invoke the newly-created method.
      send(method)
    end

    # StepDriver is eager to please.
    def respond_to_missing?(_)
      true
    end

    private

    def __context__
      @__context__ ||= Hypercuke::Context.new
    end

    def __step_adapters__
      @__step_adapters__ ||= {}
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
hypercuke-0.5.2 lib/hypercuke/step_driver.rb
hypercuke-0.5.1 lib/hypercuke/step_driver.rb
hypercuke-0.5.0 lib/hypercuke/step_driver.rb
hypercuke-0.4.1 lib/hypercuke/step_driver.rb