csv_record 1.3.0 → 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.
- data/README.md +4 -0
- data/lib/csv_record/reader.rb +10 -2
- data/lib/csv_record/version.rb +1 -1
- data/lib/csv_record/writer.rb +9 -1
- data/test/csv_record/reader_test.rb +13 -1
- data/test/csv_record/writer_test.rb +8 -0
- data/test/test_helper.rb +1 -1
- metadata +2 -2
data/README.md
CHANGED
@@ -48,6 +48,7 @@ Car.create( # save the new record in the database
|
|
48
48
|
car.save # save the record in the database (either creating or changing)
|
49
49
|
|
50
50
|
car.update_attribute :year, 1999 # update a single field of an object
|
51
|
+
car.update_attributes year: 1999, model: 'E762' # update multiple fields at the same time
|
51
52
|
|
52
53
|
car.destroy # removes the record from the database
|
53
54
|
|
@@ -58,6 +59,9 @@ Car.all # retrieves all saved records
|
|
58
59
|
Car.find car.id # find through its id
|
59
60
|
Car.find car # find through the record
|
60
61
|
|
62
|
+
Car.find_by_model 'F450' # find dynamically with a property
|
63
|
+
Car.find_by_model_and_price 'F450', 5000.00 # find dynamically with multiple properties
|
64
|
+
|
61
65
|
Car.where year: 2007, make: 'Chevrolet', model: 'F450' # find with a multiple parameters hash
|
62
66
|
|
63
67
|
Car.count # returns the amount of records in the database
|
data/lib/csv_record/reader.rb
CHANGED
@@ -3,13 +3,21 @@ module CsvRecord
|
|
3
3
|
module ClassMethods
|
4
4
|
DYNAMIC_FINDER_PATTERN = /^find_by_(.+)$/
|
5
5
|
|
6
|
+
def build(params={})
|
7
|
+
inst = new
|
8
|
+
params.each do |key, value|
|
9
|
+
inst.public_send("#{key}=", value)
|
10
|
+
end
|
11
|
+
inst
|
12
|
+
end
|
13
|
+
|
6
14
|
def __fields__
|
7
15
|
instance_methods(false).select { |m| m.to_s !~ /=$/ }
|
8
16
|
end
|
9
17
|
|
10
18
|
def all
|
11
19
|
open_database_file do |csv|
|
12
|
-
csv.entries.map { |attributes| self.
|
20
|
+
csv.entries.map { |attributes| self.build attributes }
|
13
21
|
end
|
14
22
|
end
|
15
23
|
|
@@ -35,7 +43,7 @@ module CsvRecord
|
|
35
43
|
def __where__(params)
|
36
44
|
open_database_file do |csv|
|
37
45
|
rows = search_for csv, params
|
38
|
-
rows.map { |row| self.
|
46
|
+
rows.map { |row| self.build row.to_hash }
|
39
47
|
end
|
40
48
|
end
|
41
49
|
|
data/lib/csv_record/version.rb
CHANGED
data/lib/csv_record/writer.rb
CHANGED
@@ -4,7 +4,7 @@ module CsvRecord
|
|
4
4
|
module Writer
|
5
5
|
module ClassMethods
|
6
6
|
def __create__(attributes={})
|
7
|
-
instance = self.
|
7
|
+
instance = self.build attributes
|
8
8
|
instance.run_before_create_callbacks
|
9
9
|
result = instance.save
|
10
10
|
instance.run_after_create_callbacks if result
|
@@ -32,6 +32,13 @@ module CsvRecord
|
|
32
32
|
self.save
|
33
33
|
end
|
34
34
|
|
35
|
+
def __update_attributes__(params={})
|
36
|
+
params.each do |field, value|
|
37
|
+
self.public_send "#{field}=", value
|
38
|
+
end
|
39
|
+
self.save
|
40
|
+
end
|
41
|
+
|
35
42
|
def __destroy__
|
36
43
|
self.class.parse_database_file do |row|
|
37
44
|
new_row = row
|
@@ -82,6 +89,7 @@ module CsvRecord
|
|
82
89
|
alias :write_object :__write_object__
|
83
90
|
alias :update_attribute :__update_attribute__
|
84
91
|
alias :destroy :__destroy__
|
92
|
+
alias :update_attributes :__update_attributes__
|
85
93
|
end
|
86
94
|
end
|
87
95
|
end
|
@@ -15,7 +15,7 @@ describe CsvRecord::Reader do
|
|
15
15
|
|
16
16
|
describe 'validating the methods behavior' do
|
17
17
|
let(:second_car) do
|
18
|
-
Car.
|
18
|
+
Car.build(
|
19
19
|
year: 2007,
|
20
20
|
make: 'Chevrolet',
|
21
21
|
model: 'F450',
|
@@ -24,6 +24,18 @@ describe CsvRecord::Reader do
|
|
24
24
|
)
|
25
25
|
end
|
26
26
|
|
27
|
+
it 'building an instance' do
|
28
|
+
new_car = Car.build(
|
29
|
+
year: 2007,
|
30
|
+
make: 'Chevrolet',
|
31
|
+
model: 'F450',
|
32
|
+
description: 'ac, abs, moon',
|
33
|
+
price: 5000.00
|
34
|
+
)
|
35
|
+
new_car.wont_be_nil
|
36
|
+
new_car.must_be_instance_of Car
|
37
|
+
end
|
38
|
+
|
27
39
|
it "Check the current fields" do
|
28
40
|
Car.fields.must_equal [:id, :created_at, :updated_at, :year, :make, :model, :description, :price]
|
29
41
|
end
|
@@ -16,6 +16,7 @@ describe CsvRecord::Writer do
|
|
16
16
|
it ('responds to write_object') { car.must_respond_to :write_object }
|
17
17
|
it ('responds to id') { car.must_respond_to :id }
|
18
18
|
it ('responds to update_attribute') { car.must_respond_to :update_attribute }
|
19
|
+
it ('responds to update_attributes') { car.must_respond_to :update_attributes }
|
19
20
|
it ('responds to destroy') { car.must_respond_to :destroy }
|
20
21
|
end
|
21
22
|
|
@@ -79,6 +80,13 @@ describe CsvRecord::Writer do
|
|
79
80
|
Car.find(car).year.must_equal '2008'
|
80
81
|
end
|
81
82
|
|
83
|
+
it "Updates multiple fields at the same time" do
|
84
|
+
car.save
|
85
|
+
car.update_attributes year: 2008, model: 'E846'
|
86
|
+
Car.find(car).year.must_equal '2008'
|
87
|
+
Car.find(car).model.must_equal 'E846'
|
88
|
+
end
|
89
|
+
|
82
90
|
it "Updates multiple fields using save" do
|
83
91
|
car.save
|
84
92
|
car.year = 2008
|
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.4.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-05 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|