Class: CouchResource::Errors
- Inherits:
-
Object
- Object
- CouchResource::Errors
- Includes:
- Enumerable
- Defined in:
- lib/couch_resource/validations.rb
Overview
CouchResource::Errors is the completely same as ActiveRecord::Errors
Constant Summary collapse
- @@default_error_messages =
{ :inclusion => "is not included in the list", :exclusion => "is reserved", :invalid => "is invalid", :confirmation => "doesn't match confirmation", :accepted => "must be accepted", :empty => "can't be empty", :blank => "can't be blank", :too_long => "is too long (maximum is %d characters)", :too_short => "is too short (minimum is %d characters)", :wrong_length => "is the wrong length (should be %d characters)", :taken => "has already been taken", :not_a_number => "is not a number", :greater_than => "must be greater than %d", :greater_than_or_equal_to => "must be greater than or equal to %d", :equal_to => "must be equal to %d", :less_than => "must be less than %d", :less_than_or_equal_to => "must be less than or equal to %d", :odd => "must be odd", :even => "must be even", :children => "is not valid" # append for object or array }
Instance Method Summary collapse
- #add(attribute, msg = @@default_error_messages[:invalid]) ⇒ Object
- #add_on_blank(attributes, msg = ) ⇒ Object
- #add_on_empty(attributes, msg = ) ⇒ Object
- #add_to_base(msg) ⇒ Object
- #clear ⇒ Object
- #each ⇒ Object
- #each_full ⇒ Object
- #empty? ⇒ Boolean
- #full_messages ⇒ Object
-
#initialize(base) ⇒ Errors
constructor
:nodoc:.
- #invalid?(attribute) ⇒ Boolean
- #on(attribute) ⇒ Object (also: #[])
- #on_base ⇒ Object
- #size ⇒ Object (also: #count, #length)
- #to_hash ⇒ Object
- #to_json ⇒ Object
- #to_xml(options = {}) ⇒ Object
Constructor Details
#initialize(base) ⇒ Errors
:nodoc:
24 25 26 |
# File 'lib/couch_resource/validations.rb', line 24 def initialize(base) # :nodoc: @base, @errors = base, {} end |
Instance Method Details
#add(attribute, msg = @@default_error_messages[:invalid]) ⇒ Object
57 58 59 60 |
# File 'lib/couch_resource/validations.rb', line 57 def add(attribute, msg = [:invalid]) @errors[attribute.to_s] = [] if @errors[attribute.to_s].nil? @errors[attribute.to_s] << msg end |
#add_on_blank(attributes, msg = ) ⇒ Object
70 71 72 73 74 75 |
# File 'lib/couch_resource/validations.rb', line 70 def add_on_blank(attributes, msg = [:blank]) for attr in [attributes].flatten value = @base.respond_to?(attr.to_s) ? @base.send(attr.to_s) : @base[attr.to_s] add(attr, msg) if value.blank? end end |
#add_on_empty(attributes, msg = ) ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/couch_resource/validations.rb', line 62 def add_on_empty(attributes, msg = [:empty]) for attr in [attributes].flatten value = @base.respond_to?(attr.to_s) ? @base.send(attr.to_s) : @base[attr.to_s] is_empty = value.respond_to?("empty?") ? value.empty? : false add(attr, msg) unless !value.nil? && !is_empty end end |
#add_to_base(msg) ⇒ Object
53 54 55 |
# File 'lib/couch_resource/validations.rb', line 53 def add_to_base(msg) add(:base, msg) end |
#clear ⇒ Object
121 122 123 |
# File 'lib/couch_resource/validations.rb', line 121 def clear @errors = {} end |
#each ⇒ Object
93 94 95 |
# File 'lib/couch_resource/validations.rb', line 93 def each @errors.each_key { |attr| @errors[attr].each { |msg| yield attr, msg } } end |
#each_full ⇒ Object
97 98 99 |
# File 'lib/couch_resource/validations.rb', line 97 def each_full .each { |msg| yield msg } end |
#empty? ⇒ Boolean
117 118 119 |
# File 'lib/couch_resource/validations.rb', line 117 def empty? @errors.empty? end |
#full_messages ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/couch_resource/validations.rb', line 101 def = [] @errors.each_key do |attr| @errors[attr].each do |msg| next if msg.nil? if attr == "base" << msg else << @base.class.human_attribute_name(attr) + " " + msg end end end end |
#invalid?(attribute) ⇒ Boolean
77 78 79 |
# File 'lib/couch_resource/validations.rb', line 77 def invalid?(attribute) !@errors[attribute.to_s].nil? end |
#on(attribute) ⇒ Object Also known as: []
81 82 83 84 85 |
# File 'lib/couch_resource/validations.rb', line 81 def on(attribute) errors = @errors[attribute.to_s] return nil if errors.nil? errors.size == 1 ? errors.first : errors end |
#on_base ⇒ Object
89 90 91 |
# File 'lib/couch_resource/validations.rb', line 89 def on_base on(:base) end |
#size ⇒ Object Also known as: count, length
125 126 127 |
# File 'lib/couch_resource/validations.rb', line 125 def size @errors.values.inject(0) { |error_count, attribute| error_count + attribute.size } end |
#to_hash ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/couch_resource/validations.rb', line 148 def to_hash array = [] @errors.each_key do |attr| @errors[attr].each do |msg| next if msg.nil? if attr == "base" array << { :message => msg } else array << { :message => @base.class.human_attribute_name(attr) + " " + msg, :attr => attr } end end end { :errors => array } end |
#to_json ⇒ Object
144 145 146 |
# File 'lib/couch_resource/validations.rb', line 144 def to_json self.to_hash.to_json end |
#to_xml(options = {}) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/couch_resource/validations.rb', line 132 def to_xml(={}) [:root] ||= "errors" [:indent] ||= 2 [:builder] ||= Builder::XmlMarkup.new(:indent => [:indent]) [:builder].instruct! unless .delete(:skip_instruct) [:builder].errors do |e| .each { |msg| e.error(msg) } end end |