Sha256: 0fd34d1ea92ff943541c2e850a8f657ca316234e7a98ec99cb504ddabbe950f9

Contents?: true

Size: 1.26 KB

Versions: 6

Compression:

Stored size: 1.26 KB

Contents

require 'factory_bot/attribute/static'
require 'factory_bot/attribute/dynamic'
require 'factory_bot/attribute/association'
require 'factory_bot/attribute/sequence'

module FactoryBot
  # @api private
  class Attribute
    attr_reader :name, :ignored

    def initialize(name, ignored)
      @name = name.to_sym
      @ignored = ignored
      ensure_non_attribute_writer!
    end

    def to_proc
      -> { }
    end

    def association?
      false
    end

    def alias_for?(attr)
      FactoryBot.aliases_for(attr).include?(name)
    end

    private

    def ensure_non_attribute_writer!
      NonAttributeWriterValidator.new(@name).validate!
    end

    class NonAttributeWriterValidator
      def initialize(method_name)
        @method_name = method_name.to_s
        @method_name_setter_match = @method_name.match(/(.*)=$/)
      end

      def validate!
        if method_is_writer?
          raise AttributeDefinitionError, error_message
        end
      end

      private

      def method_is_writer?
        !!@method_name_setter_match
      end

      def attribute_name
        @method_name_setter_match[1]
      end

      def error_message
        "factory_bot uses '#{attribute_name} value' syntax rather than '#{attribute_name} = value'"
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
factory_bot-4.11.1 lib/factory_bot/attribute.rb
factory_bot-4.11.0 lib/factory_bot/attribute.rb
factory_bot-4.10.0 lib/factory_bot/attribute.rb
factory_bot-4.8.2 lib/factory_bot/attribute.rb
factory_bot-1.0.1.alpha lib/factory_bot/attribute.rb
factory_bot-1.0.0.alpha lib/factory_bot/attribute.rb