lib/ratatouille/ratifier.rb in ratatouille-1.0.0 vs lib/ratatouille/ratifier.rb in ratatouille-1.2.0
- old
+ new
@@ -5,13 +5,15 @@
attr_reader :errors
attr_reader :ratifiable_object
# A new instance of Ratifier
# @param [Hash, Array] obj Object to validate
+ # @param [Hash] options
def initialize(obj, options={}, &block)
@errors = { "/" => [] }
@ratifiable_object = obj
+ self.name = options[:name]
case obj
when Hash then extend Ratatouille::HashMethods
when Array then extend Ratatouille::ArrayMethods
end
@@ -22,23 +24,44 @@
@errors.freeze
end#initialize
+ # Name of instance
+ #
+ # @return [String]
+ def name
+ @name ||= @ratifiable_object.class.to_s
+ end
+
+
+ # Set name of instance
+ #
+ # @param [String] namein
+ # @return [String] name of Ratatouille::Ratifier instance
+ def name=(namein)
+ case namein
+ when String
+ @name = namein unless namein.blank?
+ end
+ @name
+ end
+
+
# Add validation error. Useful for custom validations.
# @param [String] str
# @param [String] context
# @return [void]
def validation_error(err_in, context="/")
case err_in
when String
return if err_in.blank?
@errors[context] = [] unless @errors[context]
- @errors[context] << err_in
+ @errors[context] << "#{@name}: #{err_in}"
end
rescue Exception => e
- @errors["/"] << e.message
+ @errors["/"] << "#{@name}: #{e.message}"
end#validation_error
# Does the object pass all validation logic?
#
@@ -52,10 +75,10 @@
#
# @return [void]
def cleanup_errors
@errors = {} if errors_array.empty?
rescue Exception => e
- @errors["/"] << e.message
+ validation_error("#{e.message}", '/')
end#cleanup_errors
# @param [Hash] hsh Hash to act upon.
# @return [Array]