Sha256: 6222563e625b8393433fd1995fdc1e110111f65c44ae0ca9155fdd10ad0ecfde

Contents?: true

Size: 1.37 KB

Versions: 7

Compression:

Stored size: 1.37 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
    undef_method(:id) if method_defined?(:id)

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

      @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

7 entries across 7 versions & 2 rubygems

Version Path
challah-0.3.0 vendor/bundle/gems/factory_girl-2.5.0/lib/factory_girl/evaluator.rb
challah-0.2.1 vendor/bundle/gems/factory_girl-2.5.0/lib/factory_girl/evaluator.rb
challah-0.2.0 vendor/bundle/gems/factory_girl-2.5.0/lib/factory_girl/evaluator.rb
factory_girl-2.5.0 lib/factory_girl/evaluator.rb
factory_girl-2.4.2 lib/factory_girl/evaluator.rb
factory_girl-2.4.1 lib/factory_girl/evaluator.rb
factory_girl-2.4.0 lib/factory_girl/evaluator.rb