Sha256: 3902f3a33781cd80842a0d14d6b49df8271fac498850a4835f5175122d256859

Contents?: true

Size: 1.49 KB

Versions: 1

Compression:

Stored size: 1.49 KB

Contents

require 'factory_girl'

module ActiveCucumber

  # Creates ActiveRecord entries with data from given Cucumber tables.
  class Creator

    include FactoryGirl::Syntax::Methods

    def initialize attributes, context
      @attributes = attributes
      context.each do |key, value|
        instance_variable_set "@#{key}", value
      end
    end

    # Returns the FactoryGirl version of this Creator's attributes
    def factorygirl_attributes
      symbolize_attributes!
      @attributes.each do |key, value|
        next unless respond_to?(method = method_name(key))
        if (result = send method, value)
          @attributes[key] = result if @attributes.key? key
        else
          @attributes.delete key
        end
      end
    end


  private

    def method_missing method_name, *arguments
      # This is necessary so that a Creator subclass can access
      # methods of @attributes as if they were its own.
      @attributes.send method_name, *arguments
    end


    # Returns the name of the value_for method for the given key
    def method_name key
      "value_for_#{key}"
    end


    # Converts the key given in Cucumber format into FactoryGirl format
    def normalized_key key
      key.downcase.parameterize.underscore.to_sym
    end


    # Makes the keys on @attributes be normalized symbols
    def symbolize_attributes!
      @attributes = {}.tap do |result|
        @attributes.each do |key, value|
          result[normalized_key key] = value
        end
      end
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_cucumber-0.0.7 lib/active_cucumber/creator.rb