Class: ActiveModel::BetterErrors::ErrorMessage

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/active_model/better_errors/error_message.rb

Overview

ErrorMessage

Constant Summary collapse

CALLBACKS_OPTIONS =
[
  :if, :unless, :on, :allow_nil, :allow_blank, :strict
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, attribute, type, message, options = {}) ⇒ ErrorMessage

Returns a new instance of ErrorMessage.



56
57
58
59
60
61
62
# File 'lib/active_model/better_errors/error_message.rb', line 56

def initialize(base, attribute, type, message, options = {})
  @base       = base
  @attribute  = attribute
  @type       = type
  @message    = message
  @options    = options
end

Instance Attribute Details

#attributeObject (readonly)

Returns the value of attribute attribute.



54
55
56
# File 'lib/active_model/better_errors/error_message.rb', line 54

def attribute
  @attribute
end

#baseObject (readonly)

Returns the value of attribute base.



54
55
56
# File 'lib/active_model/better_errors/error_message.rb', line 54

def base
  @base
end

#messageObject (readonly)

Returns the value of attribute message.



54
55
56
# File 'lib/active_model/better_errors/error_message.rb', line 54

def message
  @message
end

#optionsObject (readonly)

Returns the value of attribute options.



54
55
56
# File 'lib/active_model/better_errors/error_message.rb', line 54

def options
  @options
end

#typeObject (readonly)

Returns the value of attribute type.



54
55
56
# File 'lib/active_model/better_errors/error_message.rb', line 54

def type
  @type
end

Class Method Details

.build(base, attribute, message, options = nil) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/active_model/better_errors/error_message.rb', line 45

def self.build(base, attribute, message, options = nil)
  options   = options ? options : {}
  options   = options.except(*CALLBACKS_OPTIONS)

  symbol, string = identify message, options.delete(:message)

  new(base, attribute, symbol, string, options)
end

.identify(message, override) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/active_model/better_errors/error_message.rb', line 28

def self.identify(message, override)
  symbol = string = nil

  message   = normalize message
  override  = normalize override

  symbol = message if message.is_a?(Symbol)
  symbol = override if override.is_a?(Symbol)

  string = message if message.is_a?(String)
  string = override if override.is_a?(String)

  string = nil if string.blank?

  [symbol, string]
end

.normalize(message) ⇒ Object

return the message either as nil, symbol, or string



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

def self.normalize(message)
  case message
  when nil
    nil
  when Symbol
    message
  when Proc
    message.call
  else
    message.to_s
  end
end

Instance Method Details

#<=>(other) ⇒ Object



64
65
66
# File 'lib/active_model/better_errors/error_message.rb', line 64

def <=>(other)
  to_hash <=> other.to_hash
end

#==(other) ⇒ Object



93
94
95
96
# File 'lib/active_model/better_errors/error_message.rb', line 93

def ==(other)
  return type == other if other.is_a?(Symbol)
  to_s == other.to_s
end

#as_json(*json_args) ⇒ Object



77
78
79
# File 'lib/active_model/better_errors/error_message.rb', line 77

def as_json(*json_args)
  to_hash
end

#hashObject



81
82
83
# File 'lib/active_model/better_errors/error_message.rb', line 81

def hash
  to_hash.hash
end

#inspectObject



89
90
91
# File 'lib/active_model/better_errors/error_message.rb', line 89

def inspect
  to_s.inspect
end

#to_hashObject



68
69
70
71
72
73
74
75
# File 'lib/active_model/better_errors/error_message.rb', line 68

def to_hash
  {
    attribute: attribute,
    type:      type,
    message:   message,
    options:   options
  }
end

#to_sObject



85
86
87
# File 'lib/active_model/better_errors/error_message.rb', line 85

def to_s
  ::ActiveModel::BetterErrors.format_message(base, self)
end