Sha256: 4e4aafa2f20b308ec1667097eecb0fa0a1100deaa048a7795aa1efd594d80bec

Contents?: true

Size: 1.99 KB

Versions: 17

Compression:

Stored size: 1.99 KB

Contents

require 'active_support/core_ext/hash/except'
require 'active_support/core_ext/class/attribute'

module FactoryGirl
  # @api private
  class Evaluator
    class_attribute :attribute_lists

    private_instance_methods.each do |method|
      undef_method(method) unless method =~ /^__|initialize/
    end

    def initialize(build_strategy, overrides = {})
      @build_strategy = build_strategy
      @overrides = overrides
      @cached_attributes = overrides
      @instance = nil

      @overrides.each do |name, value|
        singleton_class.define_attribute(name) { value }
      end
    end

    def association(factory_name, *traits_and_overrides)
      overrides = traits_and_overrides.extract_options!
      strategy_override = overrides.fetch(:strategy) { :create }

      traits_and_overrides += [overrides.except(:strategy)]

      runner = FactoryRunner.new(factory_name, strategy_override, traits_and_overrides)
      @build_strategy.association(runner)
    end

    def instance=(object_instance)
      @instance = object_instance
    end

    def method_missing(method_name, *args, &block)
      if @instance.respond_to?(method_name)
        @instance.send(method_name, *args, &block)
      else
        SyntaxRunner.new.send(method_name, *args, &block)
      end
    end

    def respond_to_missing?(method_name, include_private = false)
      @instance.respond_to?(method_name) || SyntaxRunner.new.respond_to?(method_name)
    end

    def __override_names__
      @overrides.keys
    end

    def increment_sequence(sequence)
      sequence.next(self)
    end

    def self.attribute_list
      AttributeList.new.tap do |list|
        attribute_lists.each do |attribute_list|
          list.apply_attributes attribute_list.to_a
        end
      end
    end

    def self.define_attribute(name, &block)
      define_method(name) do
        if @cached_attributes.key?(name)
          @cached_attributes[name]
        else
          @cached_attributes[name] = instance_exec(&block)
        end
      end
    end
  end
end

Version data entries

17 entries across 14 versions & 6 rubygems

Version Path
opal-factory_girl-4.5.0.3 factory_girl/lib/factory_girl/evaluator.rb
sc_core-0.0.7 test/dummy/vendor/bundle/ruby/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/evaluator.rb
solidus_backend-1.0.0.pre3 vendor/bundle/gems/factory_girl-4.5.0/lib/factory_girl/evaluator.rb
solidus_backend-1.0.0.pre2 vendor/bundle/gems/factory_girl-4.5.0/lib/factory_girl/evaluator.rb
solidus_backend-1.0.0.pre vendor/bundle/gems/factory_girl-4.5.0/lib/factory_girl/evaluator.rb
apl-library-0.0.90 vendor/bundle/ruby/1.9.1/gems/factory_girl-4.4.0/lib/factory_girl/evaluator.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/1.9.1/gems/factory_girl-4.4.0/lib/factory_girl/evaluator.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/2.1.0/gems/factory_girl-4.4.0/lib/factory_girl/evaluator.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/factory_girl-4.4.0/lib/factory_girl/evaluator.rb
factory_girl-4.5.0 lib/factory_girl/evaluator.rb
factory_girl-4.4.0 lib/factory_girl/evaluator.rb
factory_girl-4.3.0 lib/factory_girl/evaluator.rb
challah-1.0.0 vendor/bundle/gems/factory_girl-4.2.0/lib/factory_girl/evaluator.rb
challah-1.0.0.beta3 vendor/bundle/gems/factory_girl-4.2.0/lib/factory_girl/evaluator.rb
challah-1.0.0.beta2 vendor/bundle/gems/factory_girl-4.2.0/lib/factory_girl/evaluator.rb
challah-1.0.0.beta vendor/bundle/gems/factory_girl-4.2.0/lib/factory_girl/evaluator.rb
factory_girl-4.2.0 lib/factory_girl/evaluator.rb