Class: SimpleRecord::SimpleRecord_errors

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_record/errors.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*params) ⇒ SimpleRecord_errors

Returns a new instance of SimpleRecord_errors.



54
55
56
57
# File 'lib/simple_record/errors.rb', line 54

def initialize(*params)
    super(*params)
    @errors={}
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



52
53
54
# File 'lib/simple_record/errors.rb', line 52

def errors
  @errors
end

Instance Method Details

#add(attribute, message, options = {}) ⇒ Object



63
64
65
66
67
68
# File 'lib/simple_record/errors.rb', line 63

def add(attribute, message, options = {})
    # options param note used; just for drop in compatibility with ActiveRecord
    error, message = message, nil if message.is_a?(Error)
    @errors[attribute.to_s] ||= []
    @errors[attribute.to_s] << (error || Error.new(@base, attribute, message, options))
end

#add_to_base(msg) ⇒ Object



59
60
61
# File 'lib/simple_record/errors.rb', line 59

def add_to_base(msg)
    add(:base, msg)
end

#clearObject



83
84
85
# File 'lib/simple_record/errors.rb', line 83

def clear
    @errors.clear
end

#empty?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/simple_record/errors.rb', line 87

def empty?
    @errors.empty?
end

#full_messagesObject



77
78
79
80
81
# File 'lib/simple_record/errors.rb', line 77

def full_messages
    @errors.values.inject([]) do |full_messages, errors|
        full_messages + errors.map { |error| error.full_message }
    end
end

#lengthObject Also known as: count, size



70
71
72
# File 'lib/simple_record/errors.rb', line 70

def length
    return @errors.length
end

#on(attribute) ⇒ Object Also known as: []



91
92
93
94
95
96
# File 'lib/simple_record/errors.rb', line 91

def on(attribute)
    attribute = attribute.to_s
    return nil unless @errors.has_key?(attribute)
    errors = @errors[attribute].map(&:to_s)
    errors.size == 1 ? errors.first : errors
end

#on_baseObject



100
101
102
# File 'lib/simple_record/errors.rb', line 100

def on_base
    on(:base)
end