csv_record 1.5.0 → 1.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +29 -2
- data/Rakefile +1 -1
- data/csv_record.gemspec +1 -0
- data/lib/csv_record/callbacks.rb +1 -0
- data/lib/csv_record/document.rb +3 -0
- data/lib/csv_record/helpers.rb +10 -4
- data/lib/csv_record/validations.rb +43 -0
- data/lib/csv_record/version.rb +1 -1
- data/lib/csv_record/writer.rb +5 -4
- data/test/csv_record/associations_test.rb +0 -1
- data/test/csv_record/validation_test.rb +77 -0
- data/test/models/jedi.rb +2 -0
- data/test/monkey_patches/array_test.rb +14 -0
- data/test/monkey_patches/object_test.rb +57 -0
- data/test/test_helper.rb +0 -1
- metadata +25 -2
data/README.md
CHANGED
@@ -33,6 +33,8 @@ class Car
|
|
33
33
|
include CsvRecord::Document
|
34
34
|
|
35
35
|
attr_accessor :year, :make, :model, :description, :price
|
36
|
+
|
37
|
+
validates_presence_of :price, :model
|
36
38
|
end
|
37
39
|
```
|
38
40
|
|
@@ -48,7 +50,7 @@ Car.create( # save the new record in the database
|
|
48
50
|
)
|
49
51
|
|
50
52
|
car.save # save the record in the database (either creating or changing)
|
51
|
-
|
53
|
+
car.valid? # verifies its validity, specially if it is declared some of the available validators (e.x:validates_presence_of).
|
52
54
|
car.update_attribute :year, 1999 # update a single field of an object
|
53
55
|
car.update_attributes year: 1999, model: 'E762' # update multiple fields at the same time
|
54
56
|
|
@@ -102,7 +104,7 @@ car.company # #<Company:0x007f9b249b24d8>
|
|
102
104
|
```
|
103
105
|
|
104
106
|
###Has Many
|
105
|
-
Extending the previous example, you can use the `has_many` method to
|
107
|
+
Extending the previous example, you can use the `has_many` method to establish the inverse relationship:
|
106
108
|
```ruby
|
107
109
|
class Company
|
108
110
|
include CsvRecord::Document
|
@@ -134,6 +136,31 @@ end
|
|
134
136
|
```
|
135
137
|
`obj` refers to the instance you are in
|
136
138
|
|
139
|
+
##Validations
|
140
|
+
|
141
|
+
Helpers available:
|
142
|
+
|
143
|
+
`validates_presence_of`: Ensures if the specified attributes were filled
|
144
|
+
|
145
|
+
```ruby
|
146
|
+
class Company
|
147
|
+
include CsvRecord::Document
|
148
|
+
validates_presence_of :name
|
149
|
+
attr_accessor :name
|
150
|
+
end
|
151
|
+
|
152
|
+
company = Company.create :name => ''
|
153
|
+
company.save
|
154
|
+
# => false
|
155
|
+
|
156
|
+
company = Company.create :name => ''
|
157
|
+
company.valid?
|
158
|
+
# => false
|
159
|
+
company.invalid?
|
160
|
+
# => true
|
161
|
+
|
162
|
+
```
|
163
|
+
|
137
164
|
##Precautions
|
138
165
|
CsvRecord creates a `db` folder in the root of your application. Be sure that it has permission to do so.
|
139
166
|
|
data/Rakefile
CHANGED
data/csv_record.gemspec
CHANGED
data/lib/csv_record/callbacks.rb
CHANGED
data/lib/csv_record/document.rb
CHANGED
@@ -7,6 +7,7 @@ require 'csv_record/timestamps'
|
|
7
7
|
require 'csv_record/callbacks'
|
8
8
|
require 'csv_record/helpers'
|
9
9
|
require 'csv_record/associations'
|
10
|
+
require 'csv_record/validations'
|
10
11
|
|
11
12
|
module CsvRecord
|
12
13
|
|
@@ -23,9 +24,11 @@ module CsvRecord
|
|
23
24
|
receiver.extend CsvRecord::Writer::ClassMethods
|
24
25
|
receiver.extend CsvRecord::Reader::ClassMethods
|
25
26
|
receiver.extend CsvRecord::Associations
|
27
|
+
receiver.extend CsvRecord::Validations::ClassMethods
|
26
28
|
receiver.send :include, CsvRecord::Writer::InstanceMethods
|
27
29
|
receiver.send :include, CsvRecord::Reader::InstanceMethods
|
28
30
|
receiver.send :include, CsvRecord::Timestamps
|
31
|
+
receiver.send :include, CsvRecord::Validations::InstanceMethods
|
29
32
|
receiver.send :include, CsvRecord::Callbacks
|
30
33
|
end
|
31
34
|
end
|
data/lib/csv_record/helpers.rb
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
class Object
|
2
2
|
def integer?
|
3
|
-
self.to_s
|
4
|
-
|
3
|
+
/(?<number>^\d+$)/ =~ self.to_s
|
4
|
+
not number.nil?
|
5
5
|
end
|
6
6
|
|
7
7
|
def float?
|
8
|
-
self.to_s
|
9
|
-
|
8
|
+
/(?<number>^\d+\.\d+)$/ =~ self.to_s
|
9
|
+
not number.nil?
|
10
10
|
end
|
11
11
|
|
12
12
|
def to_param
|
@@ -32,4 +32,10 @@ class String
|
|
32
32
|
tr("-", "_").
|
33
33
|
downcase
|
34
34
|
end
|
35
|
+
end
|
36
|
+
|
37
|
+
class Array
|
38
|
+
def add(obj)
|
39
|
+
self << obj
|
40
|
+
end
|
35
41
|
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module CsvRecord
|
2
|
+
module Validations
|
3
|
+
module ClassMethods
|
4
|
+
attr_writer :fields_to_validate
|
5
|
+
|
6
|
+
def fields_to_validate
|
7
|
+
@fields_to_validate || []
|
8
|
+
end
|
9
|
+
|
10
|
+
def __validates_presence_of__(*attr_names)
|
11
|
+
@fields_to_validate = attr_names
|
12
|
+
end
|
13
|
+
|
14
|
+
alias :validates_presence_of :__validates_presence_of__
|
15
|
+
end
|
16
|
+
|
17
|
+
module InstanceMethods
|
18
|
+
def __valid__?
|
19
|
+
validate_each(self.class.fields_to_validate)
|
20
|
+
errors.empty?
|
21
|
+
end
|
22
|
+
|
23
|
+
def invalid?
|
24
|
+
not self.__valid__?
|
25
|
+
end
|
26
|
+
|
27
|
+
def errors
|
28
|
+
@errors || []
|
29
|
+
end
|
30
|
+
|
31
|
+
alias :valid? :__valid__?
|
32
|
+
|
33
|
+
private
|
34
|
+
def validate_each(attributes)
|
35
|
+
attributes.each do |attribute|
|
36
|
+
if self.public_send(attribute).nil?
|
37
|
+
@errors = self.errors.add attribute
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/lib/csv_record/version.rb
CHANGED
data/lib/csv_record/writer.rb
CHANGED
@@ -26,7 +26,11 @@ module CsvRecord
|
|
26
26
|
end
|
27
27
|
|
28
28
|
def __save__
|
29
|
-
self.
|
29
|
+
if self.valid?
|
30
|
+
self.new_record? ? self.append_registry : self.update_registry
|
31
|
+
else
|
32
|
+
false
|
33
|
+
end
|
30
34
|
end
|
31
35
|
|
32
36
|
def new_record?
|
@@ -59,9 +63,6 @@ module CsvRecord
|
|
59
63
|
|
60
64
|
def calculate_id
|
61
65
|
@id = self.class.count + 1
|
62
|
-
# if self.respond_to? :jedi_order_id
|
63
|
-
# p "#{self.class} -> #{@id} -> #{self.jedi_order_id}"
|
64
|
-
# end
|
65
66
|
end
|
66
67
|
|
67
68
|
def append_registry
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require_relative '../test_helper'
|
2
|
+
require 'pry'
|
3
|
+
require_relative '../models/jedi'
|
4
|
+
require_relative '../models/jedi_order'
|
5
|
+
|
6
|
+
describe CsvRecord::Validations do
|
7
|
+
describe 'initializing class methods' do
|
8
|
+
it ('responds to validates_presence_of') { Jedi.must_respond_to :validates_presence_of }
|
9
|
+
it ('responds to valid?') { Jedi.new.must_respond_to :valid? }
|
10
|
+
it ('responds to invalid?') { Jedi.new.must_respond_to :invalid? }
|
11
|
+
end
|
12
|
+
|
13
|
+
describe 'validates_presence_of :name and :age behavior' do
|
14
|
+
it 'is not valid without name' do
|
15
|
+
yoda.name = nil
|
16
|
+
yoda.valid?.wont_equal true
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'is not valid without age' do
|
20
|
+
yoda.age = nil
|
21
|
+
yoda.valid?.wont_equal true
|
22
|
+
end
|
23
|
+
|
24
|
+
it "is valid with all attributes filled" do
|
25
|
+
yoda.valid?.must_equal true
|
26
|
+
end
|
27
|
+
|
28
|
+
it "saves with both attributes filled" do
|
29
|
+
yoda.save.must_equal true
|
30
|
+
end
|
31
|
+
|
32
|
+
it "doesn't save without name" do
|
33
|
+
yoda.name = nil
|
34
|
+
yoda.valid?.wont_equal true
|
35
|
+
yoda.save.wont_equal true
|
36
|
+
end
|
37
|
+
|
38
|
+
it "doesn't save without age" do
|
39
|
+
yoda.age = nil
|
40
|
+
yoda.valid?.wont_equal true
|
41
|
+
yoda.save.wont_equal true
|
42
|
+
end
|
43
|
+
|
44
|
+
it "doesn't save without both" do
|
45
|
+
yoda.age = nil
|
46
|
+
yoda.name = nil
|
47
|
+
yoda.valid?.wont_equal true
|
48
|
+
yoda.save.wont_equal true
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
describe 'default methods' do
|
53
|
+
let (:invalid_jedi) { Jedi.new }
|
54
|
+
|
55
|
+
describe 'invalid?' do
|
56
|
+
it 'invalid object' do
|
57
|
+
invalid_jedi.invalid?.must_equal true
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'valid object' do
|
61
|
+
yoda.invalid?.must_equal false
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
describe 'errors' do
|
66
|
+
it 'wont be empty when invalid' do
|
67
|
+
invalid_jedi.valid?
|
68
|
+
invalid_jedi.errors.wont_be_empty
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'should have two erros' do
|
72
|
+
invalid_jedi.valid?
|
73
|
+
invalid_jedi.errors.length.must_equal 2
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
data/test/models/jedi.rb
CHANGED
@@ -0,0 +1,14 @@
|
|
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
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require_relative '../test_helper'
|
2
|
+
|
3
|
+
describe Object do
|
4
|
+
describe 'monkey patch' do
|
5
|
+
it ('responds to integer?') { Object.new.must_respond_to :integer? }
|
6
|
+
it ('responds to float?') { Object.new.must_respond_to :float? }
|
7
|
+
it ('responds to to_param?') { Object.new.must_respond_to :to_param }
|
8
|
+
it ('responds to underscored_class_name') { Object.new.must_respond_to :underscored_class_name }
|
9
|
+
end
|
10
|
+
|
11
|
+
describe 'integer?' do
|
12
|
+
it 'passing an integer' do
|
13
|
+
15.integer?.must_equal true
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'passing a string' do
|
17
|
+
'abc'.integer?.must_equal false
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe 'float?' do
|
22
|
+
it 'passing a float' do
|
23
|
+
(15.50).float?.must_equal true
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'passing an integer' do
|
27
|
+
15.float?.must_equal false
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'passing a string' do
|
31
|
+
'abc'.float?.must_equal false
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe 'to_param?' do
|
36
|
+
it 'in an integer' do
|
37
|
+
15.to_param.must_equal 15
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'passing a string' do
|
41
|
+
'abc'.to_param.must_equal 'abc'
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'passing a string' do
|
45
|
+
(15.50).to_param.must_equal 15.50
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe 'underscored_class_name' do
|
50
|
+
it 'custom class' do
|
51
|
+
new_class = Class.new
|
52
|
+
MyCustomClass = new_class
|
53
|
+
|
54
|
+
MyCustomClass.new.underscored_class_name.must_equal 'my_custom_class'
|
55
|
+
end
|
56
|
+
end
|
57
|
+
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.6.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-11-
|
12
|
+
date: 2012-11-23 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -43,6 +43,22 @@ dependencies:
|
|
43
43
|
- - ! '>='
|
44
44
|
- !ruby/object:Gem::Version
|
45
45
|
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: pry
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
46
62
|
- !ruby/object:Gem::Dependency
|
47
63
|
name: timecop
|
48
64
|
requirement: !ruby/object:Gem::Requirement
|
@@ -97,6 +113,7 @@ files:
|
|
97
113
|
- lib/csv_record/helpers.rb
|
98
114
|
- lib/csv_record/reader.rb
|
99
115
|
- lib/csv_record/timestamps.rb
|
116
|
+
- lib/csv_record/validations.rb
|
100
117
|
- lib/csv_record/version.rb
|
101
118
|
- lib/csv_record/writer.rb
|
102
119
|
- test/csv_record/associations_test.rb
|
@@ -107,11 +124,14 @@ files:
|
|
107
124
|
- test/csv_record/helpers_test.rb
|
108
125
|
- test/csv_record/reader_test.rb
|
109
126
|
- test/csv_record/timestamps_test.rb
|
127
|
+
- test/csv_record/validation_test.rb
|
110
128
|
- test/csv_record/writer_test.rb
|
111
129
|
- test/models/callback_test_class.rb
|
112
130
|
- test/models/car.rb
|
113
131
|
- test/models/jedi.rb
|
114
132
|
- test/models/jedi_order.rb
|
133
|
+
- test/monkey_patches/array_test.rb
|
134
|
+
- test/monkey_patches/object_test.rb
|
115
135
|
- test/test_helper.rb
|
116
136
|
homepage: https://github.com/lukasalexandre/csv_record
|
117
137
|
licenses: []
|
@@ -147,9 +167,12 @@ test_files:
|
|
147
167
|
- test/csv_record/helpers_test.rb
|
148
168
|
- test/csv_record/reader_test.rb
|
149
169
|
- test/csv_record/timestamps_test.rb
|
170
|
+
- test/csv_record/validation_test.rb
|
150
171
|
- test/csv_record/writer_test.rb
|
151
172
|
- test/models/callback_test_class.rb
|
152
173
|
- test/models/car.rb
|
153
174
|
- test/models/jedi.rb
|
154
175
|
- test/models/jedi_order.rb
|
176
|
+
- test/monkey_patches/array_test.rb
|
177
|
+
- test/monkey_patches/object_test.rb
|
155
178
|
- test/test_helper.rb
|