lib/aequitas/message_transformer.rb in aequitas-0.0.1 vs lib/aequitas/message_transformer.rb in aequitas-0.0.2
- old
+ new
@@ -1,17 +1,25 @@
# -*- encoding: utf-8 -*-
module Aequitas
+
# Transforms Violations to error message strings.
#
# @abstract
# Subclass and override {#transform} to implement a custom message
- # transformer. Use {Violation.message_transformer=} to set a new
+ # transformer. Use {Violation.default_transformer=} to set a new default
# message transformer or pass the transformer to {Violation#message}.
class MessageTransformer
+ # Get the default MessageTransformer for this process
+ #
+ # @return [MessageTransformer]
+ # default MessageTransformer for this process
+ #
+ # @see {Violation.default_transformer}
+ #
def self.default
- defined?(I18n) ? DefaultI18n.new : Default.new
+ defined?(::I18n) ? DefaultI18n.new : DefaultStatic.new
end
# Transforms the specified Violation to an error message string.
#
# @param [Violation] violation
@@ -24,11 +32,11 @@
# +violation+ is +nil+.
def transform(violation)
raise NotImplementedError, "#{self.class}#transform has not been implemented"
end
- class Default < self
+ class DefaultStatic < MessageTransformer
@error_messages = {
:nil => '%s must not be nil',
:blank => '%s must not be blank',
:not_nil => '%s must be nil',
:not_blank => '%s must be blank',
@@ -48,11 +56,12 @@
:not_equal_to => '%s must not be equal to %s',
:less_than => '%s must be less than %s',
:less_than_or_equal_to => '%s must be less than or equal to %s',
:value_between => '%s must be between %s and %s',
:not_unique => '%s is already taken',
- :primitive => '%s must be of type %s'
+ :primitive => '%s must be of type %s',
+ :unsatisfied_condition => '%s condition was not satisfied',
}
class << self
# Gets the hash of error messages used to transform violations.
#
@@ -73,44 +82,50 @@
self.error_messages.merge!(error_messages)
end
def self.error_message(violation_type, attribute_name, violation_values)
if message = self.error_messages[violation_type]
- attribute_name = DataMapper::Inflector.humanize(attribute_name)
+ attribute_name = ::DataMapper::Inflector.humanize(attribute_name)
message % [attribute_name, *violation_values].flatten
else
violation_type.to_s
end
end
+ def initialize
+ require 'dm-core'
+ end
+
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
- end # class Default
+ end # class DefaultStatic
- class DefaultI18n < self
+ class DefaultI18n < MessageTransformer
def initialize
require 'i18n'
end
def transform(violation)
raise ArgumentError, "+violation+ must be specified" if violation.nil?
resource = violation.resource
+ # TODO: resource#model and Model#model_name are assumptions from DM
+ # Figure out a more flexible way to lookup error messages in I18n
model_name = resource.model.model_name
attribute_name = violation.attribute_name
# TODO: Include attribute value in Violation; it may have changed by now
# attribute_value = violation.attribute_value
options = {
:model => ::I18n.translate("models.#{model_name}"),
:attribute => ::I18n.translate("attributes.#{model_name}.#{attribute_name}"),
# TODO: Include attribute value in Violation; it may have changed by now
- :value => resource.validation_attribute_value(attribute_name)
+ :value => resource.validation_attribute_value(attribute_name),
}.merge(violation.info)
::I18n.translate("#{i18n_namespace}.#{violation.type}", options)
end