csv_record 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -9,4 +9,4 @@ Rake::TestTask.new do |t|
9
9
  end
10
10
 
11
11
  desc "Run tests"
12
- task :default => :test
12
+ task default: :test
@@ -1,10 +1,8 @@
1
- require 'FileUtils'
2
-
3
1
  module CsvRecord
4
2
  module Connector
5
3
  def __initialize_db_directory__
6
4
  unless Dir.exists? 'db'
7
- FileUtils.mkdir_p('db')
5
+ Dir.mkdir 'db'
8
6
  end
9
7
  end
10
8
 
@@ -22,14 +20,14 @@ module CsvRecord
22
20
  end
23
21
 
24
22
  def __open_database_file__(mode='r')
25
- CSV.open(self.const_get('DATABASE_LOCATION'), mode, :headers => true) do |csv|
23
+ CSV.open(self.const_get('DATABASE_LOCATION'), mode, headers: true) do |csv|
26
24
  yield(csv)
27
25
  end
28
26
  end
29
27
 
30
28
  def __parse_database_file__
31
29
  open_database_file do |csv|
32
- CSV.open(self.const_get('DATABASE_LOCATION_TMP'), 'w', :headers => true) do |copy|
30
+ CSV.open(self.const_get('DATABASE_LOCATION_TMP'), 'w', headers: true) do |copy|
33
31
  copy << fields
34
32
  csv.entries.each do |entry|
35
33
  new_row = yield(entry)
@@ -1,6 +1,8 @@
1
1
  module CsvRecord
2
2
  module Reader
3
3
  module ClassMethods
4
+ DYNAMIC_FINDER_PATTERN = /^find_by_(.+)$/
5
+
4
6
  def __fields__
5
7
  instance_methods(false).select { |m| m.to_s !~ /=$/ }
6
8
  end
@@ -27,7 +29,7 @@ module CsvRecord
27
29
 
28
30
  def __find__(param)
29
31
  param = param.id unless param.is_a? Integer
30
- (__where__ :id => param).first
32
+ (__where__ id: param).first
31
33
  end
32
34
 
33
35
  def __where__(params)
@@ -56,7 +58,7 @@ module CsvRecord
56
58
  end
57
59
 
58
60
  def method_missing(meth, *args, &block)
59
- if meth.to_s =~ /^find_by_(.+)$/
61
+ if meth.to_s =~ DYNAMIC_FINDER_PATTERN
60
62
  dynamic_finder $1, *args, &block
61
63
  else
62
64
  super # You *must* call super if you don't handle the
@@ -65,6 +67,18 @@ module CsvRecord
65
67
  end
66
68
  end
67
69
 
70
+ def respond_to?(meth)
71
+ (meth.to_s =~ DYNAMIC_FINDER_PATTERN) || super
72
+ end
73
+
74
+ protected
75
+
76
+ def dynamic_finder(meth, *args, &block)
77
+ properties = meth.split '_and_'
78
+ conditions = Hash[properties.zip args]
79
+ __where__ conditions
80
+ end
81
+
68
82
  alias :fields :__fields__
69
83
  alias :find :__find__
70
84
  alias :count :__count__
@@ -1,3 +1,3 @@
1
1
  module CsvRecord
2
- VERSION = "1.2.0"
2
+ VERSION = "1.3.0"
3
3
  end
@@ -33,7 +33,7 @@ describe CsvRecord::Reader do
33
33
  end
34
34
 
35
35
  it "Check the current attributes" do
36
- expected_result = {:id=>nil, :created_at=>nil, :updated_at=>nil, :year=>1997, :make=>"Ford", :model=>"E350", :description=>"ac, abs, moon", :price=>3000.0}
36
+ expected_result = {id: nil, created_at: nil, updated_at: nil, year: 1997, make: 'Ford', model: 'E350', description: 'ac, abs, moon', price: 3000.0}
37
37
  car.attributes.must_equal expected_result
38
38
  end
39
39
 
@@ -106,6 +106,41 @@ describe CsvRecord::Reader do
106
106
  result = Car.where year: 2008, make: 'Chevroletion'
107
107
  result.must_be_empty
108
108
  end
109
+
110
+ end
111
+ describe 'dynamic finders' do
112
+ before do
113
+ car.save
114
+ second_car.save
115
+ end
116
+
117
+ let (:properties) { Car.fields }
118
+
119
+ it 'respond to certain dynamic methods' do
120
+ Car.must_respond_to "find_by_#{properties.sample}"
121
+ end
122
+
123
+ it 'finding with a single field' do
124
+ found_cars = Car.find_by_year 2007
125
+ found_cars.wont_be_empty
126
+ found_cars.first.must_be_instance_of Car
127
+ end
128
+
129
+ it 'finding with multiple fields' do
130
+ conditions = []
131
+ properties.each_with_index do |property, i|
132
+ values = []
133
+ i.times do |k|
134
+ conditions[i] = conditions[i] ? "#{conditions[i]}_and_#{properties[k]}" : properties[k]
135
+ values << car.send(properties[k])
136
+ end
137
+ if conditions[i]
138
+ found_cars = Car.public_send "find_by_#{conditions[i]}", *values
139
+ found_cars.wont_be_empty
140
+ found_cars.first.must_be_instance_of Car
141
+ end
142
+ end
143
+ end
109
144
  end
110
145
  end
111
146
  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.2.0
4
+ version: 1.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: