devlin 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -14,4 +14,5 @@ rdoc
14
14
  spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
+ spec/*.sqlite3
17
18
  tmp
data/lib/devlin/column.rb CHANGED
@@ -1,8 +1,9 @@
1
1
  module Devlin
2
2
  class Column
3
- def initialize(name, config)
3
+ def initialize(name, config, *args)
4
4
  @name = name
5
5
  @config = config
6
+ @arguments = args.extract_options!
6
7
  end
7
8
 
8
9
  def select_definition
@@ -20,5 +21,9 @@ module Devlin
20
21
  value
21
22
  end
22
23
  end
24
+
25
+ def arguments
26
+ @arguments
27
+ end
23
28
  end
24
29
  end
data/lib/devlin/query.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  module Devlin
2
2
  class Query
3
- attr_reader :query, :scope, :select, :conditions, :group
3
+ attr_reader :query, :scope
4
4
 
5
5
  def initialize(parent, q)
6
6
  @parent = parent
@@ -9,6 +9,7 @@ module Devlin
9
9
  @select = query['select']
10
10
  @conditions = query['conditions']
11
11
  @group = query['group']
12
+ @order = query['order']
12
13
  end
13
14
 
14
15
  # This method returns the resulting relation to calculate the given
@@ -16,7 +17,7 @@ module Devlin
16
17
  def result
17
18
  res = @scope.relation
18
19
  res = res.select(self.select.map { |c| @scope.column(c).select_definition })
19
- @conditions.each do |col, val|
20
+ self.conditions.each do |col, val|
20
21
  col, op = col.split('.')
21
22
  res = case op
22
23
  when 'g'
@@ -34,6 +35,34 @@ module Devlin
34
35
  end
35
36
  end
36
37
  res = res.group(self.group.map { |c| @scope.column(c).definition })
38
+ self.order.each do |col, val|
39
+ res = res.order("#{@scope.column(col).definition} #{val}")
40
+ end
41
+ res
42
+ end # def result
43
+
44
+ def select
45
+ @select or raise "No selection columns given"
46
+ end # def select
47
+
48
+ def conditions
49
+ @conditions || {}
50
+ end # def conditions
51
+
52
+ def group
53
+ @group || []
54
+ end # def group
55
+
56
+ def order
57
+ res = {}
58
+ (@order || {}).each do |key, value|
59
+ res[key] = case value.to_s.downcase
60
+ when 'desc'
61
+ 'DESC'
62
+ else
63
+ 'ASC'
64
+ end
65
+ end
37
66
  res
38
67
  end
39
68
  end
data/lib/devlin/scope.rb CHANGED
@@ -14,8 +14,8 @@ module Devlin
14
14
  @scope.relation = rel
15
15
  end
16
16
 
17
- def column(name, definition, &block)
18
- @scope.add_column(name, definition, &block)
17
+ def column(name, definition, *args, &block)
18
+ @scope.add_column(name, definition, *args, &block)
19
19
  end
20
20
  end
21
21
 
@@ -26,12 +26,12 @@ module Devlin
26
26
  end
27
27
 
28
28
  # add a column definition to the scope
29
- def add_column(name, definition, &block)
29
+ def add_column(name, definition, *args, &block)
30
30
  @columns ||= {}
31
- @columns[name.to_sym] = Column.new name, {
31
+ @columns[name.to_sym] = Column.new(name, {
32
32
  definition: definition,
33
33
  getter: block
34
- }
34
+ }, *args)
35
35
  end
36
36
 
37
37
  def columns
@@ -1,3 +1,3 @@
1
1
  module Devlin
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/spec/query_spec.rb CHANGED
@@ -19,6 +19,9 @@ describe TestReport do
19
19
  group:
20
20
  - manufacturer
21
21
  - month
22
+ order:
23
+ month: asc
24
+ manufacturer: desc
22
25
  EOF
23
26
  end
24
27
 
@@ -29,20 +32,46 @@ describe TestReport do
29
32
  rep.query(@q).select.should eq(['month', 'manufacturer', 'earnings'])
30
33
  rep.query(@q).conditions.should eq('year' => 2012, 'month.geq' => 8, 'month.leq' => 10)
31
34
  rep.query(@q).group.should eq(['manufacturer', 'month'])
35
+ rep.query(@q).order.should eq('month' => 'ASC', 'manufacturer' => 'DESC')
36
+ end # it
37
+
38
+ it 'should return an exception if no selections are given' do
39
+ rep = TestReport.new user_id: 1
40
+ expect {
41
+ rep.query('scope: transaction').select
42
+ }.should raise_error
43
+ end
44
+
45
+ it 'should return an empty hash if no conditions are given' do
46
+ rep = TestReport.new user_id: 1
47
+ rep.query('scope: transaction').conditions.should be_a(Hash)
48
+ rep.query('scope: transaction').conditions.should be_empty
49
+ end
50
+
51
+ it 'should return an empty hash if no conditions are given' do
52
+ rep = TestReport.new user_id: 1
53
+ rep.query('scope: transaction').group.should be_an(Array)
54
+ rep.query('scope: transaction').group.should be_empty
55
+ end
56
+
57
+ it 'should return an empty hash if no order is given' do
58
+ rep = TestReport.new user_id: 1
59
+ rep.query('scope: transaction').order.should be_a(Hash)
60
+ rep.query('scope: transaction').order.should be_empty
32
61
  end
33
62
 
34
63
  describe 'result' do
35
64
  it 'should only contain the selected columns' do
36
65
  rep = TestReport.new user_id: 1
37
66
  rep.query(@q).result.first.attributes.keys.map(&:to_sym).should eq([:month, :manufacturer, :earnings])
38
- end
67
+ end # it
39
68
 
40
69
  it 'should contain only results with months between 1 and 3' do
41
70
  rep = TestReport.new user_id: 1
42
71
  rep.query(@q).result.each do |r|
43
72
  r.month.to_i.should be_between(8, 10)
44
73
  end
45
- end
46
- end
47
- end
74
+ end # it
75
+ end # describe 'result'
76
+ end # describe 'query'
48
77
  end
data/spec/setup_spec.rb CHANGED
@@ -37,6 +37,12 @@ describe TestReport do
37
37
  scope = rep.scope(:transaction)
38
38
  scope.column(:month).value(2).should eq('Feb')
39
39
  end
40
+
41
+ it 'should contain the given additional arguments' do
42
+ rep = TestReport.new(user_id: 1)
43
+ scope = rep.scope(:transaction)
44
+ scope.column(:month).arguments.should eq({ :hallo => 'Welt' })
45
+ end
40
46
  end
41
47
  end
42
48
  end
data/spec/spec_helper.rb CHANGED
@@ -30,7 +30,7 @@ class TestReport < Devlin::Base
30
30
 
31
31
  column :manufacturer, "manufacturer"
32
32
  column :year, "CAST(strftime('%Y', date) AS INTEGER)"
33
- column :month, "CAST(strftime('%m', date) AS INTEGER)" do |value|
33
+ column :month, "CAST(strftime('%m', date) AS INTEGER)", :hallo => 'Welt' 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
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.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -66,7 +66,6 @@ files:
66
66
  - spec/query_spec.rb
67
67
  - spec/setup_spec.rb
68
68
  - spec/spec_helper.rb
69
- - spec/test.sqlite3
70
69
  homepage: https://github.com/spieker/devlin
71
70
  licenses: []
72
71
  post_install_message:
@@ -98,5 +97,4 @@ test_files:
98
97
  - spec/query_spec.rb
99
98
  - spec/setup_spec.rb
100
99
  - spec/spec_helper.rb
101
- - spec/test.sqlite3
102
100
  has_rdoc:
data/spec/test.sqlite3 DELETED
Binary file