Sha256: 961c6b1979accfbff3252afb760a07605a3d3b579eaeda2802eab94465c904e5

Contents?: true

Size: 1.9 KB

Versions: 31

Compression:

Stored size: 1.9 KB

Contents

module FactoryGirl
  class AttributeAssigner
    def initialize(evaluator, &instance_builder)
      @instance_builder         = instance_builder
      @evaluator                = evaluator
      @attribute_list           = evaluator.class.attribute_list
      @attribute_names_assigned = []
    end

    def object
      @evaluator.instance = build_class_instance
      build_class_instance.tap do |instance|
        attributes_to_set_on_instance.each do |attribute|
          instance.send("#{attribute}=", get(attribute))
          @attribute_names_assigned << attribute
        end
      end
    end

    def hash
      @evaluator.instance = NullObject.new

      attributes_to_set_on_hash.inject({}) do |result, attribute|
        result[attribute] = get(attribute)
        result
      end
    end

    private

    def build_class_instance
      @build_class_instance ||= @evaluator.instance_exec(&@instance_builder)
    end

    def get(attribute_name)
      @evaluator.send(attribute_name)
    end

    def attributes_to_set_on_instance
      attribute_names_to_assign - @attribute_names_assigned
    end

    def attributes_to_set_on_hash
      attribute_names_to_assign - association_names
    end

    def attribute_names_to_assign
      non_ignored_attribute_names + override_names - ignored_attribute_names - alias_names_to_ignore
    end

    def non_ignored_attribute_names
      @attribute_list.reject(&:ignored).map(&:name)
    end

    def ignored_attribute_names
      @attribute_list.select(&:ignored).map(&:name)
    end

    def association_names
      @attribute_list.associations.map(&:name)
    end

    def override_names
      @evaluator.__overrides.keys
    end

    def alias_names_to_ignore
      @attribute_list.reject(&:ignored).map do |attribute|
        override_names.map {|override| attribute.name if attribute.alias_for?(override) && attribute.name != override }
      end.flatten.compact
    end
  end
end

Version data entries

31 entries across 26 versions & 2 rubygems

Version Path
challah-0.6.2 vendor/bundle/gems/factory_girl-2.6.1/lib/factory_girl/attribute_assigner.rb
challah-0.6.1 vendor/bundle/gems/factory_girl-2.5.1/lib/factory_girl/attribute_assigner.rb
challah-0.6.1 vendor/bundle/gems/factory_girl-2.6.1/lib/factory_girl/attribute_assigner.rb
challah-0.6.0 vendor/bundle/gems/factory_girl-2.5.1/lib/factory_girl/attribute_assigner.rb
challah-0.6.0 vendor/bundle/gems/factory_girl-2.6.1/lib/factory_girl/attribute_assigner.rb
challah-0.5.4 vendor/bundle/gems/factory_girl-2.5.1/lib/factory_girl/attribute_assigner.rb
challah-0.5.4 vendor/bundle/gems/factory_girl-2.6.1/lib/factory_girl/attribute_assigner.rb
challah-0.5.3 vendor/bundle/gems/factory_girl-2.6.1/lib/factory_girl/attribute_assigner.rb
challah-0.5.3 vendor/bundle/gems/factory_girl-2.5.1/lib/factory_girl/attribute_assigner.rb
challah-0.5.2 vendor/bundle/gems/factory_girl-2.5.1/lib/factory_girl/attribute_assigner.rb
challah-0.5.2 vendor/bundle/gems/factory_girl-2.6.1/lib/factory_girl/attribute_assigner.rb
factory_girl-3.0.0.rc1 lib/factory_girl/attribute_assigner.rb
factory_girl-2.6.3 lib/factory_girl/attribute_assigner.rb
factory_girl-2.6.2 lib/factory_girl/attribute_assigner.rb
challah-0.5.1 vendor/bundle/gems/factory_girl-2.6.1/lib/factory_girl/attribute_assigner.rb
factory_girl-2.6.1 lib/factory_girl/attribute_assigner.rb
challah-0.5.0 vendor/bundle/gems/factory_girl-2.5.1/lib/factory_girl/attribute_assigner.rb
factory_girl-2.6.0 lib/factory_girl/attribute_assigner.rb
factory_girl-2.5.2 lib/factory_girl/attribute_assigner.rb
challah-0.4.1 vendor/bundle/gems/factory_girl-2.5.1/lib/factory_girl/attribute_assigner.rb