Sha256: a094e2e67a4755c5df648872d9ff8e71ef521b33db0213870ccbb9fb04800109

Contents?: true

Size: 1.11 KB

Versions: 8

Compression:

Stored size: 1.11 KB

Contents

module ActiveModel
  module Validations
    # Builds the inner validators for the collection items validator
    class CollectionItemsValidator::InnerValidatorBuilder
      class << self
        def build(validator_name, options)
          new(validator_name, options).build
        end
      end

      def initialize(validator_name, options)
        @validator_name = validator_name
        @options = options
      end

      def build
        validator_class.new inner_options.merge(attributes: [:base])
      end

      private

      attr_reader :validator_name, :options

      def validator_class
        name = "#{validator_name.to_s.camelize}Validator"

        begin
          name.include?("::") ? name.constantize : ActiveModel::Validations.const_get(name)
        rescue NameError
          raise ArgumentError, "Unknown validator: '#{validator_name}'"
        end
      end

      def inner_options
        case options
        when TrueClass
          {}
        when Hash
          options
        when Range, Array
          { in: options }
        else
          { with: options }
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
ph_model-1.1.3 lib/active_model/validations/collection_items_validator/inner_validator_builder.rb
ph_model-1.1.2 lib/active_model/validations/collection_items_validator/inner_validator_builder.rb
ph_model-1.1.1 lib/active_model/validations/collection_items_validator/inner_validator_builder.rb
ph_model-1.1.0 lib/active_model/validations/collection_items_validator/inner_validator_builder.rb
ph_model-1.0.2 lib/active_model/validations/collection_items_validator/inner_validator_builder.rb
ph_model-1.0.1 lib/active_model/validations/collection_items_validator/inner_validator_builder.rb
ph_model-1.0.0 lib/active_model/validations/collection_items_validator/inner_validator_builder.rb
ph_model-0.0.1 lib/active_model/validations/collection_items_validator/inner_validator_builder.rb