module YaKassa::V3::Concerns::Validatable

Constants

VALIDATORS

Public Class Methods

included(klass) click to toggle source
# File lib/ya_kassa/v3/concerns/validatable.rb, line 30
def self.included(klass)
  klass.extend(ClassMethods)
end

Public Instance Methods

errors() click to toggle source
# File lib/ya_kassa/v3/concerns/validatable.rb, line 45
def errors
  @errors.select { |k, v| v.any? }
end
valid?() click to toggle source
# File lib/ya_kassa/v3/concerns/validatable.rb, line 34
def valid?
  @errors = {}
  valid_arr = validators.map do |opts|
    validator = create_validator(opts)
    validator.validate
    collect_errors(validator)
    validator.valid?
  end
  valid_arr.select { |v| v == false }.empty?
end

Private Instance Methods

attr_value(attr_name) click to toggle source
# File lib/ya_kassa/v3/concerns/validatable.rb, line 67
def attr_value(attr_name)
  self.public_send(attr_name)
end
collect_errors(validator) click to toggle source
# File lib/ya_kassa/v3/concerns/validatable.rb, line 71
def collect_errors(validator)
  @errors[validator.name] ||= []
  @errors[validator.name] << validator.error
end
create_validator(opts) click to toggle source
# File lib/ya_kassa/v3/concerns/validatable.rb, line 51
def create_validator(opts)
  validator = validator_class(opts[:type]).new(
    opts[:name],
    attr_value(opts[:name]),
    opts[:params]
  )
end
validator_class(type) click to toggle source
# File lib/ya_kassa/v3/concerns/validatable.rb, line 76
def validator_class(type)
  klass = VALIDATORS[type]
  Utilites::String.constantize("::YaKassa::V3::Validators::#{klass}")
end
validators() click to toggle source
# File lib/ya_kassa/v3/concerns/validatable.rb, line 59
def validators
  begin
    self.class.class_variable_get(:@@validators)
  rescue NameError
    []
  end
end