Sha256: e5a3cb0453a0b89d78c642ba3a5bab2cd7e8a2812e2c4ab3b69d1046986ef79a
Contents?: true
Size: 1.61 KB
Versions: 2
Compression:
Stored size: 1.61 KB
Contents
module Revenc class Errors include Enumerable def initialize @errors ={} end # add errors, error_on can be a symbol or object instance def add(error_on, message = "Unknown error") # humanize error_on if error_on.is_a?(Symbol) error_on_str = error_on.to_s else error_on_str = underscore(error_on.class.name) end error_on_str = error_on_str.gsub(/\//, '_') error_on_str = error_on_str.gsub(/_/, ' ') error_on_str = error_on_str.gsub(/^revenc/, '').strip #error_on_str = error_on_str.capitalize @errors[error_on_str] ||= [] @errors[error_on_str] << message.to_s end def empty? @errors.empty? end def clear @errors = {} end def each @errors.each_key { |attr| @errors[attr].each { |msg| yield attr, msg } } end def size @errors.values.inject(0) { |error_count, attribute| error_count + attribute.size } end alias_method :count, :size alias_method :length, :size def messages messages = [] @errors.each_key do |attr| @errors[attr].each do |message| next unless message attr_name = attr.to_s messages << attr_name + ' ' + message end end messages end def to_sentences messages.join("\n") end private # from ActiveSupport def underscore(camel_cased_word) camel_cased_word.to_s.gsub(/::/, '/'). gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). gsub(/([a-z\d])([A-Z])/,'\1_\2'). tr("-", "_"). downcase end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
revenc-0.1.3 | lib/revenc/errors.rb |
revenc-0.1.2 | lib/revenc/errors.rb |