Class: Kangaru::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/kangaru/validator.rb

Direct Known Subclasses

Kangaru::Validators::RequiredValidator

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model:, attribute:, **params) ⇒ Validator

Returns a new instance of Validator.



5
6
7
8
9
10
# File 'lib/kangaru/validator.rb', line 5

def initialize(model:, attribute:, **params)
  @model = model
  @attribute = attribute
  @params = params
  @value = model.public_send(attribute)
end

Instance Attribute Details

#attributeObject (readonly)

Returns the value of attribute attribute.



3
4
5
# File 'lib/kangaru/validator.rb', line 3

def attribute
  @attribute
end

#modelObject (readonly)

Returns the value of attribute model.



3
4
5
# File 'lib/kangaru/validator.rb', line 3

def model
  @model
end

#paramsObject (readonly)

Returns the value of attribute params.



3
4
5
# File 'lib/kangaru/validator.rb', line 3

def params
  @params
end

#valueObject (readonly)

Returns the value of attribute value.



3
4
5
# File 'lib/kangaru/validator.rb', line 3

def value
  @value
end

Instance Method Details

#add_error!(message) ⇒ Object



16
17
18
# File 'lib/kangaru/validator.rb', line 16

def add_error!(message)
  model.errors << Validation::Error.new(attribute:, message:)
end

#validateObject

Raises:

  • (NotImplementedError)


12
13
14
# File 'lib/kangaru/validator.rb', line 12

def validate
  raise NotImplementedError
end