simple_record 1.3.11 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -14,34 +14,66 @@ module SimpleRecord
14
14
  end
15
15
 
16
16
 
17
+ class Error
18
+ attr_accessor :base, :attribute, :type, :message, :options
19
+
20
+ def initialize(base, attribute, message, options = {})
21
+ self.base = base
22
+ self.attribute = attribute
23
+ self.message = message
24
+ end
25
+
26
+ def message
27
+ # When type is a string, it means that we do not have to do a lookup, because
28
+ # the user already sent the "final" message.
29
+ generate_message
30
+ end
31
+
32
+ def full_message
33
+ attribute.to_s == 'base' ? message : generate_full_message()
34
+ end
35
+
36
+ alias :to_s :message
37
+
38
+ def generate_message(options = {})
39
+ @message
40
+ end
41
+
42
+ def generate_full_message(options = {})
43
+ "#{attribute.to_s} #{message}"
44
+ end
45
+ end
46
+
17
47
  class SimpleRecord_errors
48
+ attr_reader :errors
49
+
18
50
  def initialize(*params)
19
51
  super(*params)
20
- @errors=[]
21
- end
22
-
23
- def add_to_base(value)
24
- @errors+=[value]
52
+ @errors={}
25
53
  end
26
54
 
27
- def add(attribute, value)
28
- @errors+=["#{attribute.to_s} #{value}"]
55
+ def add_to_base(msg)
56
+ add(:base, msg)
29
57
  end
30
58
 
31
- def count
32
- return length
59
+ def add(attribute, message, options = {})
60
+ # options param note used; just for drop in compatibility with ActiveRecord
61
+ error, message = message, nil if message.is_a?(Error)
62
+ @errors[attribute.to_s] ||= []
63
+ @errors[attribute.to_s] << (error || Error.new(@base, attribute, message, options))
33
64
  end
34
65
 
35
66
  def length
36
67
  return @errors.length
37
68
  end
38
69
 
39
- def size
40
- return length
41
- end
70
+ alias count length
71
+ alias size length
42
72
 
43
73
  def full_messages
44
- return @errors
74
+ @errors.values.inject([]) do |full_messages, errors|
75
+ full_messages + errors.map { |error| error.full_message }
76
+ end
45
77
  end
46
78
 
47
79
  def clear
@@ -51,7 +83,20 @@ module SimpleRecord
51
83
  def empty?
52
84
  @errors.empty?
53
85
  end
86
+
87
+ def on(attribute)
88
+ attribute = attribute.to_s
89
+ return nil unless @errors.has_key?(attribute)
90
+ errors = @errors[attribute].map(&:to_s)
91
+ errors.size == 1 ? errors.first : errors
92
+ end
93
+
94
+ alias :[] :on
95
+
96
+ def on_base
97
+ on(:base)
98
+ end
54
99
  end
55
-
100
+
56
101
  end
57
102
 
data/lib/simple_record.rb CHANGED
@@ -297,6 +297,15 @@ module SimpleRecord
297
297
  set(:updated, Time.now)
298
298
  end
299
299
 
300
+ # an aliased method since many people use created_at/updated_at naming convention
301
+ def created_at
302
+ self.created
303
+ end
304
+
305
+ # an aliased method since many people use created_at/updated_at naming convention
306
+ def updated_at
307
+ self.updated
308
+ end
300
309
 
301
310
  def cache_store
302
311
  @@cache_store
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_record
3
3
  version: !ruby/object:Gem::Version
4
- hash: 13
4
+ hash: 7
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
- - 3
9
- - 11
10
- version: 1.3.11
8
+ - 4
9
+ - 0
10
+ version: 1.4.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Travis Reeder
@@ -17,7 +17,7 @@ autorequire:
17
17
  bindir: bin
18
18
  cert_chain: []
19
19
 
20
- date: 2010-06-25 00:00:00 -07:00
20
+ date: 2010-07-05 00:00:00 -07:00
21
21
  default_executable:
22
22
  dependencies:
23
23
  - !ruby/object:Gem::Dependency