csv_record 2.1.0 → 2.1.1
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.
- data/README.md +247 -7
- data/lib/csv_record/version.rb +1 -1
- data/lib/csv_record.rb +3 -3
- metadata +4 -4
data/README.md
CHANGED
@@ -2,20 +2,260 @@
|
|
2
2
|
|
3
3
|
[](https://travis-ci.org/lukelex/csv_record) [](https://codeclimate.com/github/lukelex/csv_record) [](https://gemnasium.com/lukasalexandre/csv_record) [](http://badge.fury.io/rb/csv_record)
|
4
4
|
|
5
|
-
|
6
5
|
CSV Record connects Ruby classes to CSV documents database to establish an almost zero-configuration persistence layer for applications.
|
7
6
|
|
8
|
-
|
7
|
+
##Getting Started
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'csv_record'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
```
|
18
|
+
$ bundle
|
19
|
+
```
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
```
|
24
|
+
$ gem install csv_record
|
25
|
+
```
|
26
|
+
|
27
|
+
And inside your Ruby models just require and include the CSVRecord lib and start using it in the same way as your are used to:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
require 'csv_record'
|
31
|
+
|
32
|
+
class Jedi
|
33
|
+
include CsvRecord::Document
|
34
|
+
|
35
|
+
attr_accessor :name, :age, :midi_chlorians
|
36
|
+
end
|
37
|
+
```
|
38
|
+
|
39
|
+
##Persistance
|
40
|
+
To persist the data objects created in your application your can use the following methods:
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
Jedi.create( # save the new record in the database
|
44
|
+
name: 'Luke Skywalker',
|
45
|
+
age: 18,
|
46
|
+
midi_chlorians: '12k'
|
47
|
+
)
|
48
|
+
|
49
|
+
jedi.save # save the record in the database (either creating or changing)
|
50
|
+
|
51
|
+
jedi.update_attribute :age, 29 # update a single field of an object
|
52
|
+
jedi.update_attributes age: 29, midi_chlorians: '18k' # update multiple fields at the same time
|
53
|
+
|
54
|
+
jedi.destroy # removes the record from the database
|
55
|
+
|
56
|
+
jedi.new_record? # checks if the record is new
|
57
|
+
```
|
58
|
+
|
59
|
+
##Querying
|
60
|
+
Records can be queried through the following methods:
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
Jedi.all # retrieves all saved records
|
64
|
+
|
65
|
+
Jedi.find jedi.id # find through its id
|
66
|
+
Jedi.find jedi # find through the record
|
67
|
+
|
68
|
+
Jedi.find_by_age 18 # find dynamically with a property
|
69
|
+
Jedi.find_by_name_and_age 'Luke Skywalker', 18 # find dynamically with multiple properties
|
70
|
+
|
71
|
+
Jedi.where age: 18, name: 'Luke Skywalker', midi_chlorians: '12k' # find with a multiple parameters hash
|
72
|
+
|
73
|
+
Jedi.count # returns the amount of records in the database
|
74
|
+
|
75
|
+
Jedi.first # retrieves the first record in the database
|
76
|
+
Jedi.last # retrieves the last record in the database
|
77
|
+
```
|
78
|
+
|
79
|
+
Lazy querying is the default behavior now Yey!!
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
query = Jedi.where(age: 37).where(midi_chlorians: '4k')
|
83
|
+
query # #<CsvRecord::Query:0x007fdff3d31aa0>
|
84
|
+
|
85
|
+
query.first # #<Jedi:0x007f9df6cea478>
|
86
|
+
```
|
87
|
+
|
88
|
+
##Associations
|
89
|
+
###Belongs To
|
90
|
+
A Belongs To association can be declared through the following method:
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
class JediOrder
|
94
|
+
include CsvRecord::Document
|
95
|
+
|
96
|
+
attr_accessor :rank
|
97
|
+
end
|
98
|
+
|
99
|
+
class Jedi
|
100
|
+
include CsvRecord::Document
|
101
|
+
|
102
|
+
belongs_to :jedi_order
|
103
|
+
|
104
|
+
attr_accessor :name
|
105
|
+
end
|
106
|
+
|
107
|
+
jedi_order = JediOrder.create rank: 'council'
|
108
|
+
|
109
|
+
jedi = Jedi.new name: 'Lukas Alexandre'
|
110
|
+
|
111
|
+
jedi.jedi_order = jedi_order
|
112
|
+
# or
|
113
|
+
jedi.jedi_order_id = jedi_order.id
|
114
|
+
|
115
|
+
jedi.save
|
116
|
+
|
117
|
+
jedi.jedi_order # #<JediOrder:0x007f9b249b24d8>
|
118
|
+
```
|
119
|
+
|
120
|
+
###Has Many
|
121
|
+
Extending the previous example, you can use the `has_many` method to establish the inverse relationship:
|
122
|
+
|
123
|
+
```ruby
|
124
|
+
class JediOrder
|
125
|
+
include CsvRecord::Document
|
126
|
+
|
127
|
+
attr_accessor :rank
|
128
|
+
|
129
|
+
has_many :jedis
|
130
|
+
end
|
131
|
+
|
132
|
+
jedi_order = JediOrder.create rank: 'council'
|
133
|
+
|
134
|
+
jedi.jedi_order = jedi_order
|
135
|
+
jedi.save
|
136
|
+
|
137
|
+
jedi_order.jedis # [#<Jedi:0x007f9b249b24d8>]
|
138
|
+
```
|
139
|
+
|
140
|
+
###Has One
|
141
|
+
The same as has_many but limited to one associated record.
|
142
|
+
|
143
|
+
```ruby
|
144
|
+
class jedi
|
145
|
+
include CsvRecord::Document
|
146
|
+
|
147
|
+
attr_accessor :name
|
148
|
+
|
149
|
+
has_one :padawan
|
150
|
+
end
|
151
|
+
|
152
|
+
class Padawan
|
153
|
+
include CsvRecord::Document
|
154
|
+
|
155
|
+
attr_accessor :name
|
156
|
+
|
157
|
+
belongs_to :jedi
|
158
|
+
end
|
159
|
+
|
160
|
+
padawan = Padawan.create name: 'Lukas Alexandre'
|
161
|
+
|
162
|
+
jedi.padawan = padawan
|
163
|
+
|
164
|
+
jedi.padawan # #<Padawan:0x007f9b249b24d8>
|
165
|
+
```
|
166
|
+
|
167
|
+
##Callbacks
|
168
|
+
###Overview
|
169
|
+
Callbacks can be used to execute code on predetermined moments.
|
170
|
+
|
171
|
+
####Usage
|
172
|
+
```ruby
|
173
|
+
after_create do
|
174
|
+
# learn the way of the force
|
175
|
+
end
|
176
|
+
```
|
177
|
+
`self` refers to the instance you are in
|
178
|
+
|
179
|
+
###Avaiable Callbacks
|
180
|
+
Here is a list with all the available callbacks, listed in the same order in which they will get called during the respective operations:
|
181
|
+
|
182
|
+
####Finding an Object
|
183
|
+
* after_initialize
|
184
|
+
* after_find
|
185
|
+
|
186
|
+
####Creating an Object
|
187
|
+
* after_initialize
|
188
|
+
* before_validation
|
189
|
+
* after_validation
|
190
|
+
* before_save
|
191
|
+
* before_create
|
192
|
+
* after_create
|
193
|
+
* after_save
|
194
|
+
|
195
|
+
####Updating an Object
|
196
|
+
* before_validation
|
197
|
+
* after_validation
|
198
|
+
* before_save
|
199
|
+
* before_update
|
200
|
+
* after_update
|
201
|
+
* after_save
|
202
|
+
|
203
|
+
####Destroying an Object
|
204
|
+
* before_destroy
|
205
|
+
* after_destroy
|
206
|
+
|
207
|
+
##Validations
|
208
|
+
###Helpers available:
|
209
|
+
|
210
|
+
`validates_presence_of`: Ensures if the specified attribute(s) were filled
|
211
|
+
|
212
|
+
`validates_uniqueness_of`: Ensures that the specified attribute(s) are unique within the database
|
213
|
+
|
214
|
+
`validate`: Uses custom method(s) to validate the model
|
215
|
+
|
216
|
+
```ruby
|
217
|
+
class Jedi
|
218
|
+
include CsvRecord::Document
|
219
|
+
|
220
|
+
attr_accessor :name
|
221
|
+
|
222
|
+
validates_presence_of :name
|
223
|
+
validates_uniqueness_of :name
|
224
|
+
|
225
|
+
validate :my_custom_validator_method
|
226
|
+
|
227
|
+
validate do
|
228
|
+
self.errors.add :attribute if self.using_dark_force?
|
229
|
+
end
|
230
|
+
|
231
|
+
def my_custom_validator_method
|
232
|
+
self.errors.add :attribute if self.attacking_instead_of_defending?
|
233
|
+
end
|
234
|
+
end
|
235
|
+
|
236
|
+
jedi = Jedi.new
|
237
|
+
|
238
|
+
jedi.valid? # => false
|
239
|
+
jedi.invalid? # => true
|
240
|
+
jedi.save # => false
|
241
|
+
```
|
9
242
|
|
10
|
-
##
|
243
|
+
##Customizations
|
11
244
|
|
12
|
-
|
245
|
+
Someday you might want to go "out of the rail" that we propose. Here is what you can do now:
|
13
246
|
|
14
|
-
|
247
|
+
###Changing the table_name
|
248
|
+
```ruby
|
249
|
+
store_as :wierd_table_name
|
250
|
+
```
|
251
|
+
###Changing the field column name
|
252
|
+
```ruby
|
253
|
+
mapping :name => :wierd_field
|
254
|
+
```
|
15
255
|
|
16
256
|
##Bug reports
|
17
257
|
|
18
|
-
If you discover a problem with CSV_Record, we would like to know about it.
|
258
|
+
If you discover a problem with CSV_Record, we would like to know about it. Please let us know on the project issues page.
|
19
259
|
|
20
260
|
##Contributing
|
21
261
|
|
@@ -26,4 +266,4 @@ https://github.com/lukelex/csv_record/wiki/Contributing
|
|
26
266
|
You will usually want to write tests for your changes. To run the test suite, go into CSV_Record's top-level directory and run "bundle install" and "rake". For the tests to pass.
|
27
267
|
|
28
268
|
##Precautions
|
29
|
-
CsvRecord creates a `db` folder in the root of your application. Be sure that it has permission to do so.
|
269
|
+
CsvRecord creates a `db` folder in the root of your application. Be sure that it has permission to do so.
|
data/lib/csv_record/version.rb
CHANGED
data/lib/csv_record.rb
CHANGED
@@ -23,9 +23,9 @@
|
|
23
23
|
|
24
24
|
require 'csv'
|
25
25
|
|
26
|
-
require 'csv_record/
|
26
|
+
require 'csv_record/version'
|
27
27
|
|
28
|
-
require
|
28
|
+
require 'csv_record/document'
|
29
29
|
|
30
30
|
module CsvRecord
|
31
31
|
# Sets the CsvRecord configuration options. Best used by passing a block.
|
@@ -38,4 +38,4 @@ module CsvRecord
|
|
38
38
|
def configure
|
39
39
|
block_given? ? yield(self) : self
|
40
40
|
end
|
41
|
-
end
|
41
|
+
end
|
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: 2.1.
|
4
|
+
version: 2.1.1
|
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: 2013-
|
12
|
+
date: 2013-03-24 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -158,13 +158,13 @@ require_paths:
|
|
158
158
|
required_ruby_version: !ruby/object:Gem::Requirement
|
159
159
|
none: false
|
160
160
|
requirements:
|
161
|
-
- - '>='
|
161
|
+
- - ! '>='
|
162
162
|
- !ruby/object:Gem::Version
|
163
163
|
version: '0'
|
164
164
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
165
165
|
none: false
|
166
166
|
requirements:
|
167
|
-
- - '>='
|
167
|
+
- - ! '>='
|
168
168
|
- !ruby/object:Gem::Version
|
169
169
|
version: '0'
|
170
170
|
requirements: []
|