Sha256: 3748590014b171adb3a20a73d51ae80ea3702f3a83dc26ee430921115bf98bda

Contents?: true

Size: 1.11 KB

Versions: 5

Compression:

Stored size: 1.11 KB

Contents

# frozen_string_literal: true
module Might
  # Build singleton validation class for specified attribute name
  # @example you need a nice validator for a first_name
  #   validator_klass = ValueValidator.build('first_name', presence: true, length: { minimum: 3 })
  #   validator = validator_klass.new('Bo')
  #   validator.valid? #=> false
  #   validator.errors.full_messages #=> ['First name is too short (minimum is 3 characters)']
  #
  module SortValueValidator
    module_function

    # Validates if Parameter is undefined or not
    class DefinedValidator < ActiveModel::EachValidator
      def validate_each(record, attribute, _value)
        record.errors.add(attribute, :undefined_sort_order) if record.undefined?
      end
    end

    def build(definition)
      Class.new do
        include ActiveModel::Validations

        validates(definition.name, 'might/sort_value_validator/defined': true)

        define_method(:undefined?) { definition.undefined? }
        define_method(definition.name) {}

        def self.model_name
          ActiveModel::Name.new(Might, nil, 'Might')
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
might-0.7.6 lib/might/sort_value_validator.rb
might-0.7.5 lib/might/sort_value_validator.rb
might-0.7.4 lib/might/sort_value_validator.rb
might-0.7.3 lib/might/sort_value_validator.rb
might-0.7.2 lib/might/sort_value_validator.rb