Sha256: 53a7924528b877c2f30fd744b312ebe54acb11ee23c3fac6bfe37b1e799b1578

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 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

1 entries across 1 versions & 1 rubygems

Version Path
ph_model-1.2.0 lib/active_model/validations/collection_items_validator/inner_validator_builder.rb