Class: CouchResource::Errors

Inherits:
Object
  • Object
show all
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

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 = @@default_error_messages[: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 = @@default_error_messages[: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 = @@default_error_messages[: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

#clearObject



121
122
123
# File 'lib/couch_resource/validations.rb', line 121

def clear
  @errors = {}
end

#eachObject



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_fullObject



97
98
99
# File 'lib/couch_resource/validations.rb', line 97

def each_full
  full_messages.each { |msg| yield msg }
end

#empty?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/couch_resource/validations.rb', line 117

def empty?
  @errors.empty?
end

#full_messagesObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/couch_resource/validations.rb', line 101

def full_messages
  full_messages = []

  @errors.each_key do |attr|
    @errors[attr].each do |msg|
      next if msg.nil?
      if attr == "base"
        full_messages << msg
      else
        full_messages << @base.class.human_attribute_name(attr) + " " + msg
      end
    end
  end
  full_messages
end

#invalid?(attribute) ⇒ Boolean

Returns:

  • (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_baseObject



89
90
91
# File 'lib/couch_resource/validations.rb', line 89

def on_base
  on(:base)
end

#sizeObject 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_hashObject



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_jsonObject



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(options={})
  options[:root]    ||= "errors"
  options[:indent]  ||= 2
  options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent])

  options[:builder].instruct! unless options.delete(:skip_instruct)
  options[:builder].errors do |e|
    
    full_messages.each { |msg| e.error(msg) }
  end
end