Sha256: 92459e59a82ba69b39b57d0cf0d09b16287c0c14310f58cfe54667dc6fffe9c8

Contents?: true

Size: 1.59 KB

Versions: 15

Compression:

Stored size: 1.59 KB

Contents

require "active_support/core_ext/class/attribute"

module FactoryGirl
  class Evaluator
    class_attribute :callbacks, :attribute_lists

    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

    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

      singleton = class << self; self end
      @overrides.each do |name, value|
        singleton.send :define_method, name, lambda { value }
      end

      @build_strategy.add_observer(CallbackRunner.new(self.class.callbacks, self))
    end

    delegate :association, :to => :@build_strategy

    def instance=(object_instance)
      @instance = object_instance
    end

    def method_missing(method_name, *args, &block)
      if @cached_attributes.key?(method_name)
        @cached_attributes[method_name]
      else
        @instance.send(method_name, *args, &block)
      end
    end

    def __overrides
      @overrides
    end

    private

    class CallbackRunner
      def initialize(callbacks, evaluator)
        @callbacks = callbacks
        @evaluator = evaluator
      end

      def update(name, result_instance)
        @callbacks.select {|callback| callback.name == name }.each do |callback|
          callback.run(result_instance, @evaluator)
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 2 rubygems

Version Path
challah-0.6.1 vendor/bundle/gems/factory_girl-2.5.1/lib/factory_girl/evaluator.rb
challah-0.6.0 vendor/bundle/gems/factory_girl-2.5.1/lib/factory_girl/evaluator.rb
challah-0.5.4 vendor/bundle/gems/factory_girl-2.5.1/lib/factory_girl/evaluator.rb
challah-0.5.3 vendor/bundle/gems/factory_girl-2.5.1/lib/factory_girl/evaluator.rb
challah-0.5.2 vendor/bundle/gems/factory_girl-2.5.1/lib/factory_girl/evaluator.rb
challah-0.5.0 vendor/bundle/gems/factory_girl-2.5.1/lib/factory_girl/evaluator.rb
factory_girl-2.5.2 lib/factory_girl/evaluator.rb
challah-0.4.1 vendor/bundle/gems/factory_girl-2.5.1/lib/factory_girl/evaluator.rb
challah-0.4.0 vendor/bundle/gems/factory_girl-2.5.1/lib/factory_girl/evaluator.rb
challah-0.3.5 vendor/bundle/gems/factory_girl-2.5.1/lib/factory_girl/evaluator.rb
challah-0.3.4 vendor/bundle/gems/factory_girl-2.5.1/lib/factory_girl/evaluator.rb
challah-0.3.3 vendor/bundle/gems/factory_girl-2.5.1/lib/factory_girl/evaluator.rb
challah-0.3.2 vendor/bundle/gems/factory_girl-2.5.1/lib/factory_girl/evaluator.rb
challah-0.3.1 vendor/bundle/gems/factory_girl-2.5.1/lib/factory_girl/evaluator.rb
factory_girl-2.5.1 lib/factory_girl/evaluator.rb