Class: ActiveModel::ErrorCollecting::ErrorMessage

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

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.



49
50
51
52
53
54
55
# File 'lib/active_model/error_collecting/error_message.rb', line 49

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.



47
48
49
# File 'lib/active_model/error_collecting/error_message.rb', line 47

def attribute
  @attribute
end

#baseObject (readonly)

Returns the value of attribute base.



47
48
49
# File 'lib/active_model/error_collecting/error_message.rb', line 47

def base
  @base
end

#messageObject (readonly)

Returns the value of attribute message.



47
48
49
# File 'lib/active_model/error_collecting/error_message.rb', line 47

def message
  @message
end

#optionsObject (readonly)

Returns the value of attribute options.



47
48
49
# File 'lib/active_model/error_collecting/error_message.rb', line 47

def options
  @options
end

#typeObject (readonly)

Returns the value of attribute type.



47
48
49
# File 'lib/active_model/error_collecting/error_message.rb', line 47

def type
  @type
end

Class Method Details

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



38
39
40
41
42
43
44
45
# File 'lib/active_model/error_collecting/error_message.rb', line 38

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



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/active_model/error_collecting/error_message.rb', line 21

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



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/active_model/error_collecting/error_message.rb', line 8

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



57
58
59
# File 'lib/active_model/error_collecting/error_message.rb', line 57

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

#==(other) ⇒ Object



82
83
84
85
# File 'lib/active_model/error_collecting/error_message.rb', line 82

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

#hashObject



70
71
72
# File 'lib/active_model/error_collecting/error_message.rb', line 70

def hash
  to_hash.hash
end

#inspectObject



78
79
80
# File 'lib/active_model/error_collecting/error_message.rb', line 78

def inspect
  to_s.inspect
end

#to_hashObject



61
62
63
64
65
66
67
68
# File 'lib/active_model/error_collecting/error_message.rb', line 61

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

#to_sObject



74
75
76
# File 'lib/active_model/error_collecting/error_message.rb', line 74

def to_s
  HumanMessageFormatter.new(base, self).format_message
end