Module: Might::SortValueValidator

Defined in:
lib/might/sort_value_validator.rb

Overview

Build singleton validation class for specified attribute name

Examples:

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)']

Defined Under Namespace

Classes: DefinedValidator

Class Method Summary collapse

Class Method Details

.build(definition) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/might/sort_value_validator.rb', line 20

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