Module: ChipmunkValidatable::ClassMethods

Defined in:
lib/chipmunk_validatable.rb

Instance Method Summary collapse

Instance Method Details

#validates(_description = "", only_if: -> { true }, precondition: -> {}, condition:, error:) ⇒ Object


14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/chipmunk_validatable.rb', line 14

def validates(_description = "", only_if: -> { true }, precondition: -> {}, condition:, error:)
  validators << lambda do
    return true unless instance_exec(&only_if)

    precond_result = instance_exec(&precondition)
    if instance_exec(*precond_result, &condition)
      true
    else
      errors << instance_exec(*precond_result, &error)
      false
    end
  end
end

#validatorsObject


10
11
12
# File 'lib/chipmunk_validatable.rb', line 10

def validators
  @validators ||= []
end