active_repository 0.2.6 → 0.2.7
Sign up to get free protection for your applications and to get access to all the features.
- data/active_repository.gemspec +1 -2
- data/lib/active_repository/base.rb +7 -3
- data/lib/active_repository/version.rb +1 -1
- data/lib/active_repository/write_support.rb +1 -1
- data/spec/active_repository/base_spec.rb +4 -4
- data/spec/spec_helper.rb +4 -0
- data/spec/support/shared_examples.rb +1 -1
- metadata +39 -11
- checksums.yaml +0 -15
- data/lib/active_repository/sql_query_executor.rb +0 -170
- data/spec/active_repository/sql_query_executor_spec.rb +0 -81
- data/spec/support/sql_query_shared_examples.rb +0 -325
data/active_repository.gemspec
CHANGED
@@ -24,14 +24,13 @@ Gem::Specification.new do |gem|
|
|
24
24
|
"Gemfile",
|
25
25
|
"spec/active_repository/base_spec.rb",
|
26
26
|
"spec/active_repository/associations_spec.rb",
|
27
|
-
"spec/active_repository/sql_query_executor_spec.rb",
|
28
27
|
"spec/support/shared_examples.rb",
|
29
|
-
"spec/support/sql_query_shared_examples.rb",
|
30
28
|
"spec/spec_helper.rb"
|
31
29
|
]
|
32
30
|
|
33
31
|
gem.add_runtime_dependency(%q<active_hash>, [">= 0.9.12"])
|
34
32
|
gem.add_runtime_dependency(%q<activemodel>, [">= 3.2.6"])
|
33
|
+
gem.add_runtime_dependency(%q<sql_query_executor>, [">= 0.0.1"])
|
35
34
|
gem.add_development_dependency(%q<rspec>, [">= 2.2.0"])
|
36
35
|
gem.add_development_dependency(%q<activerecord>, [">= 3.2.6"])
|
37
36
|
gem.add_development_dependency(%q<mongoid>, [">= 3.0.11"])
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'active_repository/associations'
|
2
2
|
require 'active_repository/uniqueness'
|
3
3
|
require 'active_repository/write_support'
|
4
|
-
require '
|
4
|
+
require 'sql_query_executor'
|
5
5
|
require 'active_repository/finders'
|
6
6
|
require 'active_repository/writers'
|
7
7
|
require 'active_repository/adapters/persistence_adapter'
|
@@ -138,7 +138,8 @@ module ActiveRepository
|
|
138
138
|
|
139
139
|
if repository?
|
140
140
|
args = args.first if args.try(:first).is_a?(Array)
|
141
|
-
|
141
|
+
query_executor = SqlQueryExecutor::Base.new(all)
|
142
|
+
query_executor.where(args)
|
142
143
|
else
|
143
144
|
objects = PersistenceAdapter.where(self, sanitize_args(args)).map do |object|
|
144
145
|
self.serialize!(object.attributes)
|
@@ -188,7 +189,10 @@ module ActiveRepository
|
|
188
189
|
# Updates attributes from self with the attributes from the parameters
|
189
190
|
def serialize!(attributes)
|
190
191
|
unless attributes.nil?
|
191
|
-
|
192
|
+
attributes.each do |key, value|
|
193
|
+
key = "id" if key == "_id"
|
194
|
+
self.send("#{key}=", (value.dup rescue value))
|
195
|
+
end
|
192
196
|
end
|
193
197
|
|
194
198
|
self.dup
|
@@ -16,7 +16,7 @@ describe ActiveRepository, "Base" do
|
|
16
16
|
Object.send :remove_const, :Country
|
17
17
|
end
|
18
18
|
|
19
|
-
context "in_memory" do
|
19
|
+
context "in_memory", :in_memory do
|
20
20
|
before do
|
21
21
|
Country.fields :name, :monarch, :language, :created_at, :updated_at
|
22
22
|
Country.set_model_class(Country)
|
@@ -60,7 +60,7 @@ describe ActiveRepository, "Base" do
|
|
60
60
|
it_behaves_like '.delete_all'
|
61
61
|
end
|
62
62
|
|
63
|
-
context "active_record" do
|
63
|
+
context "active_record", :active_record do
|
64
64
|
before do
|
65
65
|
Country.fields :name, :monarch, :language, :created_at, :updated_at
|
66
66
|
|
@@ -121,7 +121,7 @@ describe ActiveRepository, "Base" do
|
|
121
121
|
it_behaves_like '.delete_all'
|
122
122
|
end
|
123
123
|
|
124
|
-
context "mongoid" do
|
124
|
+
context "mongoid", :mongoid do
|
125
125
|
before do
|
126
126
|
Country.fields :name, :monarch, :language, :created_at, :updated_at
|
127
127
|
|
@@ -135,7 +135,7 @@ describe ActiveRepository, "Base" do
|
|
135
135
|
field :name
|
136
136
|
field :monarch
|
137
137
|
field :language
|
138
|
-
field :
|
138
|
+
field :id, type: Integer
|
139
139
|
field :updated_at
|
140
140
|
field :created_at
|
141
141
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -563,7 +563,7 @@ shared_examples ".create" do
|
|
563
563
|
|
564
564
|
country2 = Country.new :name => "bar"
|
565
565
|
country2.save
|
566
|
-
country2.id.should == ((country1.id.class.name == "BSON::ObjectId") ? country2.id : country1.id + 1)
|
566
|
+
country2.id.should == ((country1.id.class.name == "Moped::BSON::ObjectId") ? country2.id : country1.id + 1)
|
567
567
|
end
|
568
568
|
|
569
569
|
it "adds the new object to the data collection" do
|
metadata
CHANGED
@@ -1,18 +1,20 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_repository
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.7
|
5
|
+
prerelease:
|
5
6
|
platform: ruby
|
6
7
|
authors:
|
7
8
|
- Caio Torres
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
|
-
date:
|
12
|
+
date: 2014-01-18 00:00:00.000000000 Z
|
12
13
|
dependencies:
|
13
14
|
- !ruby/object:Gem::Dependency
|
14
15
|
name: active_hash
|
15
16
|
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
16
18
|
requirements:
|
17
19
|
- - ! '>='
|
18
20
|
- !ruby/object:Gem::Version
|
@@ -20,6 +22,7 @@ dependencies:
|
|
20
22
|
type: :runtime
|
21
23
|
prerelease: false
|
22
24
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
23
26
|
requirements:
|
24
27
|
- - ! '>='
|
25
28
|
- !ruby/object:Gem::Version
|
@@ -27,6 +30,7 @@ dependencies:
|
|
27
30
|
- !ruby/object:Gem::Dependency
|
28
31
|
name: activemodel
|
29
32
|
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
30
34
|
requirements:
|
31
35
|
- - ! '>='
|
32
36
|
- !ruby/object:Gem::Version
|
@@ -34,13 +38,31 @@ dependencies:
|
|
34
38
|
type: :runtime
|
35
39
|
prerelease: false
|
36
40
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
37
42
|
requirements:
|
38
43
|
- - ! '>='
|
39
44
|
- !ruby/object:Gem::Version
|
40
45
|
version: 3.2.6
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: sql_query_executor
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 0.0.1
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.0.1
|
41
62
|
- !ruby/object:Gem::Dependency
|
42
63
|
name: rspec
|
43
64
|
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
44
66
|
requirements:
|
45
67
|
- - ! '>='
|
46
68
|
- !ruby/object:Gem::Version
|
@@ -48,6 +70,7 @@ dependencies:
|
|
48
70
|
type: :development
|
49
71
|
prerelease: false
|
50
72
|
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
51
74
|
requirements:
|
52
75
|
- - ! '>='
|
53
76
|
- !ruby/object:Gem::Version
|
@@ -55,6 +78,7 @@ dependencies:
|
|
55
78
|
- !ruby/object:Gem::Dependency
|
56
79
|
name: activerecord
|
57
80
|
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
58
82
|
requirements:
|
59
83
|
- - ! '>='
|
60
84
|
- !ruby/object:Gem::Version
|
@@ -62,6 +86,7 @@ dependencies:
|
|
62
86
|
type: :development
|
63
87
|
prerelease: false
|
64
88
|
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
65
90
|
requirements:
|
66
91
|
- - ! '>='
|
67
92
|
- !ruby/object:Gem::Version
|
@@ -69,6 +94,7 @@ dependencies:
|
|
69
94
|
- !ruby/object:Gem::Dependency
|
70
95
|
name: mongoid
|
71
96
|
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
72
98
|
requirements:
|
73
99
|
- - ! '>='
|
74
100
|
- !ruby/object:Gem::Version
|
@@ -76,6 +102,7 @@ dependencies:
|
|
76
102
|
type: :development
|
77
103
|
prerelease: false
|
78
104
|
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
79
106
|
requirements:
|
80
107
|
- - ! '>='
|
81
108
|
- !ruby/object:Gem::Version
|
@@ -83,6 +110,7 @@ dependencies:
|
|
83
110
|
- !ruby/object:Gem::Dependency
|
84
111
|
name: rake
|
85
112
|
requirement: !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
86
114
|
requirements:
|
87
115
|
- - ! '>='
|
88
116
|
- !ruby/object:Gem::Version
|
@@ -90,6 +118,7 @@ dependencies:
|
|
90
118
|
type: :development
|
91
119
|
prerelease: false
|
92
120
|
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
none: false
|
93
122
|
requirements:
|
94
123
|
- - ! '>='
|
95
124
|
- !ruby/object:Gem::Version
|
@@ -97,6 +126,7 @@ dependencies:
|
|
97
126
|
- !ruby/object:Gem::Dependency
|
98
127
|
name: coveralls
|
99
128
|
requirement: !ruby/object:Gem::Requirement
|
129
|
+
none: false
|
100
130
|
requirements:
|
101
131
|
- - ! '>='
|
102
132
|
- !ruby/object:Gem::Version
|
@@ -104,6 +134,7 @@ dependencies:
|
|
104
134
|
type: :development
|
105
135
|
prerelease: false
|
106
136
|
version_requirements: !ruby/object:Gem::Requirement
|
137
|
+
none: false
|
107
138
|
requirements:
|
108
139
|
- - ! '>='
|
109
140
|
- !ruby/object:Gem::Version
|
@@ -111,6 +142,7 @@ dependencies:
|
|
111
142
|
- !ruby/object:Gem::Dependency
|
112
143
|
name: sqlite3
|
113
144
|
requirement: !ruby/object:Gem::Requirement
|
145
|
+
none: false
|
114
146
|
requirements:
|
115
147
|
- - ! '>='
|
116
148
|
- !ruby/object:Gem::Version
|
@@ -118,6 +150,7 @@ dependencies:
|
|
118
150
|
type: :development
|
119
151
|
prerelease: false
|
120
152
|
version_requirements: !ruby/object:Gem::Requirement
|
153
|
+
none: false
|
121
154
|
requirements:
|
122
155
|
- - ! '>='
|
123
156
|
- !ruby/object:Gem::Version
|
@@ -139,7 +172,6 @@ files:
|
|
139
172
|
- lib/active_repository/associations.rb
|
140
173
|
- lib/active_repository/base.rb
|
141
174
|
- lib/active_repository/finders.rb
|
142
|
-
- lib/active_repository/sql_query_executor.rb
|
143
175
|
- lib/active_repository/uniqueness.rb
|
144
176
|
- lib/active_repository/version.rb
|
145
177
|
- lib/active_repository/write_support.rb
|
@@ -148,40 +180,36 @@ files:
|
|
148
180
|
- Gemfile
|
149
181
|
- spec/active_repository/base_spec.rb
|
150
182
|
- spec/active_repository/associations_spec.rb
|
151
|
-
- spec/active_repository/sql_query_executor_spec.rb
|
152
183
|
- spec/support/shared_examples.rb
|
153
|
-
- spec/support/sql_query_shared_examples.rb
|
154
184
|
- spec/spec_helper.rb
|
155
185
|
homepage: http://github.com/efreesen/active_repository
|
156
186
|
licenses:
|
157
187
|
- GPL
|
158
|
-
metadata: {}
|
159
188
|
post_install_message:
|
160
189
|
rdoc_options: []
|
161
190
|
require_paths:
|
162
191
|
- lib
|
163
192
|
required_ruby_version: !ruby/object:Gem::Requirement
|
193
|
+
none: false
|
164
194
|
requirements:
|
165
195
|
- - ! '>='
|
166
196
|
- !ruby/object:Gem::Version
|
167
197
|
version: '0'
|
168
198
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
199
|
+
none: false
|
169
200
|
requirements:
|
170
201
|
- - ! '>='
|
171
202
|
- !ruby/object:Gem::Version
|
172
203
|
version: '0'
|
173
204
|
requirements: []
|
174
205
|
rubyforge_project:
|
175
|
-
rubygems_version:
|
206
|
+
rubygems_version: 1.8.23
|
176
207
|
signing_key:
|
177
|
-
specification_version:
|
208
|
+
specification_version: 3
|
178
209
|
summary: An implementation of repository pattern that can connect with any ORM
|
179
210
|
test_files:
|
180
211
|
- Gemfile
|
181
212
|
- spec/active_repository/base_spec.rb
|
182
213
|
- spec/active_repository/associations_spec.rb
|
183
|
-
- spec/active_repository/sql_query_executor_spec.rb
|
184
214
|
- spec/support/shared_examples.rb
|
185
|
-
- spec/support/sql_query_shared_examples.rb
|
186
215
|
- spec/spec_helper.rb
|
187
|
-
has_rdoc:
|
checksums.yaml
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
---
|
2
|
-
!binary "U0hBMQ==":
|
3
|
-
metadata.gz: !binary |-
|
4
|
-
MGU1NzgzYmJjNzk1OWFkY2M0ZjYwMWFiOTBmNjJjYzY4MDBlMjk5OQ==
|
5
|
-
data.tar.gz: !binary |-
|
6
|
-
OGJkZDk3ZGUyNDBmOWY4NGU0MmU2ZTE5Y2M5ZDYxNWYxYzY3YTI2ZQ==
|
7
|
-
!binary "U0hBNTEy":
|
8
|
-
metadata.gz: !binary |-
|
9
|
-
NGZmZjQ3ZmIzY2Q1Y2Q3ZmQ1NTVlNzQyNTU4MDUxNzgzNzdiOGExMjAxMDdi
|
10
|
-
NzViZWI4MjNjZTAxZTNkOWRkNWZlMDcxZmExNDQ0OGU1MWQyODdlNTAzNDNm
|
11
|
-
MmMxODhhOTMyMTAwMmEzNWExYjAzNjIxMjRhYWYzNjU4Nzk0M2U=
|
12
|
-
data.tar.gz: !binary |-
|
13
|
-
YjEwYmRiNDMwMzBiYmI1NWQ0Y2VhMTE2YzliMzZkOTY0YmE2MDAzZjhlNzZl
|
14
|
-
NzUyMjBlMjVmNTgxNzljNzUzZDdiY2NjMTYxMDMwYmM1M2I4NjJiZDU1M2Jj
|
15
|
-
ZmVlYTViYjlhZTg3MjFkZjJlYzA5ZTA5YjBjNDNhYTkwZTJlOWI=
|
@@ -1,170 +0,0 @@
|
|
1
|
-
# Simulates a SQL where clause to filter objects from the database
|
2
|
-
module ActiveHash #:nodoc:
|
3
|
-
class SQLQueryExecutor #:nodoc:
|
4
|
-
class << self #:nodoc:
|
5
|
-
# Prepares query by replacing all ? by it's real values in #args
|
6
|
-
def args_to_query(args)
|
7
|
-
return args.first if args.size == 1
|
8
|
-
|
9
|
-
query = args.first
|
10
|
-
param = args.delete_at(1)
|
11
|
-
|
12
|
-
param = convert_param(param)
|
13
|
-
|
14
|
-
args[0] = query.sub("?", param)
|
15
|
-
|
16
|
-
args_to_query(args)
|
17
|
-
end
|
18
|
-
|
19
|
-
# Recursive method that divides the query in sub queries, executes each part individually
|
20
|
-
# and finally relates its results as specified in the query.
|
21
|
-
def execute(klass, query)
|
22
|
-
query = query.gsub(/\[.*?\]/) { |substr| substr.gsub(' ', '') }
|
23
|
-
|
24
|
-
@operator, @sub_query, @objects = process_first(klass, query, query.split(" ")[1].downcase)
|
25
|
-
|
26
|
-
@operator.nil? ? @objects : @objects.send(@operator, execute(klass, @sub_query)).sort_by{ |o| o.id }
|
27
|
-
end
|
28
|
-
|
29
|
-
private
|
30
|
-
# Splits the first sub query from the rest of the query and returns it.
|
31
|
-
def divide_query
|
32
|
-
array = @query.split(" ")
|
33
|
-
case @operator
|
34
|
-
when "between"
|
35
|
-
array[0..5]
|
36
|
-
when "is"
|
37
|
-
size = array[2] == "not" ? 4 : 3
|
38
|
-
array[0..size]
|
39
|
-
else
|
40
|
-
array[0..3]
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
# Replaces white spaces for underscores inside quotes in order to avoid getting parameters
|
45
|
-
# split into separate components of the query.
|
46
|
-
def convert_attrs(field, *attrs)
|
47
|
-
attrs.each_with_index do |attribute, i|
|
48
|
-
attribute = attribute.gsub("_", " ") rescue attribute
|
49
|
-
attrs[i] = field.is_a?(Integer) ? attribute.to_i : attribute
|
50
|
-
end
|
51
|
-
|
52
|
-
field = field.is_a?(Integer) ? field : field.to_s
|
53
|
-
|
54
|
-
[field, attrs].flatten
|
55
|
-
end
|
56
|
-
|
57
|
-
# Returns converted #param based on its Class, so it can be used on the query
|
58
|
-
def convert_param(param)
|
59
|
-
case param.class.name
|
60
|
-
when "String"
|
61
|
-
param = "'#{param}'"
|
62
|
-
when "Date"
|
63
|
-
param = "'#{param.strftime("%Y-%m-%d")}'"
|
64
|
-
when "Time"
|
65
|
-
param = "'#{param.strftime("%Y-%m-%d %H:%M:%S %z")}'"
|
66
|
-
else
|
67
|
-
param = param.to_s
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
# Execute SQL between filter
|
72
|
-
def execute_between(klass, sub_query)
|
73
|
-
klass.all.select do |o|
|
74
|
-
field = sub_query.first.gsub('(', '')
|
75
|
-
field, first_attr, second_attr = convert_attrs(o.send(field), sub_query[2], sub_query[4])
|
76
|
-
|
77
|
-
(field >= first_attr && field <= second_attr)
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
# Executes SQL is filter
|
82
|
-
def execute_is(klass, sub_query)
|
83
|
-
klass.all.select do |o|
|
84
|
-
field = o.send(sub_query.first).blank?
|
85
|
-
|
86
|
-
sub_query.size == 3 ? field : !field
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
# Executes SQL is filter
|
91
|
-
def execute_in(klass, sub_query)
|
92
|
-
klass.all.select do |o|
|
93
|
-
field = o.send(sub_query.first)
|
94
|
-
values = sub_query[2].gsub(/\(|\[|\]|\)/, '').split(/,|, /)
|
95
|
-
|
96
|
-
values.include?(field.to_s)
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
|
-
# Executes the #sub_quey defined operator filter
|
101
|
-
def execute_operator(klass, sub_query)
|
102
|
-
klass.all.select do |o|
|
103
|
-
query = sub_query.first
|
104
|
-
|
105
|
-
if query
|
106
|
-
field, attribute = convert_attrs(o.send(query.gsub(/[\(\)]/, "")), sub_query[2])
|
107
|
-
|
108
|
-
field.blank? ? false : field.send(@operator, attribute)
|
109
|
-
else
|
110
|
-
false
|
111
|
-
end
|
112
|
-
end
|
113
|
-
end
|
114
|
-
|
115
|
-
# Executes the #sub_query
|
116
|
-
def execute_sub_query(klass, sub_query)
|
117
|
-
case @operator
|
118
|
-
when "between"
|
119
|
-
execute_between(klass, sub_query)
|
120
|
-
when "is"
|
121
|
-
execute_is(klass, sub_query)
|
122
|
-
when "in"
|
123
|
-
execute_in(klass, sub_query)
|
124
|
-
else
|
125
|
-
execute_operator(klass, sub_query)
|
126
|
-
end
|
127
|
-
end
|
128
|
-
|
129
|
-
# Converts SQL where clause sub query operator to its Ruby Array counterpart
|
130
|
-
def get_operator(attributes)
|
131
|
-
operator = attributes.size >= 4 ? attributes.last : nil
|
132
|
-
|
133
|
-
case operator.try(:downcase)
|
134
|
-
when "or" then "+"
|
135
|
-
when "and" then "&"
|
136
|
-
else nil
|
137
|
-
end
|
138
|
-
end
|
139
|
-
|
140
|
-
# Processes the first sub query in query
|
141
|
-
def process_first(klass, query, operator)
|
142
|
-
@operator = (operator == "=" ? "==" : (operator == '<>' ? '!=' : operator))
|
143
|
-
@query = sanitize_query(query)
|
144
|
-
sub_query = divide_query
|
145
|
-
|
146
|
-
binding_operator = get_operator(sub_query)
|
147
|
-
|
148
|
-
objects = execute_sub_query(klass, sub_query)
|
149
|
-
|
150
|
-
query_array = query.split(' ')
|
151
|
-
|
152
|
-
sub_query = query_array[sub_query.size..query_array.size].join(' ')
|
153
|
-
|
154
|
-
[binding_operator, sub_query, objects]
|
155
|
-
end
|
156
|
-
|
157
|
-
# Removes all accents and other non default characters
|
158
|
-
def sanitize_query(query)
|
159
|
-
new_query = query
|
160
|
-
params = query.scan(/([\"'])(.*?)\1/)
|
161
|
-
|
162
|
-
params.each do |quote, param|
|
163
|
-
new_query = new_query.gsub(quote,"").gsub(param, param.gsub(" ", "_"))
|
164
|
-
end
|
165
|
-
|
166
|
-
new_query
|
167
|
-
end
|
168
|
-
end
|
169
|
-
end
|
170
|
-
end
|
@@ -1,81 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'support/sql_query_shared_examples'
|
3
|
-
|
4
|
-
require 'active_repository'
|
5
|
-
require "active_record"
|
6
|
-
|
7
|
-
describe ActiveRepository, "Base" do
|
8
|
-
|
9
|
-
before do
|
10
|
-
class Country < ActiveRepository::Base
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
after do
|
15
|
-
Object.send :remove_const, :Country
|
16
|
-
end
|
17
|
-
|
18
|
-
context "in_memory" do
|
19
|
-
before do
|
20
|
-
Country.fields :name, :monarch, :language, :founded_at
|
21
|
-
Country.set_model_class(Country)
|
22
|
-
Country.set_save_in_memory(true)
|
23
|
-
|
24
|
-
Country.create(:id => 1, :name => "US", :language => 'English')
|
25
|
-
Country.create(:id => 2, :name => "Canada", :language => 'English', :monarch => "The Crown of England")
|
26
|
-
Country.create(:id => 3, :name => "Mexico", :language => 'Spanish')
|
27
|
-
Country.create(:id => 4, :name => "UK", :language => 'English', :monarch => "The Crown of England")
|
28
|
-
Country.create(:id => 5, :name => "Brazil", :founded_at => Time.parse('1500-04-22 13:34:25'))
|
29
|
-
end
|
30
|
-
|
31
|
-
describe ".where" do
|
32
|
-
it_behaves_like '='
|
33
|
-
it_behaves_like '>'
|
34
|
-
it_behaves_like '>='
|
35
|
-
it_behaves_like '<'
|
36
|
-
it_behaves_like '<='
|
37
|
-
it_behaves_like 'between'
|
38
|
-
it_behaves_like 'is'
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
context "active_record" do
|
43
|
-
before do
|
44
|
-
Country.fields :name, :monarch, :language, :founded_at
|
45
|
-
|
46
|
-
class CountryModel < ActiveRecord::Base
|
47
|
-
self.table_name = 'countries'
|
48
|
-
establish_connection :adapter => "sqlite3", :database => ":memory:"
|
49
|
-
connection.create_table(:countries, :force => true) do |t|
|
50
|
-
t.string :name
|
51
|
-
t.string :monarch
|
52
|
-
t.string :language
|
53
|
-
t.datetime :founded_at
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
Country.set_model_class(CountryModel)
|
58
|
-
Country.set_save_in_memory(false)
|
59
|
-
|
60
|
-
Country.create(:id => 1, :name => "US", :language => 'English')
|
61
|
-
Country.create(:id => 2, :name => "Canada", :language => 'English', :monarch => "The Crown of England")
|
62
|
-
Country.create(:id => 3, :name => "Mexico", :language => 'Spanish')
|
63
|
-
Country.create(:id => 4, :name => "UK", :language => 'English', :monarch => "The Crown of England")
|
64
|
-
Country.create(:id => 5, :name => "Brazil", :founded_at => Time.parse('1500-04-22 13:34:25'))
|
65
|
-
end
|
66
|
-
|
67
|
-
after do
|
68
|
-
Object.send :remove_const, :CountryModel
|
69
|
-
end
|
70
|
-
|
71
|
-
describe ".where" do
|
72
|
-
it_behaves_like '='
|
73
|
-
it_behaves_like '>'
|
74
|
-
it_behaves_like '>='
|
75
|
-
it_behaves_like '<'
|
76
|
-
it_behaves_like '<='
|
77
|
-
it_behaves_like 'between'
|
78
|
-
it_behaves_like 'is'
|
79
|
-
end
|
80
|
-
end
|
81
|
-
end
|
@@ -1,325 +0,0 @@
|
|
1
|
-
require 'set'
|
2
|
-
|
3
|
-
shared_examples "=" do
|
4
|
-
context "when attribute is string" do
|
5
|
-
it "matches a record" do
|
6
|
-
record = Country.where("name = 'US'")
|
7
|
-
record.count.should == 1
|
8
|
-
record.first.id.should == 1
|
9
|
-
record.first.name.should == 'US'
|
10
|
-
end
|
11
|
-
|
12
|
-
it "doesn't match any record" do
|
13
|
-
record = Country.where("name = 'Argentina'")
|
14
|
-
record.count.should == 0
|
15
|
-
record.should == []
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
context "when attribute is integer" do
|
20
|
-
it "matches a record" do
|
21
|
-
record = Country.where("id = 1")
|
22
|
-
record.count.should == 1
|
23
|
-
record.first.id.should == 1
|
24
|
-
record.first.name.should == 'US'
|
25
|
-
end
|
26
|
-
|
27
|
-
it "doesn't match any record" do
|
28
|
-
record = Country.where("id = 43")
|
29
|
-
record.count.should == 0
|
30
|
-
record.should == []
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
context "when attribute is datetime" do
|
35
|
-
it "matches a record" do
|
36
|
-
record = Country.where("founded_at = ?", Time.parse('1500-04-22 13:34:25'))
|
37
|
-
record.count.should == 1
|
38
|
-
record.first.id.should == 5
|
39
|
-
record.first.name.should == 'Brazil'
|
40
|
-
end
|
41
|
-
|
42
|
-
it "doesn't match any record" do
|
43
|
-
record = Country.where("id = ?", Time.parse('1500-09-07 13:34:25'))
|
44
|
-
record.count.should == 0
|
45
|
-
record.should == []
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
shared_examples ">" do
|
51
|
-
context "when attribute is a string" do
|
52
|
-
it "matches a record" do
|
53
|
-
records = Country.where("name > 'T'")
|
54
|
-
records.count.should == 2
|
55
|
-
records.first.id.should == 1
|
56
|
-
records.should == [Country.find(1), Country.find(4)]
|
57
|
-
end
|
58
|
-
|
59
|
-
it "doesn't match any record" do
|
60
|
-
record = Country.where("name > 'Z'")
|
61
|
-
record.count.should == 0
|
62
|
-
record.should == []
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
context "when attribute is integer" do
|
67
|
-
it "matches a record" do
|
68
|
-
records = Country.where("id > 3")
|
69
|
-
records.count.should == 2
|
70
|
-
records.first.id.should == 4
|
71
|
-
records.should == [Country.find(4), Country.find(5)]
|
72
|
-
end
|
73
|
-
|
74
|
-
it "doesn't match any record" do
|
75
|
-
record = Country.where("id > 5")
|
76
|
-
record.count.should == 0
|
77
|
-
record.should == []
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
context "when attribute is datetime" do
|
82
|
-
it "matches a record" do
|
83
|
-
record = Country.where("founded_at > ?", Time.parse('1500-04-20 13:34:25'))
|
84
|
-
record.count.should == 1
|
85
|
-
record.first.id.should == 5
|
86
|
-
record.first.name.should == 'Brazil'
|
87
|
-
end
|
88
|
-
|
89
|
-
it "doesn't match any record" do
|
90
|
-
record = Country.where("founded_at > ?", Time.parse('1500-04-23 13:34:25'))
|
91
|
-
record.count.should == 0
|
92
|
-
record.should == []
|
93
|
-
end
|
94
|
-
end
|
95
|
-
end
|
96
|
-
|
97
|
-
shared_examples ">=" do
|
98
|
-
context "when attribute is a string" do
|
99
|
-
it "matches a record" do
|
100
|
-
records = Country.where("name >= 'U'")
|
101
|
-
records.count.should == 2
|
102
|
-
records.first.id.should == 1
|
103
|
-
records.should == [Country.find(1), Country.find(4)]
|
104
|
-
end
|
105
|
-
|
106
|
-
it "doesn't match any record" do
|
107
|
-
record = Country.where("name >= 'Z'")
|
108
|
-
record.count.should == 0
|
109
|
-
record.should == []
|
110
|
-
end
|
111
|
-
end
|
112
|
-
|
113
|
-
context "when attribute is integer" do
|
114
|
-
it "matches a record" do
|
115
|
-
records = Country.where("id >= 4")
|
116
|
-
records.count.should == 2
|
117
|
-
records.first.id.should == 4
|
118
|
-
records.should == [Country.find(4), Country.find(5)]
|
119
|
-
end
|
120
|
-
|
121
|
-
it "doesn't match any record" do
|
122
|
-
record = Country.where("id >= 6")
|
123
|
-
record.count.should == 0
|
124
|
-
record.should == []
|
125
|
-
end
|
126
|
-
end
|
127
|
-
|
128
|
-
context "when attribute is datetime" do
|
129
|
-
it "matches a record" do
|
130
|
-
record = Country.where("founded_at >= ?", Time.parse('1500-04-22 13:34:25'))
|
131
|
-
record.count.should == 1
|
132
|
-
record.first.id.should == 5
|
133
|
-
record.first.name.should == 'Brazil'
|
134
|
-
end
|
135
|
-
|
136
|
-
it "doesn't match any record" do
|
137
|
-
record = Country.where("founded_at >= ?", Time.parse('1500-04-23 13:34:25'))
|
138
|
-
record.count.should == 0
|
139
|
-
record.should == []
|
140
|
-
end
|
141
|
-
end
|
142
|
-
end
|
143
|
-
|
144
|
-
shared_examples "<" do
|
145
|
-
context "when attribute is a string" do
|
146
|
-
it "matches a record" do
|
147
|
-
records = Country.where("name < 'C'")
|
148
|
-
records.count.should == 1
|
149
|
-
records.first.id.should == 5
|
150
|
-
records.should == [Country.find(5)]
|
151
|
-
end
|
152
|
-
|
153
|
-
it "doesn't match any record" do
|
154
|
-
record = Country.where("name < 'B'")
|
155
|
-
record.count.should == 0
|
156
|
-
record.should == []
|
157
|
-
end
|
158
|
-
end
|
159
|
-
|
160
|
-
context "when attribute is integer" do
|
161
|
-
it "matches a record" do
|
162
|
-
records = Country.where("id < 3")
|
163
|
-
records.count.should == 2
|
164
|
-
records.first.id.should == 1
|
165
|
-
records.should == [Country.find(1), Country.find(2)]
|
166
|
-
end
|
167
|
-
|
168
|
-
it "doesn't match any record" do
|
169
|
-
record = Country.where("id < 1")
|
170
|
-
record.count.should == 0
|
171
|
-
record.should == []
|
172
|
-
end
|
173
|
-
end
|
174
|
-
|
175
|
-
context "when attribute is datetime" do
|
176
|
-
it "matches a record" do
|
177
|
-
record = Country.where("founded_at < ?", Time.parse('1500-04-22 13:34:26'))
|
178
|
-
record.count.should == 1
|
179
|
-
record.first.id.should == 5
|
180
|
-
record.first.name.should == 'Brazil'
|
181
|
-
end
|
182
|
-
|
183
|
-
it "doesn't match any record" do
|
184
|
-
record = Country.where("founded_at < ?", Time.parse('1500-04-22 13:34:25'))
|
185
|
-
record.count.should == 0
|
186
|
-
record.should == []
|
187
|
-
end
|
188
|
-
end
|
189
|
-
end
|
190
|
-
|
191
|
-
shared_examples "<=" do
|
192
|
-
context "when attribute is a string" do
|
193
|
-
it "matches a record" do
|
194
|
-
records = Country.where("name <= 'Brb'")
|
195
|
-
records.count.should == 1
|
196
|
-
records.first.id.should == 5
|
197
|
-
records.should == [Country.find(5)]
|
198
|
-
end
|
199
|
-
|
200
|
-
it "doesn't match any record" do
|
201
|
-
record = Country.where("name <= 'A'")
|
202
|
-
record.count.should == 0
|
203
|
-
record.should == []
|
204
|
-
end
|
205
|
-
end
|
206
|
-
|
207
|
-
context "when attribute is integer" do
|
208
|
-
it "matches a record" do
|
209
|
-
records = Country.where("id <= 2")
|
210
|
-
records.count.should == 2
|
211
|
-
records.first.id.should == 1
|
212
|
-
records.should == [Country.find(1), Country.find(2)]
|
213
|
-
end
|
214
|
-
|
215
|
-
it "doesn't match any record" do
|
216
|
-
record = Country.where("id <= 0")
|
217
|
-
record.count.should == 0
|
218
|
-
record.should == []
|
219
|
-
end
|
220
|
-
end
|
221
|
-
|
222
|
-
context "when attribute is datetime" do
|
223
|
-
it "matches a record" do
|
224
|
-
record = Country.where("founded_at <= ?", Time.parse('1500-04-22 13:34:25'))
|
225
|
-
record.count.should == 1
|
226
|
-
record.first.id.should == 5
|
227
|
-
record.first.name.should == 'Brazil'
|
228
|
-
end
|
229
|
-
|
230
|
-
it "doesn't match any record" do
|
231
|
-
record = Country.where("founded_at <= ?", Time.parse('1500-04-22 13:34:24'))
|
232
|
-
record.count.should == 0
|
233
|
-
record.should == []
|
234
|
-
end
|
235
|
-
end
|
236
|
-
end
|
237
|
-
|
238
|
-
shared_examples "between" do
|
239
|
-
context "when attribute is a string" do
|
240
|
-
it "matches a record" do
|
241
|
-
records = Country.where("name between 'A' and 'C'")
|
242
|
-
records.count.should == 1
|
243
|
-
records.first.id.should == 5
|
244
|
-
records.should == [Country.find(5)]
|
245
|
-
end
|
246
|
-
|
247
|
-
it "doesn't match any record" do
|
248
|
-
record = Country.where("name between 'K' and 'M'")
|
249
|
-
record.count.should == 0
|
250
|
-
record.should == []
|
251
|
-
end
|
252
|
-
end
|
253
|
-
|
254
|
-
context "when attribute is integer" do
|
255
|
-
it "matches a record" do
|
256
|
-
records = Country.where("id between 1 and 2")
|
257
|
-
records.count.should == 2
|
258
|
-
records.first.id.should == 1
|
259
|
-
records.should == [Country.find(1), Country.find(2)]
|
260
|
-
end
|
261
|
-
|
262
|
-
it "doesn't match any record" do
|
263
|
-
record = Country.where("id between 6 and 10")
|
264
|
-
record.count.should == 0
|
265
|
-
record.should == []
|
266
|
-
end
|
267
|
-
end
|
268
|
-
|
269
|
-
context "when attribute is datetime" do
|
270
|
-
it "matches a record" do
|
271
|
-
record = Country.where("founded_at between ? and ?", Time.parse('1500-04-22 13:34:24'), Time.parse('1500-04-22 13:34:26'))
|
272
|
-
record.count.should == 1
|
273
|
-
record.first.id.should == 5
|
274
|
-
record.first.name.should == 'Brazil'
|
275
|
-
end
|
276
|
-
|
277
|
-
it "doesn't match any record" do
|
278
|
-
record = Country.where("founded_at between ? and ?", Time.parse('1500-04-22 13:34:26'), Time.parse('1500-09-22 13:34:25'))
|
279
|
-
record.count.should == 0
|
280
|
-
record.should == []
|
281
|
-
end
|
282
|
-
end
|
283
|
-
end
|
284
|
-
|
285
|
-
shared_examples "is" do
|
286
|
-
it "attribute is condition" do
|
287
|
-
records = Country.where("founded_at is null")
|
288
|
-
records.count.should == 4
|
289
|
-
records.first.id.should == 1
|
290
|
-
records.should == [Country.find(1), Country.find(2), Country.find(3), Country.find(4)]
|
291
|
-
end
|
292
|
-
|
293
|
-
it "attribute is not condition" do
|
294
|
-
id = Country.last.id
|
295
|
-
records = Country.where("founded_at is not null")
|
296
|
-
records.count.should == 1
|
297
|
-
records.first.id.should == id
|
298
|
-
records.should == [Country.find(id)]
|
299
|
-
end
|
300
|
-
end
|
301
|
-
|
302
|
-
shared_examples "and" do
|
303
|
-
it "attribute and condition" do
|
304
|
-
records = Country.where("language = 'English' and monarch = 'The Crown of England'")
|
305
|
-
records.count.should == 2
|
306
|
-
records.first.id.should == 2
|
307
|
-
records.should == [Country.find(2), Country.find(4)]
|
308
|
-
end
|
309
|
-
|
310
|
-
it "integer attribute and condition" do
|
311
|
-
records = Country.where("id = 2 and language = 'English'")
|
312
|
-
records.count.should == 2
|
313
|
-
records.first.id.should == 2
|
314
|
-
records.should == [Country.find(2), Country.find(4)]
|
315
|
-
end
|
316
|
-
end
|
317
|
-
|
318
|
-
shared_examples "or" do
|
319
|
-
it "attribute or condition" do
|
320
|
-
records = Country.where("language = 'English' or language = 'Spanish'")
|
321
|
-
records.count.should == 4
|
322
|
-
records.first.id.should == 1
|
323
|
-
records.should == [Country.find(1), Country.find(2), Country.find(3), Country.find(4)]
|
324
|
-
end
|
325
|
-
end
|