Class: SteelWheel::Handler::Validator

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/steel_wheel/handler.rb

Overview

Runs validation on objects

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#http_statusObject

Returns the value of attribute http_status.



25
26
27
# File 'lib/steel_wheel/handler.rb', line 25

def http_status
  @http_status
end

Class Method Details

.run(flow) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/steel_wheel/handler.rb', line 27

def self.run(flow)
  validator = new
  [flow.params, flow.query, flow.command].each do |obj|
    break if validator.errors.any?

    validator.validate(obj)
  end
  flow.status = validator.http_status || :ok
  flow.errors.merge!(validator.errors)
end

Instance Method Details

#validate(object) ⇒ Object



38
39
40
41
42
43
# File 'lib/steel_wheel/handler.rb', line 38

def validate(object)
  return if object.valid?

  self.http_status ||= object.http_status
  errors.merge!(object.errors) if errors.empty?
end