Sha256: fa3bd97c4d189eb7ac060bac1ec3f5021f57d733214d0c11a688ea640069c78f

Contents?: true

Size: 1.67 KB

Versions: 5

Compression:

Stored size: 1.67 KB

Contents

module UnitTests
  module CreateModelArguments
    class UniquenessMatcher < Basic
      def self.normalize_attribute(attribute)
        if attribute.is_a?(Hash)
          Attribute.new(attribute)
        else
          Attribute.new(name: attribute)
        end
      end

      def self.normalize_attributes(attributes)
        attributes.map do |attribute|
          normalize_attribute(attribute)
        end
      end

      def columns
        attributes.reduce({}) do |options, attribute|
          options.merge(
            attribute.name => {
              type: attribute.column_type,
              options: attribute.column_options
            }
          )
        end
      end

      def validation_options
        super.merge(scope: scope_attribute_names)
      end

      def attribute_default_values_by_name
        attributes.reduce({}) do |values, attribute|
          values.merge(attribute.name => attribute.default_value)
        end
      end

      protected

      def attribute_class
        Attribute
      end

      private

      def attributes
        [attribute] + scope_attributes + additional_attributes
      end

      def scope_attribute_names
        scope_attributes.map(&:name)
      end

      def scope_attributes
        @_scope_attributes ||= self.class.normalize_attributes(
          args.fetch(:scopes, [])
        )
      end

      def additional_attributes
        @_additional_attributes ||= self.class.normalize_attributes(
          args.fetch(:additional_attributes, [])
        )
      end

      class Attribute < UnitTests::Attribute
        def value_type
          args.fetch(:value_type) { column_type }
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
shoulda-matchers-3.1.3 spec/support/unit/create_model_arguments/uniqueness_matcher.rb
shoulda-matchers-4.0.0.rc1 spec/support/unit/create_model_arguments/uniqueness_matcher.rb
shoulda-matchers-3.1.2 spec/support/unit/create_model_arguments/uniqueness_matcher.rb
shoulda-matchers-3.1.1 spec/support/unit/create_model_arguments/uniqueness_matcher.rb
shoulda-matchers-3.1.0 spec/support/unit/create_model_arguments/uniqueness_matcher.rb