csv_record 1.0.2 → 1.2.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 CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
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)
4
4
 
5
- CSV Record connects classes to CSV documents database to establish an almost zero-configuration persistence layer for applications.
5
+ CSV Record connects Ruby classes to CSV documents database to establish an almost zero-configuration persistence layer for applications.
6
6
 
7
7
  ## Installation
8
8
 
@@ -27,6 +27,8 @@ $ gem install csv_record
27
27
  ## Usage
28
28
 
29
29
  ```ruby
30
+ requite 'csv_record'
31
+
30
32
  class Car
31
33
  include CsvRecord::Document
32
34
  end
@@ -56,7 +58,12 @@ Car.all # retrieves all saved records
56
58
  Car.find car.id # find through its id
57
59
  Car.find car # find through the record
58
60
 
61
+ Car.where year: 2007, make: 'Chevrolet', model: 'F450' # find with a multiple parameters hash
62
+
59
63
  Car.count # returns the amount of records in the database
64
+
65
+ Car.first # retrieves the first record in the database
66
+ Car.last # retrieves the last record in the database
60
67
  ```
61
68
 
62
69
  ##Callbacks
data/csv_record.gemspec CHANGED
@@ -4,8 +4,8 @@ require File.expand_path('../lib/csv_record/version', __FILE__)
4
4
  Gem::Specification.new do |gem|
5
5
  gem.authors = ["Lukas Alexandre"]
6
6
  gem.email = ["lukasalexandre@gmail.com"]
7
- gem.description = %q{CSV Object-relational mapping}
8
- gem.summary = %q{CSV Record connects classes to CSV documents database to establish an almost zero-configuration persistence layer for applications.}
7
+ gem.description = %q{CSV Object-relational mapping for Ruby}
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
11
  gem.files = `git ls-files`.split($\)
@@ -11,6 +11,14 @@ module CsvRecord
11
11
  end
12
12
  end
13
13
 
14
+ def first
15
+ all.first
16
+ end
17
+
18
+ def last
19
+ all.last
20
+ end
21
+
14
22
  def __count__
15
23
  open_database_file do |csv|
16
24
  csv.entries.size
@@ -19,15 +27,48 @@ module CsvRecord
19
27
 
20
28
  def __find__(param)
21
29
  param = param.id unless param.is_a? Integer
30
+ (__where__ :id => param).first
31
+ end
32
+
33
+ def __where__(params)
22
34
  open_database_file do |csv|
23
- row = csv.entries.select { |attributes| attributes['id'].to_i == param }.first
24
- self.new row.to_hash
35
+ rows = search_for csv, params
36
+ rows.map { |row| self.new row.to_hash }
37
+ end
38
+ end
39
+
40
+ def search_for(csv, params)
41
+ conditions = handle_params params
42
+ csv.entries.select do |attributes|
43
+ eval conditions
44
+ end
45
+ end
46
+
47
+ def handle_params(params)
48
+ conditions = ''
49
+ index = 0
50
+ params.each_pair do |property, value|
51
+ conditions << "attributes['#{property}'] == '#{value}'"
52
+ conditions << ' && ' if (params.size > 1) && (index != params.size - 1)
53
+ index += 1
54
+ end
55
+ conditions
56
+ end
57
+
58
+ def method_missing(meth, *args, &block)
59
+ if meth.to_s =~ /^find_by_(.+)$/
60
+ dynamic_finder $1, *args, &block
61
+ else
62
+ super # You *must* call super if you don't handle the
63
+ # method, otherwise you'll mess up Ruby's method
64
+ # lookup.
25
65
  end
26
66
  end
27
67
 
28
68
  alias :fields :__fields__
29
69
  alias :find :__find__
30
70
  alias :count :__count__
71
+ alias :where :__where__
31
72
  end
32
73
 
33
74
  module InstanceMethods
@@ -1,3 +1,3 @@
1
1
  module CsvRecord
2
- VERSION = "1.0.2"
2
+ VERSION = "1.2.0"
3
3
  end
@@ -51,24 +51,61 @@ describe CsvRecord::Reader do
51
51
  Car.count.must_equal 2
52
52
  end
53
53
 
54
- it 'querying by id' do
55
- cars = []
56
- 3.times do
57
- cars << car.clone
58
- cars.last.save
54
+ describe 'simple query' do
55
+ let (:cars) { [] }
56
+
57
+ before do
58
+ 3.times do
59
+ cars << car.clone
60
+ cars.last.save
61
+ end
62
+ end
63
+
64
+ it 'querying by id' do
65
+ Car.find(cars.first.id).wont_be_nil
66
+ Car.find(cars.first.id).must_be_instance_of Car
67
+ end
68
+
69
+ it 'querying by object' do
70
+ Car.find(cars.first).wont_be_nil
71
+ Car.find(cars.first.id).must_be_instance_of Car
72
+ end
73
+
74
+ it 'gets the first' do
75
+ first_car = Car.first
76
+ first_car.wont_be_nil
77
+ first_car.must_be_instance_of Car
78
+ end
79
+
80
+ it 'gets the last' do
81
+ last_car = Car.last
82
+ last_car.wont_be_nil
83
+ last_car.must_be_instance_of Car
59
84
  end
60
- Car.find(cars.first.id).wont_be_nil
61
- Car.find(cars.first.id).must_be_instance_of Car
62
85
  end
63
86
 
64
- it 'querying by object' do
65
- cars = []
66
- 3.times do
67
- cars << car.clone
68
- cars.last.save
87
+ describe 'complex queries' do
88
+ before do
89
+ car.save
90
+ second_car.save
91
+ end
92
+
93
+ it 'with a single parameter' do
94
+ result = Car.where year: 2007
95
+ result.wont_be_empty
96
+ result.first.year.must_equal '2007'
97
+ end
98
+
99
+ it 'with multiple parameters' do
100
+ result = Car.where year: 2007, make: 'Chevrolet', model: 'F450'
101
+ result.wont_be_empty
102
+ result.first.year.must_equal '2007'
103
+ end
104
+
105
+ it 'with invalid parameter' do
106
+ result = Car.where year: 2008, make: 'Chevroletion'
107
+ result.must_be_empty
69
108
  end
70
- Car.find(cars.first).wont_be_nil
71
- Car.find(cars.first.id).must_be_instance_of Car
72
109
  end
73
110
  end
74
111
  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: 1.0.2
4
+ version: 1.2.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-01 00:00:00.000000000 Z
12
+ date: 2012-11-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -59,7 +59,7 @@ dependencies:
59
59
  - - ! '>='
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
- description: CSV Object-relational mapping
62
+ description: CSV Object-relational mapping for Ruby
63
63
  email:
64
64
  - lukasalexandre@gmail.com
65
65
  executables: []
@@ -116,8 +116,8 @@ rubyforge_project:
116
116
  rubygems_version: 1.8.24
117
117
  signing_key:
118
118
  specification_version: 3
119
- summary: CSV Record connects classes to CSV documents database to establish an almost
120
- zero-configuration persistence layer for applications.
119
+ summary: CSV Record connects Ruby classes to CSV documents database to establish an
120
+ almost zero-configuration persistence layer for applications.
121
121
  test_files:
122
122
  - test/csv_record/callbacks_test.rb
123
123
  - test/csv_record/connector_test.rb