Class: Aequitas::MessageTransformer::DefaultStatic

Inherits:
Aequitas::MessageTransformer show all
Defined in:
lib/aequitas/message_transformer.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Aequitas::MessageTransformer

default

Constructor Details

#initializeDefaultStatic

Returns a new instance of DefaultStatic.



94
95
96
# File 'lib/aequitas/message_transformer.rb', line 94

def initialize
  require 'dm-core'
end

Class Attribute Details

.error_messagesHash{Symbol=>String}

Gets the hash of error messages used to transform violations.

Returns:

  • (Hash{Symbol=>String})


69
70
71
# File 'lib/aequitas/message_transformer.rb', line 69

def error_messages
  @error_messages
end

Class Method Details

.error_message(violation_type, attribute_name, violation_values) ⇒ Object



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

def self.error_message(violation_type, attribute_name, violation_values)
  if message = self.error_messages[violation_type]
    attribute_name = ::DataMapper::Inflector.humanize(attribute_name)
    message % [attribute_name, *violation_values].flatten
  else
    violation_type.to_s
  end
end

Instance Method Details

#transform(violation) ⇒ Object

Raises:

  • (ArgumentError)


98
99
100
101
102
103
104
# File 'lib/aequitas/message_transformer.rb', line 98

def transform(violation)
  raise ArgumentError, "+violation+ must be specified" if violation.nil?

  attribute_name = violation.attribute_name

  self.class.error_message(violation.type, attribute_name, violation.values)
end