devlin 0.0.1 → 0.0.2
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 +12 -2
- data/lib/devlin/query.rb +15 -1
- data/lib/devlin/version.rb +1 -1
- data/spec/query_spec.rb +10 -1
- data/spec/setup_spec.rb +1 -1
- data/spec/spec_helper.rb +2 -2
- data/spec/test.sqlite3 +0 -0
- metadata +2 -2
data/README.md
CHANGED
@@ -58,12 +58,22 @@ can look like this:
|
|
58
58
|
- earnings
|
59
59
|
conditions:
|
60
60
|
year: 2012
|
61
|
+
month.geq: 1
|
62
|
+
month.leq: 3
|
61
63
|
group:
|
62
64
|
- manufacturer
|
63
65
|
- month
|
64
66
|
|
65
|
-
The generated query returns a sum of earnings for each manufacturer
|
66
|
-
|
67
|
+
The generated query returns a sum of earnings for each manufacturer
|
68
|
+
in the year 2012 and the months of January, February and Mach.
|
69
|
+
|
70
|
+
Possible modifiers for conditions are
|
71
|
+
|
72
|
+
g # > ...
|
73
|
+
geq # >= ...
|
74
|
+
l # < ...
|
75
|
+
leq # <= ...
|
76
|
+
in # IN (...)
|
67
77
|
|
68
78
|
## Todo
|
69
79
|
|
data/lib/devlin/query.rb
CHANGED
@@ -17,7 +17,21 @@ module Devlin
|
|
17
17
|
res = @scope.relation
|
18
18
|
res = res.select(self.select.map { |c| @scope.column(c).select_definition })
|
19
19
|
@conditions.each do |col, val|
|
20
|
-
|
20
|
+
col, op = col.split('.')
|
21
|
+
res = case op
|
22
|
+
when 'g'
|
23
|
+
res.where(["#{@scope.column(col).definition}>?", val])
|
24
|
+
when 'geq'
|
25
|
+
res.where(["#{@scope.column(col).definition}>=?", val])
|
26
|
+
when 'l'
|
27
|
+
res.where(["#{@scope.column(col).definition}<?", val])
|
28
|
+
when 'leq'
|
29
|
+
res.where(["#{@scope.column(col).definition}<=?", val])
|
30
|
+
when 'in'
|
31
|
+
res.where(["#{@scope.column(col).definition} IN (?)", val])
|
32
|
+
else
|
33
|
+
res.where(["#{@scope.column(col).definition}=?", val])
|
34
|
+
end
|
21
35
|
end
|
22
36
|
res = res.group(self.group.map { |c| @scope.column(c).definition })
|
23
37
|
res
|
data/lib/devlin/version.rb
CHANGED
data/spec/query_spec.rb
CHANGED
@@ -14,6 +14,8 @@ describe TestReport do
|
|
14
14
|
- earnings
|
15
15
|
conditions:
|
16
16
|
year: 2012
|
17
|
+
month.geq: 8
|
18
|
+
month.leq: 10
|
17
19
|
group:
|
18
20
|
- manufacturer
|
19
21
|
- month
|
@@ -25,7 +27,7 @@ describe TestReport do
|
|
25
27
|
rep = TestReport.new user_id: 1
|
26
28
|
rep.query(@q).scope.should eq(rep.scope(:transaction))
|
27
29
|
rep.query(@q).select.should eq(['month', 'manufacturer', 'earnings'])
|
28
|
-
rep.query(@q).conditions.should eq('year' => 2012)
|
30
|
+
rep.query(@q).conditions.should eq('year' => 2012, 'month.geq' => 8, 'month.leq' => 10)
|
29
31
|
rep.query(@q).group.should eq(['manufacturer', 'month'])
|
30
32
|
end
|
31
33
|
|
@@ -34,6 +36,13 @@ describe TestReport do
|
|
34
36
|
rep = TestReport.new user_id: 1
|
35
37
|
rep.query(@q).result.first.attributes.keys.map(&:to_sym).should eq([:month, :manufacturer, :earnings])
|
36
38
|
end
|
39
|
+
|
40
|
+
it 'should contain only results with months between 1 and 3' do
|
41
|
+
rep = TestReport.new user_id: 1
|
42
|
+
rep.query(@q).result.each do |r|
|
43
|
+
r.month.to_i.should be_between(8, 10)
|
44
|
+
end
|
45
|
+
end
|
37
46
|
end
|
38
47
|
end
|
39
48
|
end
|
data/spec/setup_spec.rb
CHANGED
@@ -23,7 +23,7 @@ describe TestReport do
|
|
23
23
|
rep = TestReport.new(user_id: 1)
|
24
24
|
scope = rep.scope(:transaction)
|
25
25
|
scope.column(:manufacturer).select_definition.should eq('manufacturer AS manufacturer')
|
26
|
-
scope.column(:year).select_definition.should eq("strftime('%Y', date) AS year")
|
26
|
+
scope.column(:year).select_definition.should eq("CAST(strftime('%Y', date) AS INTEGER) AS year")
|
27
27
|
end
|
28
28
|
|
29
29
|
it 'should return the column value as is, if no getter is defined' do
|
data/spec/spec_helper.rb
CHANGED
@@ -29,8 +29,8 @@ class TestReport < Devlin::Base
|
|
29
29
|
relation Transaction.where(user_id: scope.params[:user_id]).scoped
|
30
30
|
|
31
31
|
column :manufacturer, "manufacturer"
|
32
|
-
column :year, "strftime('%Y', date)"
|
33
|
-
column :month, "strftime('%m', date)" do |value|
|
32
|
+
column :year, "CAST(strftime('%Y', date) AS INTEGER)"
|
33
|
+
column :month, "CAST(strftime('%m', date) AS INTEGER)" do |value|
|
34
34
|
months = %W(~ Jan Feb Mar Apr May Jun Jul Aug Sep Okt Nov Dec)
|
35
35
|
months[value]
|
36
36
|
end
|
data/spec/test.sqlite3
CHANGED
Binary file
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: devlin
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
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-08-
|
12
|
+
date: 2012-08-18 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|