Class: ActA::Validator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ Validator

Returns a new instance of Validator.



27
28
29
# File 'lib/act_a.rb', line 27

def initialize(klass)
  self.klass = klass
end

Instance Attribute Details

#keysObject

Returns the value of attribute keys.



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

def keys
  @keys
end

#klassObject

Returns the value of attribute klass.



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

def klass
  @klass
end

#recordObject

Returns the value of attribute record.



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

def record
  @record
end

Instance Method Details

#apply(key_and_values = {}) ⇒ Object



31
32
33
34
35
# File 'lib/act_a.rb', line 31

def apply(key_and_values = {})
  self.keys = key_and_values.keys
  self.record = klass.new(key_and_values)
  self
end

#errorsObject



37
38
39
# File 'lib/act_a.rb', line 37

def errors
  record.errors
end

#messagesObject



41
42
43
# File 'lib/act_a.rb', line 41

def messages
  record.errors.try(:messages)
end

#valid?Boolean

Returns:

  • (Boolean)


45
46
47
48
# File 'lib/act_a.rb', line 45

def valid?
  validate
  errors.empty?
end

#valid_brutally?Boolean

Returns:

  • (Boolean)


50
51
52
53
# File 'lib/act_a.rb', line 50

def valid_brutally?
  validate_brutally
  errors.empty?
end

#validateObject



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/act_a.rb', line 55

def validate
  keys.each do |attribute_name|
    validators(attribute_name).each do |validator|
      duplicate_validator(validator, attribute_name).validate(record)
    end
  end
rescue
  # do nothing
ensure
  reject_not_target
  return self
end

#validate!Object



68
69
70
71
72
73
74
# File 'lib/act_a.rb', line 68

def validate!
  if valid?
    self
  else
    raise ActiveRecord::RecordInvalid, record
  end
end

#validate_brutallyObject



76
77
78
79
80
81
82
83
# File 'lib/act_a.rb', line 76

def validate_brutally
  record.valid?
rescue
  # do nothing
ensure
  reject_not_target
  return self
end

#validate_brutally!Object



85
86
87
88
89
90
91
# File 'lib/act_a.rb', line 85

def validate_brutally!
  if valid_brutally?
    self
  else
    raise ActiveRecord::RecordInvalid, record
  end
end