csv_record 1.8.0 → 1.9.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +38 -2
- data/csv_record.gemspec +2 -0
- data/lib/csv_record/associations.rb +12 -2
- data/lib/csv_record/callbacks.rb +17 -0
- data/lib/csv_record/helpers.rb +0 -6
- data/lib/csv_record/reader.rb +17 -17
- data/lib/csv_record/validations.rb +13 -6
- data/lib/csv_record/version.rb +1 -1
- data/test/csv_record/associations_test.rb +22 -7
- data/test/csv_record/callbacks_test.rb +25 -2
- data/test/csv_record/reader_test.rb +1 -2
- data/test/csv_record/validation_test.rb +42 -25
- data/test/models/callback_test_class.rb +15 -0
- data/test/models/custom_errors_class.rb +13 -0
- data/test/models/jedi.rb +11 -8
- data/test/models/padawan.rb +7 -0
- data/test/test_helper.rb +7 -0
- metadata +8 -5
- data/test/monkey_patches/array_test.rb +0 -14
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# CsvRecord
|
2
2
|
|
3
|
-
[![Build Status](https://secure.travis-ci.org/lukasalexandre/csv_record.png)](http://travis-ci.org/lukasalexandre/csv_record) [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/lukasalexandre/csv_record)
|
3
|
+
[![Build Status](https://secure.travis-ci.org/lukasalexandre/csv_record.png)](http://travis-ci.org/lukasalexandre/csv_record) [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/lukasalexandre/csv_record) [![Dependency Status](https://gemnasium.com/lukasalexandre/csv_record.png)](https://gemnasium.com/lukasalexandre/csv_record)
|
4
4
|
|
5
5
|
CSV Record connects Ruby classes to CSV documents database to establish an almost zero-configuration persistence layer for applications.
|
6
6
|
|
@@ -126,6 +126,26 @@ car.save
|
|
126
126
|
company.cars # [#<Car:0x007f9b249b24d8>]
|
127
127
|
```
|
128
128
|
|
129
|
+
###Has One
|
130
|
+
The same as has_many but limited to one associated record.
|
131
|
+
|
132
|
+
```ruby
|
133
|
+
class Company
|
134
|
+
include CsvRecord::Document
|
135
|
+
|
136
|
+
attr_accessor :name
|
137
|
+
|
138
|
+
has_one :car
|
139
|
+
end
|
140
|
+
|
141
|
+
company = Company.create :name => 'Chutz'
|
142
|
+
|
143
|
+
car.save
|
144
|
+
company.car = car
|
145
|
+
|
146
|
+
company.car # #<Car:0x007f9b249b24d8>
|
147
|
+
```
|
148
|
+
|
129
149
|
##Callbacks
|
130
150
|
###Overview
|
131
151
|
Callbacks can be used to execute code on predetermined moments.
|
@@ -141,18 +161,30 @@ end
|
|
141
161
|
###Avaiable Callbacks
|
142
162
|
Here is a list with all the available callbacks, listed in the same order in which they will get called during the respective operations:
|
143
163
|
|
164
|
+
####Finding an Object
|
165
|
+
* after_initialize
|
166
|
+
* after_find
|
167
|
+
|
144
168
|
####Creating an Object
|
145
169
|
* after_initialize
|
146
170
|
* before_validation
|
147
171
|
* after_validation
|
172
|
+
* before_save
|
148
173
|
* before_create
|
149
174
|
* after_create
|
175
|
+
* after_save
|
150
176
|
|
151
177
|
####Updating an Object
|
152
178
|
* before_validation
|
153
179
|
* after_validation
|
180
|
+
* before_save
|
154
181
|
* before_update
|
155
182
|
* after_update
|
183
|
+
* after_save
|
184
|
+
|
185
|
+
####Destroying an Object
|
186
|
+
* before_destroy
|
187
|
+
* after_destroy
|
156
188
|
|
157
189
|
##Validations
|
158
190
|
|
@@ -175,8 +207,12 @@ class Company
|
|
175
207
|
|
176
208
|
validate :my_custom_validator_method
|
177
209
|
|
210
|
+
validate do
|
211
|
+
self.errors.add :attribute
|
212
|
+
end
|
213
|
+
|
178
214
|
def my_custom_validator_method
|
179
|
-
|
215
|
+
self.errors.add :attribute
|
180
216
|
end
|
181
217
|
end
|
182
218
|
|
data/csv_record.gemspec
CHANGED
@@ -8,6 +8,8 @@ Gem::Specification.new do |gem|
|
|
8
8
|
gem.summary = %q{CSV Record connects Ruby classes to CSV documents database to establish an almost zero-configuration persistence layer for applications.}
|
9
9
|
gem.homepage = "https://github.com/lukasalexandre/csv_record"
|
10
10
|
|
11
|
+
gem.license = 'MIT'
|
12
|
+
|
11
13
|
gem.files = `git ls-files`.split($\)
|
12
14
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
15
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
@@ -19,11 +19,21 @@ module CsvRecord
|
|
19
19
|
end
|
20
20
|
|
21
21
|
def has_many(klass)
|
22
|
-
|
22
|
+
self.class_eval do
|
23
|
+
define_method klass do
|
24
|
+
klass.to_s.to_class.where :"#{self.underscored_class_name}_id" => self.id
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
23
28
|
|
29
|
+
def has_one(klass)
|
24
30
|
self.class_eval do
|
31
|
+
define_method "#{klass}=" do |obj|
|
32
|
+
obj.send "#{self.underscored_class_name}_id=", self.id
|
33
|
+
obj.save
|
34
|
+
end
|
25
35
|
define_method klass do
|
26
|
-
|
36
|
+
klass.to_s.to_class.where("#{self.underscored_class_name}_id" => self.id).first
|
27
37
|
end
|
28
38
|
end
|
29
39
|
end
|
data/lib/csv_record/callbacks.rb
CHANGED
@@ -2,10 +2,13 @@ module CsvRecord
|
|
2
2
|
module Callbacks
|
3
3
|
CALLBACKS = [
|
4
4
|
:after_initialize,
|
5
|
+
:after_find,
|
5
6
|
:before_validation,
|
6
7
|
:after_validation,
|
7
8
|
:before_save,
|
8
9
|
:after_save,
|
10
|
+
:after_destroy,
|
11
|
+
:before_destroy,
|
9
12
|
:before_create,
|
10
13
|
:after_create,
|
11
14
|
:before_update,
|
@@ -20,6 +23,12 @@ module CsvRecord
|
|
20
23
|
const_get(const_variable) << block if block
|
21
24
|
end
|
22
25
|
end
|
26
|
+
|
27
|
+
def __where__(*args)
|
28
|
+
results = super
|
29
|
+
results.each &:run_after_find_callbacks
|
30
|
+
results
|
31
|
+
end
|
23
32
|
end
|
24
33
|
|
25
34
|
module InstanceMethods
|
@@ -50,6 +59,13 @@ module CsvRecord
|
|
50
59
|
is_valid
|
51
60
|
end
|
52
61
|
|
62
|
+
def destroy
|
63
|
+
self.run_before_destroy_callbacks
|
64
|
+
is_destroyed = super
|
65
|
+
self.run_after_destroy_callbacks if is_destroyed
|
66
|
+
is_destroyed
|
67
|
+
end
|
68
|
+
|
53
69
|
def save(*args)
|
54
70
|
self.run_before_save_callbacks
|
55
71
|
is_saved = super
|
@@ -67,6 +83,7 @@ module CsvRecord
|
|
67
83
|
def update_registry
|
68
84
|
self.run_before_update_callbacks
|
69
85
|
saved = super
|
86
|
+
self.run_after_destroy_callbacks if saved
|
70
87
|
self.run_after_update_callbacks if saved
|
71
88
|
saved
|
72
89
|
end
|
data/lib/csv_record/helpers.rb
CHANGED
data/lib/csv_record/reader.rb
CHANGED
@@ -46,6 +46,22 @@ module CsvRecord
|
|
46
46
|
end
|
47
47
|
end
|
48
48
|
|
49
|
+
def method_missing(meth, *args, &block)
|
50
|
+
if meth.to_s =~ DYNAMIC_FINDER_PATTERN
|
51
|
+
dynamic_finder $1, *args, &block
|
52
|
+
else
|
53
|
+
super # You *must* call super if you don't handle the
|
54
|
+
# method, otherwise you'll mess up Ruby's method
|
55
|
+
# lookup.
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def respond_to?(meth)
|
60
|
+
(meth.to_s =~ DYNAMIC_FINDER_PATTERN) || super
|
61
|
+
end
|
62
|
+
|
63
|
+
protected
|
64
|
+
|
49
65
|
def search_for(csv, params)
|
50
66
|
conditions = handle_params params
|
51
67
|
csv.entries.select do |attributes|
|
@@ -64,22 +80,6 @@ module CsvRecord
|
|
64
80
|
conditions
|
65
81
|
end
|
66
82
|
|
67
|
-
def method_missing(meth, *args, &block)
|
68
|
-
if meth.to_s =~ DYNAMIC_FINDER_PATTERN
|
69
|
-
dynamic_finder $1, *args, &block
|
70
|
-
else
|
71
|
-
super # You *must* call super if you don't handle the
|
72
|
-
# method, otherwise you'll mess up Ruby's method
|
73
|
-
# lookup.
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
def respond_to?(meth)
|
78
|
-
(meth.to_s =~ DYNAMIC_FINDER_PATTERN) || super
|
79
|
-
end
|
80
|
-
|
81
|
-
protected
|
82
|
-
|
83
83
|
def dynamic_finder(meth, *args, &block)
|
84
84
|
properties = meth.split '_and_'
|
85
85
|
conditions = Hash[properties.zip args]
|
@@ -102,7 +102,7 @@ module CsvRecord
|
|
102
102
|
end
|
103
103
|
|
104
104
|
def __to_param__
|
105
|
-
self.id
|
105
|
+
self.id.to_s
|
106
106
|
end
|
107
107
|
|
108
108
|
def ==(obj)
|
@@ -14,12 +14,12 @@ module CsvRecord
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def custom_validators
|
17
|
-
@custom_validators
|
17
|
+
@custom_validators ||= []
|
18
18
|
end
|
19
19
|
|
20
20
|
def validate(*args, &block)
|
21
21
|
@custom_validators ||= []
|
22
|
-
|
22
|
+
@custom_validators += args
|
23
23
|
@custom_validators << block if block_given?
|
24
24
|
end
|
25
25
|
end
|
@@ -37,7 +37,14 @@ module CsvRecord
|
|
37
37
|
end
|
38
38
|
|
39
39
|
def errors
|
40
|
-
@errors
|
40
|
+
unless @errors
|
41
|
+
@errors = []
|
42
|
+
def @errors.add(attribute)
|
43
|
+
self << attribute
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
@errors
|
41
48
|
end
|
42
49
|
|
43
50
|
alias :valid? :__valid__?
|
@@ -46,7 +53,7 @@ module CsvRecord
|
|
46
53
|
def trigger_presence_validations
|
47
54
|
self.class.fields_to_validate_presence.each do |attribute|
|
48
55
|
if self.public_send(attribute).nil?
|
49
|
-
|
56
|
+
self.errors.add attribute
|
50
57
|
end
|
51
58
|
end
|
52
59
|
end
|
@@ -57,7 +64,7 @@ module CsvRecord
|
|
57
64
|
condition[attribute] = self.public_send attribute
|
58
65
|
records = self.class.__where__ condition
|
59
66
|
if records.any? { |record| record != self }
|
60
|
-
|
67
|
+
self.errors.add attribute
|
61
68
|
end
|
62
69
|
end
|
63
70
|
end
|
@@ -67,7 +74,7 @@ module CsvRecord
|
|
67
74
|
if not validator.is_a? Proc
|
68
75
|
self.send validator
|
69
76
|
else
|
70
|
-
|
77
|
+
self.instance_eval &validator
|
71
78
|
end
|
72
79
|
end
|
73
80
|
end
|
data/lib/csv_record/version.rb
CHANGED
@@ -1,19 +1,21 @@
|
|
1
1
|
require_relative '../test_helper'
|
2
2
|
require_relative '../models/jedi'
|
3
3
|
require_relative '../models/jedi_order'
|
4
|
+
require_relative '../models/padawan'
|
4
5
|
|
5
6
|
describe CsvRecord::Associations do
|
6
|
-
describe '
|
7
|
+
describe 'belongs_to' do
|
7
8
|
it ('responds to belongs_to') { Jedi.must_respond_to :belongs_to }
|
9
|
+
|
8
10
|
it ('responds to jedi_order') { Jedi.new.must_respond_to :jedi_order }
|
9
11
|
it ('responds to jedi_order=') { Jedi.new.must_respond_to :jedi_order= }
|
10
12
|
it ('responds to jedi_order_id') { Jedi.new.must_respond_to :jedi_order_id }
|
11
|
-
it ('responds to has_many') { JediOrder.must_respond_to :has_many }
|
12
|
-
end
|
13
13
|
|
14
|
-
|
15
|
-
it 'checking to param extraction' do
|
14
|
+
before do
|
16
15
|
jedi_council.save
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'checking to param extraction' do
|
17
19
|
luke.save
|
18
20
|
luke.jedi_order = jedi_council
|
19
21
|
luke.jedi_order_id.wont_be_nil
|
@@ -22,7 +24,6 @@ describe CsvRecord::Associations do
|
|
22
24
|
end
|
23
25
|
|
24
26
|
it 'has a single jedi order associated' do
|
25
|
-
jedi_council.save
|
26
27
|
luke.jedi_order = jedi_council
|
27
28
|
luke.save.must_equal true
|
28
29
|
first_jedi = Jedi.first
|
@@ -32,7 +33,9 @@ describe CsvRecord::Associations do
|
|
32
33
|
end
|
33
34
|
end
|
34
35
|
|
35
|
-
describe 'has_many
|
36
|
+
describe 'has_many' do
|
37
|
+
it ('responds to has_many') { JediOrder.must_respond_to :has_many }
|
38
|
+
|
36
39
|
it 'has many jedis associated' do
|
37
40
|
jedi_council.save
|
38
41
|
yoda.jedi_order = jedi_council
|
@@ -44,4 +47,16 @@ describe CsvRecord::Associations do
|
|
44
47
|
jedis.first.must_be_instance_of Jedi
|
45
48
|
end
|
46
49
|
end
|
50
|
+
|
51
|
+
describe 'has_one' do
|
52
|
+
it ('responds to has_one') { Jedi.must_respond_to :has_one }
|
53
|
+
|
54
|
+
it 'has_one padawan' do
|
55
|
+
qui_gon_jinn.save
|
56
|
+
obi_wan_kenobi_padawan.save
|
57
|
+
qui_gon_jinn.padawan = obi_wan_kenobi_padawan
|
58
|
+
qui_gon_jinn.padawan.must_equal obi_wan_kenobi_padawan
|
59
|
+
qui_gon_jinn.padawan.must_be_instance_of Padawan
|
60
|
+
end
|
61
|
+
end
|
47
62
|
end
|
@@ -1,5 +1,4 @@
|
|
1
1
|
require_relative '../test_helper'
|
2
|
-
|
3
2
|
require_relative '../models/car'
|
4
3
|
require_relative '../models/callback_test_class'
|
5
4
|
|
@@ -10,15 +9,20 @@ describe CsvRecord::Callbacks do
|
|
10
9
|
it ('before_create callback') { klass.must_respond_to(:before_create) }
|
11
10
|
it ('after_create callback') { klass.must_respond_to(:after_create) }
|
12
11
|
it ('before_save callback') { klass.must_respond_to(:before_save) }
|
12
|
+
it ('before_destroy callback') {klass.must_respond_to(:before_destroy)}
|
13
13
|
it ('after_save callback') { klass.must_respond_to(:after_save) }
|
14
|
+
it ('before_update callback') { klass.must_respond_to(:before_update) }
|
15
|
+
it ('after_update callback') { klass.must_respond_to(:after_update) }
|
14
16
|
it ('after_initialize callback') { klass.must_respond_to(:after_initialize) }
|
17
|
+
it ('after_destroy callback') { klass.must_respond_to(:after_destroy) }
|
18
|
+
it ('after_find callback') { klass.must_respond_to(:after_find) }
|
15
19
|
end
|
16
20
|
|
17
21
|
describe "Check the run callback definitions" do
|
18
22
|
let (:klass) { CallbackTestClass.new }
|
19
|
-
|
20
23
|
it ('run before_create callbacks') { klass.must_respond_to(:run_before_create_callbacks) }
|
21
24
|
it ('run after_create callbacks') { klass.must_respond_to(:run_after_create_callbacks) }
|
25
|
+
it ('run after_destroy callback') { klass.must_respond_to(:run_after_destroy_callbacks) }
|
22
26
|
it ('run before_validation callbacks') { klass.must_respond_to(:run_before_validation_callbacks) }
|
23
27
|
it ('run after_validation callbacks') { klass.must_respond_to(:run_after_validation_callbacks) }
|
24
28
|
it ('run before_save callbacks') { klass.must_respond_to(:run_before_save_callbacks) }
|
@@ -26,6 +30,8 @@ describe CsvRecord::Callbacks do
|
|
26
30
|
it ('run after_initialize callbacks') { klass.must_respond_to(:run_after_initialize_callbacks) }
|
27
31
|
it ('run before_update callbacks') { klass.must_respond_to(:run_before_update_callbacks) }
|
28
32
|
it ('run after_update callbacks') { klass.must_respond_to(:run_after_update_callbacks) }
|
33
|
+
it ('run before_destroy callbacks ') { klass.must_respond_to(:run_before_destroy_callbacks) }
|
34
|
+
it ('run after_find callbacks') { klass.must_respond_to(:run_after_find_callbacks) }
|
29
35
|
end
|
30
36
|
|
31
37
|
describe 'Checking the callbacks execution' do
|
@@ -35,6 +41,11 @@ describe CsvRecord::Callbacks do
|
|
35
41
|
object_created
|
36
42
|
end
|
37
43
|
|
44
|
+
let (:object_destroyed) do
|
45
|
+
object_created.destroy
|
46
|
+
object_created
|
47
|
+
end
|
48
|
+
|
38
49
|
it 'after_initialize' do
|
39
50
|
CallbackTestClass.new.after_initialize_called.must_equal true
|
40
51
|
end
|
@@ -70,5 +81,17 @@ describe CsvRecord::Callbacks do
|
|
70
81
|
it 'after_update' do
|
71
82
|
object_created.after_save_called.must_equal true
|
72
83
|
end
|
84
|
+
|
85
|
+
it 'after_destroy' do
|
86
|
+
object_destroyed.after_destroy_called.must_equal true
|
87
|
+
end
|
88
|
+
it 'before_destroy' do
|
89
|
+
object_destroyed.before_destroy_called.must_equal true
|
90
|
+
end
|
91
|
+
|
92
|
+
it 'after_find using FIND' do
|
93
|
+
object_found = CallbackTestClass.find(object_created)
|
94
|
+
object_found.after_find_called.must_equal true
|
95
|
+
end
|
73
96
|
end
|
74
97
|
end
|
@@ -2,6 +2,7 @@ require_relative '../test_helper'
|
|
2
2
|
require 'pry'
|
3
3
|
require_relative '../models/jedi'
|
4
4
|
require_relative '../models/jedi_order'
|
5
|
+
require_relative '../models/custom_errors_class'
|
5
6
|
|
6
7
|
describe CsvRecord::Validations do
|
7
8
|
let (:invalid_jedi) { Jedi.new }
|
@@ -9,8 +10,12 @@ describe CsvRecord::Validations do
|
|
9
10
|
describe 'initializing class methods' do
|
10
11
|
it ('responds to validates_presence_of') { Jedi.must_respond_to :validates_presence_of }
|
11
12
|
it ('responds to validates_uniqueness_of') { Jedi.must_respond_to :validates_uniqueness_of }
|
13
|
+
end
|
14
|
+
|
15
|
+
describe 'initializing instance methods' do
|
12
16
|
it ('responds to valid?') { Jedi.new.must_respond_to :valid? }
|
13
17
|
it ('responds to invalid?') { Jedi.new.must_respond_to :invalid? }
|
18
|
+
it ('responds to errors') { Jedi.new.must_respond_to :errors }
|
14
19
|
end
|
15
20
|
|
16
21
|
describe 'validates_presence_of :name and :age behavior' do
|
@@ -50,34 +55,45 @@ describe CsvRecord::Validations do
|
|
50
55
|
yoda.valid?.wont_equal true
|
51
56
|
yoda.save.wont_equal true
|
52
57
|
end
|
58
|
+
|
53
59
|
end
|
54
60
|
|
55
|
-
describe '
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
end
|
61
|
+
describe 'invalid?' do
|
62
|
+
it 'invalid object' do
|
63
|
+
invalid_jedi.invalid?.must_equal true
|
64
|
+
end
|
60
65
|
|
61
|
-
|
62
|
-
|
63
|
-
|
66
|
+
it 'valid object' do
|
67
|
+
yoda.invalid?.must_equal false
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
describe 'errors' do
|
72
|
+
it 'wont be empty when invalid' do
|
73
|
+
invalid_jedi.valid?
|
74
|
+
invalid_jedi.errors.wont_be_empty
|
64
75
|
end
|
65
76
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
end
|
77
|
+
it 'should have two erros' do
|
78
|
+
invalid_jedi.valid?
|
79
|
+
invalid_jedi.errors.length.must_equal 2
|
80
|
+
end
|
71
81
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
82
|
+
it 'should contain the errors found' do
|
83
|
+
invalid_jedi.valid?
|
84
|
+
invalid_jedi.errors.must_include :name
|
85
|
+
invalid_jedi.errors.must_include :age
|
86
|
+
end
|
87
|
+
|
88
|
+
describe 'add' do
|
89
|
+
it ('responds to add') { invalid_jedi.errors.must_respond_to :add }
|
76
90
|
|
77
|
-
it '
|
78
|
-
invalid_jedi.
|
79
|
-
|
80
|
-
|
91
|
+
it 'adding' do
|
92
|
+
invalid_jedi.instance_eval do
|
93
|
+
self.errors.add(:testing)
|
94
|
+
self.errors.wont_be_empty
|
95
|
+
self.errors.last.must_equal :testing
|
96
|
+
end
|
81
97
|
end
|
82
98
|
end
|
83
99
|
end
|
@@ -95,16 +111,17 @@ describe CsvRecord::Validations do
|
|
95
111
|
end
|
96
112
|
|
97
113
|
describe 'custom_validator' do
|
98
|
-
it ('responds to validate') {
|
114
|
+
it ('responds to validate') { CustomErrorsClass.must_respond_to :validate }
|
99
115
|
|
100
|
-
|
116
|
+
let(:custom_error_class) { CustomErrorsClass.new }
|
117
|
+
before { custom_error_class.valid? }
|
101
118
|
|
102
119
|
it 'adding a custom validator' do
|
103
|
-
|
120
|
+
custom_error_class.errors.must_include :custom_error
|
104
121
|
end
|
105
122
|
|
106
123
|
it 'validate can have a block' do
|
107
|
-
|
124
|
+
custom_error_class.errors.must_include :custom_error_with_block
|
108
125
|
end
|
109
126
|
end
|
110
127
|
|
@@ -8,11 +8,17 @@ class CallbackTestClass
|
|
8
8
|
attr_accessor :before_validation_called, :after_validation_called
|
9
9
|
attr_accessor :before_update_called, :after_update_called
|
10
10
|
attr_accessor :before_save_called, :after_save_called
|
11
|
+
attr_accessor :before_destroy_called, :after_destroy_called
|
12
|
+
attr_accessor :after_find_called, :after_where_called
|
11
13
|
|
12
14
|
after_initialize do
|
13
15
|
self.after_initialize_called = true
|
14
16
|
end
|
15
17
|
|
18
|
+
after_find do
|
19
|
+
self.after_find_called = true
|
20
|
+
end
|
21
|
+
|
16
22
|
before_create do
|
17
23
|
self.before_create_called = true
|
18
24
|
end
|
@@ -44,4 +50,13 @@ class CallbackTestClass
|
|
44
50
|
after_save do
|
45
51
|
self.after_save_called = true
|
46
52
|
end
|
53
|
+
|
54
|
+
after_destroy do
|
55
|
+
self.after_destroy_called = true
|
56
|
+
end
|
57
|
+
|
58
|
+
before_destroy do
|
59
|
+
self.before_destroy_called = true
|
60
|
+
end
|
61
|
+
|
47
62
|
end
|
data/test/models/jedi.rb
CHANGED
@@ -2,20 +2,16 @@ class Jedi
|
|
2
2
|
include CsvRecord::Document
|
3
3
|
|
4
4
|
attr_accessor :name, :age, :midi_chlorians
|
5
|
-
attr_reader :custom_validator_checker, :custom_validator_checker_with_block
|
5
|
+
attr_reader :custom_validator_checker, :custom_validator_checker_with_block, :after_destroy_value, :after_save_value
|
6
6
|
|
7
|
+
after_destroy :my_after_destroy_method
|
8
|
+
after_save :my_after_save_method
|
7
9
|
belongs_to :jedi_order
|
10
|
+
has_one :padawan
|
8
11
|
|
9
12
|
validates_presence_of :name, :age
|
10
13
|
validates_uniqueness_of :name
|
11
14
|
|
12
|
-
validate :my_custom_validator_method
|
13
|
-
validate do |obj|
|
14
|
-
obj.instance_eval do
|
15
|
-
@custom_validator_checker_with_block = true
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
15
|
def initialize(params={})
|
20
16
|
params.each do |key, value|
|
21
17
|
self.public_send("#{key}=", value)
|
@@ -26,4 +22,11 @@ class Jedi
|
|
26
22
|
def my_custom_validator_method
|
27
23
|
@custom_validator_checker = true
|
28
24
|
end
|
25
|
+
def my_after_destroy_method
|
26
|
+
@after_destroy_value = true
|
27
|
+
end
|
28
|
+
def my_after_save_method
|
29
|
+
@after_save_value = true
|
30
|
+
end
|
31
|
+
|
29
32
|
end
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: csv_record
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.9.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-12-
|
12
|
+
date: 2012-12-11 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -128,13 +128,15 @@ files:
|
|
128
128
|
- test/csv_record/writer_test.rb
|
129
129
|
- test/models/callback_test_class.rb
|
130
130
|
- test/models/car.rb
|
131
|
+
- test/models/custom_errors_class.rb
|
131
132
|
- test/models/jedi.rb
|
132
133
|
- test/models/jedi_order.rb
|
133
|
-
- test/
|
134
|
+
- test/models/padawan.rb
|
134
135
|
- test/monkey_patches/object_test.rb
|
135
136
|
- test/test_helper.rb
|
136
137
|
homepage: https://github.com/lukasalexandre/csv_record
|
137
|
-
licenses:
|
138
|
+
licenses:
|
139
|
+
- MIT
|
138
140
|
post_install_message:
|
139
141
|
rdoc_options: []
|
140
142
|
require_paths:
|
@@ -171,8 +173,9 @@ test_files:
|
|
171
173
|
- test/csv_record/writer_test.rb
|
172
174
|
- test/models/callback_test_class.rb
|
173
175
|
- test/models/car.rb
|
176
|
+
- test/models/custom_errors_class.rb
|
174
177
|
- test/models/jedi.rb
|
175
178
|
- test/models/jedi_order.rb
|
176
|
-
- test/
|
179
|
+
- test/models/padawan.rb
|
177
180
|
- test/monkey_patches/object_test.rb
|
178
181
|
- test/test_helper.rb
|
@@ -1,14 +0,0 @@
|
|
1
|
-
require_relative '../test_helper'
|
2
|
-
|
3
|
-
describe Array do
|
4
|
-
describe 'add' do
|
5
|
-
it ('responds to add') { Object.new.must_respond_to :integer? }
|
6
|
-
|
7
|
-
it 'adding' do
|
8
|
-
arr = Array.new
|
9
|
-
arr.add(:testing)
|
10
|
-
arr.wont_be_empty
|
11
|
-
arr.last.must_equal :testing
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|