Sha256: 98645c1fb9d8dabd184b76cc6664c1aa5160d4d76e228351a5887b33e6126c9d

Contents?: true

Size: 1.47 KB

Versions: 5

Compression:

Stored size: 1.47 KB

Contents

module ActiveCucumber

  # A decorator for ActiveRecord objects that adds methods to
  # format record attributes as they are displayed in Cucumber tables.
  #
  # This class is used by default. You can subclass it to create
  # custom Cucumberators for your ActiveRecord classes.
  class Cucumberator

    # object - the instance to decorate
    def initialize object, context
      @object = object
      context.each do |key, value|
        instance_variable_set "@#{key}", value
      end
    end


    # Returns the Cucumber value for the given attribute key.
    #
    # If a value_for_* method is not defined for the given key,
    # returns the attribute value of the decorated object,
    # converted to a String.
    def value_for key
      method_name = value_method_name key
      if respond_to? method_name
        send(method_name).to_s
      else
        @object.send(normalized_key key).to_s
      end
    end


  private

    def method_missing method_name
      # This is necessary so that a Cucumberator subclass can access
      # attributes of @object as if they were its own.
      @object.send method_name
    end


    # Converts the key given in Cucumber format into the format used to
    # access attributes on an ActiveRecord instance.
    def normalized_key key
      key.to_s.downcase.parameterize.underscore
    end


    # Returns the name of the value_of_* method for the given key
    def value_method_name key
      "value_for_#{normalized_key key}"
    end

  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
active_cucumber-1.0.0 lib/active_cucumber/cucumberator.rb
active_cucumber-0.2.1 lib/active_cucumber/cucumberator.rb
active_cucumber-0.2.0 lib/active_cucumber/cucumberator.rb
active_cucumber-0.1.0 lib/active_cucumber/cucumberator.rb
active_cucumber-0.0.10 lib/active_cucumber/cucumberator.rb