Sha256: 88fe95be3e6ebdf5c67b9fa1338e6562c2367ecc7683587d6a2d373b9fbd73c8

Contents?: true

Size: 1.86 KB

Versions: 3

Compression:

Stored size: 1.86 KB

Contents

module FactoryGirl
  class AttributeAssigner
    def initialize(build_class, evaluator)
      @build_class              = build_class
      @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 ||= @build_class.new
    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

3 entries across 3 versions & 1 rubygems

Version Path
factory_girl-2.4.2 lib/factory_girl/attribute_assigner.rb
factory_girl-2.4.1 lib/factory_girl/attribute_assigner.rb
factory_girl-2.4.0 lib/factory_girl/attribute_assigner.rb