dm-aggregates 0.10.2 → 1.0.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +36 -0
- data/Gemfile +141 -0
- data/Rakefile +2 -3
- data/VERSION +1 -1
- data/dm-aggregates.gemspec +20 -14
- data/lib/dm-aggregates.rb +8 -0
- data/lib/dm-aggregates/adapters/data_objects_adapter.rb +10 -10
- data/lib/dm-aggregates/aggregate_functions.rb +33 -8
- data/spec/public/collection_spec.rb +20 -7
- data/spec/public/model_spec.rb +7 -7
- data/spec/public/shared/aggregate_shared_spec.rb +7 -33
- data/spec/spec_helper.rb +46 -24
- data/tasks/local_gemfile.rake +18 -0
- data/tasks/spec.rake +0 -3
- metadata +42 -28
data/.gitignore
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
## MAC OS
|
2
|
+
.DS_Store
|
3
|
+
|
4
|
+
## TEXTMATE
|
5
|
+
*.tmproj
|
6
|
+
tmtags
|
7
|
+
|
8
|
+
## EMACS
|
9
|
+
*~
|
10
|
+
\#*
|
11
|
+
.\#*
|
12
|
+
|
13
|
+
## VIM
|
14
|
+
*.swp
|
15
|
+
|
16
|
+
## Rubinius
|
17
|
+
*.rbc
|
18
|
+
|
19
|
+
## PROJECT::GENERAL
|
20
|
+
*.gem
|
21
|
+
coverage
|
22
|
+
rdoc
|
23
|
+
pkg
|
24
|
+
tmp
|
25
|
+
doc
|
26
|
+
log
|
27
|
+
.yardoc
|
28
|
+
measurements
|
29
|
+
|
30
|
+
## BUNDLER
|
31
|
+
.bundle
|
32
|
+
Gemfile.local
|
33
|
+
Gemfile.lock
|
34
|
+
|
35
|
+
## PROJECT::SPECIFIC
|
36
|
+
spec/db/
|
data/Gemfile
ADDED
@@ -0,0 +1,141 @@
|
|
1
|
+
# If you're working on more than one datamapper gem at a time, then it's
|
2
|
+
# recommended to create a local Gemfile and use this instead of the git
|
3
|
+
# sources. This will make sure that you are developing against your
|
4
|
+
# other local datamapper sources that you currently work on. Gemfile.local
|
5
|
+
# will behave identically to the standard Gemfile apart from the fact that
|
6
|
+
# it fetches the datamapper gems from local paths. This means that you can use
|
7
|
+
# the same environment variables, like ADAPTER(S) or PLUGIN(S) when running
|
8
|
+
# bundle commands. Gemfile.local is added to .gitignore, so you don't need to
|
9
|
+
# worry about accidentally checking local development paths into git.
|
10
|
+
# In order to create a local Gemfile, all you need to do is run:
|
11
|
+
#
|
12
|
+
# bundle exec rake local_gemfile
|
13
|
+
#
|
14
|
+
# This will give you a Gemfile.local file that points to your local clones of
|
15
|
+
# the various datamapper gems. It's assumed that all datamapper repo clones
|
16
|
+
# reside in the same directory. You can use the Gemfile.local like so for
|
17
|
+
# running any bundle command:
|
18
|
+
#
|
19
|
+
# BUNDLE_GEMFILE=Gemfile.local bundle foo
|
20
|
+
#
|
21
|
+
# You can also specify which adapter(s) should be part of the bundle by setting
|
22
|
+
# an environment variable. This of course also works when using the Gemfile.local
|
23
|
+
#
|
24
|
+
# bundle foo # dm-sqlite-adapter
|
25
|
+
# ADAPTER=mysql bundle foo # dm-mysql-adapter
|
26
|
+
# ADAPTERS=sqlite,mysql bundle foo # dm-sqlite-adapter and dm-mysql-adapter
|
27
|
+
#
|
28
|
+
# Of course you can also use the ADAPTER(S) variable when using the Gemfile.local
|
29
|
+
# and running specs against selected adapters.
|
30
|
+
#
|
31
|
+
# For easily working with adapters supported on your machine, it's recommended
|
32
|
+
# that you first install all adapters that you are planning to use or work on
|
33
|
+
# by doing something like
|
34
|
+
#
|
35
|
+
# ADAPTERS=sqlite,mysql,postgres bundle install
|
36
|
+
#
|
37
|
+
# This will clone the various repositories and make them available to bundler.
|
38
|
+
# Once you have them installed you can easily switch between adapters for the
|
39
|
+
# various development tasks. Running something like
|
40
|
+
#
|
41
|
+
# ADAPTER=mysql bundle exec rake spec
|
42
|
+
#
|
43
|
+
# will make sure that the dm-mysql-adapter is part of the bundle, and will be used
|
44
|
+
# when running the specs.
|
45
|
+
#
|
46
|
+
# You can also specify which plugin(s) should be part of the bundle by setting
|
47
|
+
# an environment variable. This also works when using the Gemfile.local
|
48
|
+
#
|
49
|
+
# bundle foo # dm-migrations
|
50
|
+
# PLUGINS=dm-validations bundle foo # dm-migrations and dm-validations
|
51
|
+
# PLUGINS=dm-validations,dm-types bundle foo # dm-migrations, dm-validations and dm-types
|
52
|
+
#
|
53
|
+
# Of course you can combine the PLUGIN(S) and ADAPTER(S) env vars to run specs
|
54
|
+
# for certain adapter/plugin combinations.
|
55
|
+
#
|
56
|
+
# Finally, to speed up running specs and other tasks, it's recommended to run
|
57
|
+
#
|
58
|
+
# bundle lock
|
59
|
+
#
|
60
|
+
# after running 'bundle install' for the first time. This will make 'bundle exec' run
|
61
|
+
# a lot faster compared to the unlocked version. With an unlocked bundle you would
|
62
|
+
# typically just run 'bundle install' from time to time to fetch the latest sources from
|
63
|
+
# upstream. When you locked your bundle, you need to run
|
64
|
+
#
|
65
|
+
# bundle install --relock
|
66
|
+
#
|
67
|
+
# to make sure to fetch the latest updates and then lock the bundle again. Gemfile.lock
|
68
|
+
# is added to the .gitignore file, so you don't need to worry about accidentally checking
|
69
|
+
# it into version control.
|
70
|
+
|
71
|
+
source 'http://rubygems.org'
|
72
|
+
|
73
|
+
DATAMAPPER = 'git://github.com/datamapper'
|
74
|
+
DM_VERSION = '~> 1.0.0.rc1'
|
75
|
+
|
76
|
+
group :runtime do # Runtime dependencies (as in the gemspec)
|
77
|
+
|
78
|
+
if ENV['EXTLIB']
|
79
|
+
gem 'extlib', '~> 0.9.15', :git => "#{DATAMAPPER}/extlib.git"
|
80
|
+
else
|
81
|
+
gem 'activesupport', '~> 3.0.0.beta3', :git => 'git://github.com/rails/rails.git', :require => nil
|
82
|
+
end
|
83
|
+
|
84
|
+
gem 'dm-core', DM_VERSION, :git => "#{DATAMAPPER}/dm-core.git"
|
85
|
+
|
86
|
+
end
|
87
|
+
|
88
|
+
group(:development) do # Development dependencies (as in the gemspec)
|
89
|
+
|
90
|
+
gem 'rake', '~> 0.8.7'
|
91
|
+
gem 'rspec', '~> 1.3'
|
92
|
+
gem 'jeweler', '~> 1.4'
|
93
|
+
|
94
|
+
end
|
95
|
+
|
96
|
+
group :quality do # These gems contain rake tasks that check the quality of the source code
|
97
|
+
|
98
|
+
gem 'metric_fu', '~> 1.3'
|
99
|
+
gem 'rcov', '~> 0.9.7'
|
100
|
+
gem 'reek', '~> 1.2.7'
|
101
|
+
gem 'roodi', '~> 2.1'
|
102
|
+
gem 'yard', '~> 0.5'
|
103
|
+
gem 'yardstick', '~> 0.1'
|
104
|
+
|
105
|
+
end
|
106
|
+
|
107
|
+
group :datamapper do # We need this because we want to pin these dependencies to their git master sources
|
108
|
+
|
109
|
+
adapters = ENV['ADAPTER'] || ENV['ADAPTERS']
|
110
|
+
adapters = adapters.to_s.gsub(',',' ').split(' ') - ['in_memory']
|
111
|
+
|
112
|
+
unless adapters.empty?
|
113
|
+
|
114
|
+
DO_VERSION = '~> 0.10.2'
|
115
|
+
DM_DO_ADAPTERS = %w[sqlite postgres mysql oracle sqlserver]
|
116
|
+
|
117
|
+
gem 'data_objects', DO_VERSION, :git => "#{DATAMAPPER}/do.git"
|
118
|
+
|
119
|
+
adapters.each do |adapter|
|
120
|
+
if DM_DO_ADAPTERS.any? { |dm_do_adapter| dm_do_adapter =~ /#{adapter}/ }
|
121
|
+
adapter = 'sqlite3' if adapter == 'sqlite'
|
122
|
+
gem "do_#{adapter}", DO_VERSION, :git => "#{DATAMAPPER}/do.git"
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
gem 'dm-do-adapter', DM_VERSION, :git => "#{DATAMAPPER}/dm-do-adapter.git"
|
127
|
+
|
128
|
+
adapters.each do |adapter|
|
129
|
+
gem "dm-#{adapter}-adapter", DM_VERSION, :git => "#{DATAMAPPER}/dm-#{adapter}-adapter.git"
|
130
|
+
end
|
131
|
+
|
132
|
+
end
|
133
|
+
|
134
|
+
plugins = ENV['PLUGINS'] || ENV['PLUGIN']
|
135
|
+
plugins = (plugins.to_s.gsub(',',' ').split(' ') + ['dm-migrations']).uniq
|
136
|
+
|
137
|
+
plugins.each do |plugin|
|
138
|
+
gem plugin, DM_VERSION, :git => "#{DATAMAPPER}/#{plugin}.git"
|
139
|
+
end
|
140
|
+
|
141
|
+
end
|
data/Rakefile
CHANGED
@@ -15,10 +15,9 @@ begin
|
|
15
15
|
|
16
16
|
gem.rubyforge_project = 'datamapper'
|
17
17
|
|
18
|
-
gem.add_dependency 'dm-core', '~> 0.
|
18
|
+
gem.add_dependency 'dm-core', '~> 1.0.0.rc1'
|
19
19
|
|
20
|
-
gem.add_development_dependency 'rspec', '~> 1.
|
21
|
-
gem.add_development_dependency 'yard', '~> 0.4.0'
|
20
|
+
gem.add_development_dependency 'rspec', '~> 1.3'
|
22
21
|
end
|
23
22
|
|
24
23
|
Jeweler::GemcutterTasks.new
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
1.0.0.rc1
|
data/dm-aggregates.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{dm-aggregates}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "1.0.0.rc1"
|
9
9
|
|
10
|
-
s.required_rubygems_version = Gem::Requirement.new("
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Foy Savas"]
|
12
|
-
s.date = %q{
|
12
|
+
s.date = %q{2010-05-19}
|
13
13
|
s.description = %q{DataMapper plugin providing support for aggregates on collections}
|
14
14
|
s.email = %q{foysavas [a] gmail [d] com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -17,7 +17,9 @@ Gem::Specification.new do |s|
|
|
17
17
|
"README.rdoc"
|
18
18
|
]
|
19
19
|
s.files = [
|
20
|
-
"
|
20
|
+
".gitignore",
|
21
|
+
"Gemfile",
|
22
|
+
"LICENSE",
|
21
23
|
"README.rdoc",
|
22
24
|
"Rakefile",
|
23
25
|
"VERSION",
|
@@ -37,6 +39,7 @@ Gem::Specification.new do |s|
|
|
37
39
|
"spec/spec.opts",
|
38
40
|
"spec/spec_helper.rb",
|
39
41
|
"tasks/ci.rake",
|
42
|
+
"tasks/local_gemfile.rake",
|
40
43
|
"tasks/metrics.rake",
|
41
44
|
"tasks/spec.rake",
|
42
45
|
"tasks/yard.rake",
|
@@ -46,26 +49,29 @@ Gem::Specification.new do |s|
|
|
46
49
|
s.rdoc_options = ["--charset=UTF-8"]
|
47
50
|
s.require_paths = ["lib"]
|
48
51
|
s.rubyforge_project = %q{datamapper}
|
49
|
-
s.rubygems_version = %q{1.3.
|
52
|
+
s.rubygems_version = %q{1.3.6}
|
50
53
|
s.summary = %q{DataMapper plugin providing support for aggregates on collections}
|
54
|
+
s.test_files = [
|
55
|
+
"spec/public/collection_spec.rb",
|
56
|
+
"spec/public/model_spec.rb",
|
57
|
+
"spec/public/shared/aggregate_shared_spec.rb",
|
58
|
+
"spec/spec_helper.rb"
|
59
|
+
]
|
51
60
|
|
52
61
|
if s.respond_to? :specification_version then
|
53
62
|
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
54
63
|
s.specification_version = 3
|
55
64
|
|
56
65
|
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
57
|
-
s.add_runtime_dependency(%q<dm-core>, ["~> 0.
|
58
|
-
s.add_development_dependency(%q<rspec>, ["~> 1.
|
59
|
-
s.add_development_dependency(%q<yard>, ["~> 0.4.0"])
|
66
|
+
s.add_runtime_dependency(%q<dm-core>, ["~> 1.0.0.rc1"])
|
67
|
+
s.add_development_dependency(%q<rspec>, ["~> 1.3"])
|
60
68
|
else
|
61
|
-
s.add_dependency(%q<dm-core>, ["~> 0.
|
62
|
-
s.add_dependency(%q<rspec>, ["~> 1.
|
63
|
-
s.add_dependency(%q<yard>, ["~> 0.4.0"])
|
69
|
+
s.add_dependency(%q<dm-core>, ["~> 1.0.0.rc1"])
|
70
|
+
s.add_dependency(%q<rspec>, ["~> 1.3"])
|
64
71
|
end
|
65
72
|
else
|
66
|
-
s.add_dependency(%q<dm-core>, ["~> 0.
|
67
|
-
s.add_dependency(%q<rspec>, ["~> 1.
|
68
|
-
s.add_dependency(%q<yard>, ["~> 0.4.0"])
|
73
|
+
s.add_dependency(%q<dm-core>, ["~> 1.0.0.rc1"])
|
74
|
+
s.add_dependency(%q<rspec>, ["~> 1.3"])
|
69
75
|
end
|
70
76
|
end
|
71
77
|
|
data/lib/dm-aggregates.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'dm-core'
|
2
|
+
|
1
3
|
require 'dm-aggregates/adapters/data_objects_adapter'
|
2
4
|
require 'dm-aggregates/aggregate_functions'
|
3
5
|
require 'dm-aggregates/collection'
|
@@ -6,6 +8,12 @@ require 'dm-aggregates/model'
|
|
6
8
|
require 'dm-aggregates/query'
|
7
9
|
require 'dm-aggregates/repository'
|
8
10
|
|
11
|
+
begin
|
12
|
+
require 'active_support/core_ext/time/conversions'
|
13
|
+
rescue LoadError
|
14
|
+
require 'extlib/time'
|
15
|
+
end
|
16
|
+
|
9
17
|
module DataMapper
|
10
18
|
class Repository
|
11
19
|
include Aggregates::Repository
|
@@ -27,7 +27,7 @@ module DataMapper
|
|
27
27
|
if field.respond_to?(:operator)
|
28
28
|
send(field.operator, field.target, value)
|
29
29
|
else
|
30
|
-
field.
|
30
|
+
field.load(value)
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
@@ -48,19 +48,19 @@ module DataMapper
|
|
48
48
|
end
|
49
49
|
|
50
50
|
def min(property, value)
|
51
|
-
property.
|
51
|
+
property.load(value)
|
52
52
|
end
|
53
53
|
|
54
54
|
def max(property, value)
|
55
|
-
property.
|
55
|
+
property.load(value)
|
56
56
|
end
|
57
57
|
|
58
58
|
def avg(property, value)
|
59
|
-
property.
|
59
|
+
property.primitive == ::Integer ? value.to_f : property.load(value)
|
60
60
|
end
|
61
61
|
|
62
62
|
def sum(property, value)
|
63
|
-
property.
|
63
|
+
property.load(value)
|
64
64
|
end
|
65
65
|
|
66
66
|
module SQL
|
@@ -72,24 +72,24 @@ module DataMapper
|
|
72
72
|
RUBY
|
73
73
|
end
|
74
74
|
|
75
|
-
def property_to_column_name_with_operator(property, qualify
|
75
|
+
def property_to_column_name_with_operator(property, qualify)
|
76
76
|
case property
|
77
77
|
when DataMapper::Query::Operator
|
78
|
-
aggregate_field_statement(property.operator, property.target, qualify
|
78
|
+
aggregate_field_statement(property.operator, property.target, qualify)
|
79
79
|
|
80
80
|
when Property, DataMapper::Query::Path
|
81
|
-
property_to_column_name_without_operator(property, qualify
|
81
|
+
property_to_column_name_without_operator(property, qualify)
|
82
82
|
|
83
83
|
else
|
84
84
|
raise ArgumentError, "+property+ must be a DataMapper::Query::Operator, a DataMapper::Property or a Query::Path, but was a #{property.class} (#{property.inspect})"
|
85
85
|
end
|
86
86
|
end
|
87
87
|
|
88
|
-
def aggregate_field_statement(aggregate_function, property, qualify
|
88
|
+
def aggregate_field_statement(aggregate_function, property, qualify)
|
89
89
|
column_name = if aggregate_function == :count && property == :all
|
90
90
|
'*'
|
91
91
|
else
|
92
|
-
property_to_column_name(property, qualify
|
92
|
+
property_to_column_name(property, qualify)
|
93
93
|
end
|
94
94
|
|
95
95
|
function_name = case aggregate_function
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module DataMapper
|
2
2
|
module Aggregates
|
3
3
|
module Functions
|
4
|
-
include
|
4
|
+
include DataMapper::Assertions
|
5
5
|
|
6
6
|
# Count results (given the conditions)
|
7
7
|
#
|
@@ -58,7 +58,7 @@ module DataMapper
|
|
58
58
|
query = args.last.kind_of?(Hash) ? args.pop : {}
|
59
59
|
property_name = args.first
|
60
60
|
|
61
|
-
assert_property_type property_name, Integer, Float, BigDecimal, DateTime, Date, Time
|
61
|
+
assert_property_type property_name, ::Integer, ::Float, ::BigDecimal, ::DateTime, ::Date, ::Time
|
62
62
|
|
63
63
|
aggregate(query.merge(:fields => [ property_name.min ]))
|
64
64
|
end
|
@@ -81,7 +81,7 @@ module DataMapper
|
|
81
81
|
query = args.last.kind_of?(Hash) ? args.pop : {}
|
82
82
|
property_name = args.first
|
83
83
|
|
84
|
-
assert_property_type property_name, Integer, Float, BigDecimal, DateTime, Date, Time
|
84
|
+
assert_property_type property_name, ::Integer, ::Float, ::BigDecimal, ::DateTime, ::Date, ::Time
|
85
85
|
|
86
86
|
aggregate(query.merge(:fields => [ property_name.max ]))
|
87
87
|
end
|
@@ -104,7 +104,7 @@ module DataMapper
|
|
104
104
|
query = args.last.kind_of?(Hash) ? args.pop : {}
|
105
105
|
property_name = args.first
|
106
106
|
|
107
|
-
assert_property_type property_name, Integer, Float, BigDecimal
|
107
|
+
assert_property_type property_name, ::Integer, ::Float, ::BigDecimal
|
108
108
|
|
109
109
|
aggregate(query.merge(:fields => [ property_name.avg ]))
|
110
110
|
end
|
@@ -124,10 +124,10 @@ module DataMapper
|
|
124
124
|
#
|
125
125
|
# @api public
|
126
126
|
def sum(*args)
|
127
|
-
query = args.last.kind_of?(Hash) ? args.pop : {}
|
127
|
+
query = args.last.kind_of?(::Hash) ? args.pop : {}
|
128
128
|
property_name = args.first
|
129
129
|
|
130
|
-
assert_property_type property_name, Integer, Float, BigDecimal
|
130
|
+
assert_property_type property_name, ::Integer, ::Float, ::BigDecimal
|
131
131
|
|
132
132
|
aggregate(query.merge(:fields => [ property_name.sum ]))
|
133
133
|
end
|
@@ -156,10 +156,27 @@ module DataMapper
|
|
156
156
|
query[:fields] ||= []
|
157
157
|
query[:fields] |= args
|
158
158
|
query[:fields].map! { |f| normalize_field(f) }
|
159
|
-
query[:order] ||= query[:fields].select { |p| p.kind_of?(Property) }
|
160
159
|
|
161
160
|
raise ArgumentError, 'query[:fields] must not be empty' if query[:fields].empty?
|
162
161
|
|
162
|
+
unless query.key?(:order)
|
163
|
+
# the current collection/model is already sorted by attributes
|
164
|
+
# and since we are projecting away some of the attributes,
|
165
|
+
# and then performing aggregate functions on the remainder,
|
166
|
+
# we need to honor the existing order, as if it were already
|
167
|
+
# materialized, and we are looping over the rows in order.
|
168
|
+
|
169
|
+
directions = direction_map
|
170
|
+
|
171
|
+
query[:order] = []
|
172
|
+
|
173
|
+
# use the current query order for each property if available
|
174
|
+
query[:fields].each do |property|
|
175
|
+
next unless property.kind_of?(Property)
|
176
|
+
query[:order] << directions.fetch(property, property)
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
163
180
|
query = scoped_query(query)
|
164
181
|
|
165
182
|
if query.fields.any? { |p| p.kind_of?(Property) }
|
@@ -177,7 +194,7 @@ module DataMapper
|
|
177
194
|
end
|
178
195
|
|
179
196
|
property = property_by_name(name)
|
180
|
-
type = property.
|
197
|
+
type = property.primitive
|
181
198
|
|
182
199
|
unless types.include?(type)
|
183
200
|
raise ArgumentError, "#{name} must be #{types * ' or '}, but was #{type}"
|
@@ -202,6 +219,14 @@ module DataMapper
|
|
202
219
|
field
|
203
220
|
end
|
204
221
|
end
|
222
|
+
|
223
|
+
def direction_map
|
224
|
+
direction_map = {}
|
225
|
+
self.query.order.each do |direction|
|
226
|
+
direction_map[direction.target] = direction
|
227
|
+
end
|
228
|
+
direction_map
|
229
|
+
end
|
205
230
|
end
|
206
231
|
end
|
207
232
|
end
|
@@ -1,14 +1,15 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
describe DataMapper::Collection do
|
5
|
-
it_should_behave_like 'It Has Setup Resources'
|
3
|
+
describe DataMapper::Collection do
|
6
4
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
5
|
+
before :all do
|
6
|
+
@dragons = Dragon.all
|
7
|
+
@countries = Country.all
|
8
|
+
end
|
9
|
+
|
10
|
+
supported_by :sqlite, :mysql, :postgres do
|
11
11
|
|
12
|
+
it_should_behave_like 'It Has Setup Resources'
|
12
13
|
it_should_behave_like 'An Aggregatable Class'
|
13
14
|
|
14
15
|
describe 'ignore invalid query' do
|
@@ -102,5 +103,17 @@ if HAS_SQLITE3 || HAS_MYSQL || HAS_POSTGRES
|
|
102
103
|
it { should == [ 2, 2, 3, 4, 3.5, 7 ] }
|
103
104
|
end
|
104
105
|
end
|
106
|
+
|
107
|
+
describe 'with the order reversed by the grouping field' do
|
108
|
+
before do
|
109
|
+
@dragons = Dragon.all(:order => [ :birth_at.desc ])
|
110
|
+
end
|
111
|
+
|
112
|
+
it 'should display the results in reverse order' do
|
113
|
+
@dragons.aggregate(:birth_at, :all.count).should == Dragon.aggregate(:birth_at, :all.count).reverse
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
105
117
|
end
|
118
|
+
|
106
119
|
end
|
data/spec/public/model_spec.rb
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
describe DataMapper::Model do
|
5
|
-
it_should_behave_like 'It Has Setup Resources'
|
3
|
+
describe DataMapper::Model do
|
6
4
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
5
|
+
before :all do
|
6
|
+
@dragons = Dragon
|
7
|
+
@countries = Country
|
8
|
+
end
|
11
9
|
|
10
|
+
supported_by :sqlite, :mysql, :postgres do
|
11
|
+
it_should_behave_like 'It Has Setup Resources'
|
12
12
|
it_should_behave_like 'An Aggregatable Class'
|
13
13
|
end
|
14
14
|
end
|
@@ -1,40 +1,12 @@
|
|
1
1
|
shared_examples_for 'It Has Setup Resources' do
|
2
2
|
before :all do
|
3
|
-
|
4
|
-
|
5
|
-
include DataMapper::Resource
|
3
|
+
@mysql = defined?(DataMapper::Adapters::MysqlAdapter) && @adapter.kind_of?(DataMapper::Adapters::MysqlAdapter)
|
4
|
+
@postgres = defined?(DataMapper::Adapters::PostgresAdapter) && @adapter.kind_of?(DataMapper::Adapters::PostgresAdapter)
|
6
5
|
|
7
|
-
|
8
|
-
|
9
|
-
end
|
10
|
-
|
11
|
-
class ::Dragon
|
12
|
-
include DataMapper::Resource
|
13
|
-
|
14
|
-
property :id, Serial
|
15
|
-
property :name, String
|
16
|
-
property :is_fire_breathing, Boolean
|
17
|
-
property :toes_on_claw, Integer
|
18
|
-
property :birth_at, DateTime
|
19
|
-
property :birth_on, Date
|
20
|
-
property :birth_time, Time
|
21
|
-
|
22
|
-
belongs_to :knight, :required => false
|
23
|
-
end
|
6
|
+
@skip = (@mysql || @postgres) && ENV['TZ'].to_s.downcase != 'utc'
|
7
|
+
end
|
24
8
|
|
25
|
-
|
26
|
-
# Statistics taken from CIA World Factbook:
|
27
|
-
# https://www.cia.gov/library/publications/the-world-factbook/
|
28
|
-
class ::Country
|
29
|
-
include DataMapper::Resource
|
30
|
-
|
31
|
-
property :id, Serial
|
32
|
-
property :name, String, :required => true
|
33
|
-
property :population, Integer
|
34
|
-
property :birth_rate, Float, :precision => 4, :scale => 2
|
35
|
-
property :gold_reserve_tonnes, Float, :precision => 6, :scale => 2
|
36
|
-
property :gold_reserve_value, BigDecimal, :precision => 15, :scale => 1 # approx. value in USD
|
37
|
-
end
|
9
|
+
before :all do
|
38
10
|
|
39
11
|
DataMapper.auto_migrate!
|
40
12
|
|
@@ -140,6 +112,7 @@ shared_examples_for 'An Aggregatable Class' do
|
|
140
112
|
end
|
141
113
|
|
142
114
|
it 'should provide the lowest value of a DateTime property' do
|
115
|
+
pending 'TODO: returns incorrect value until DO handles TZs properly' if @skip
|
143
116
|
@dragons.min(:birth_at).should be_kind_of(DateTime)
|
144
117
|
@dragons.min(:birth_at).to_s.should == @birth_at.to_s
|
145
118
|
end
|
@@ -185,6 +158,7 @@ shared_examples_for 'An Aggregatable Class' do
|
|
185
158
|
end
|
186
159
|
|
187
160
|
it 'should provide the highest value of a DateTime property' do
|
161
|
+
pending 'TODO: returns incorrect value until DO handles TZs properly' if @skip
|
188
162
|
@dragons.min(:birth_at).should be_kind_of(DateTime)
|
189
163
|
@dragons.min(:birth_at).to_s.should == @birth_at.to_s
|
190
164
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,32 +1,54 @@
|
|
1
|
-
require '
|
2
|
-
|
3
|
-
# use local dm-core if running from a typical dev checkout.
|
4
|
-
lib = File.join('..', '..', 'dm-core', 'lib')
|
5
|
-
$LOAD_PATH.unshift(lib) if File.directory?(lib)
|
6
|
-
require 'dm-core'
|
7
|
-
|
8
|
-
# Support running specs with 'rake spec' and 'spec'
|
9
|
-
$LOAD_PATH.unshift('lib') unless $LOAD_PATH.include?('lib')
|
1
|
+
require 'dm-core/spec/setup'
|
2
|
+
require 'dm-core/spec/lib/adapter_helpers'
|
10
3
|
|
11
4
|
require 'dm-aggregates'
|
5
|
+
require 'dm-migrations'
|
12
6
|
|
13
7
|
require 'public/shared/aggregate_shared_spec'
|
14
8
|
|
15
|
-
|
16
|
-
return false if ENV['ADAPTER'] != name.to_s
|
9
|
+
DataMapper::Spec.setup
|
17
10
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
11
|
+
Spec::Runner.configure do |config|
|
12
|
+
|
13
|
+
config.extend(DataMapper::Spec::Adapters::Helpers)
|
14
|
+
|
15
|
+
config.before(:all) do
|
16
|
+
|
17
|
+
# A simplistic example, using with an Integer property
|
18
|
+
class ::Knight
|
19
|
+
include DataMapper::Resource
|
27
20
|
|
28
|
-
|
21
|
+
property :id, Serial
|
22
|
+
property :name, String
|
23
|
+
end
|
29
24
|
|
30
|
-
|
31
|
-
|
32
|
-
|
25
|
+
class ::Dragon
|
26
|
+
include DataMapper::Resource
|
27
|
+
|
28
|
+
property :id, Serial
|
29
|
+
property :name, String
|
30
|
+
property :is_fire_breathing, Boolean
|
31
|
+
property :toes_on_claw, Integer
|
32
|
+
property :birth_at, DateTime
|
33
|
+
property :birth_on, Date
|
34
|
+
property :birth_time, Time
|
35
|
+
|
36
|
+
belongs_to :knight, :required => false
|
37
|
+
end
|
38
|
+
|
39
|
+
# A more complex example, with BigDecimal and Float properties
|
40
|
+
# Statistics taken from CIA World Factbook:
|
41
|
+
# https://www.cia.gov/library/publications/the-world-factbook/
|
42
|
+
class ::Country
|
43
|
+
include DataMapper::Resource
|
44
|
+
|
45
|
+
property :id, Serial
|
46
|
+
property :name, String, :required => true
|
47
|
+
property :population, Integer
|
48
|
+
property :birth_rate, Float, :precision => 4, :scale => 2
|
49
|
+
property :gold_reserve_tonnes, Float, :precision => 6, :scale => 2
|
50
|
+
property :gold_reserve_value, Decimal, :precision => 15, :scale => 1 # approx. value in USD
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
desc "Support bundling from local source code (allows BUNDLE_GEMFILE=Gemfile.local bundle foo)"
|
2
|
+
task :local_gemfile do |t|
|
3
|
+
|
4
|
+
root = Pathname(__FILE__).dirname.parent
|
5
|
+
datamapper = root.parent
|
6
|
+
|
7
|
+
source_regex = /DATAMAPPER = 'git:\/\/github.com\/datamapper'/
|
8
|
+
gem_source_regex = /:git => \"#\{DATAMAPPER\}\/(.+?)(?:\.git)?\"/
|
9
|
+
|
10
|
+
root.join('Gemfile.local').open('w') do |f|
|
11
|
+
root.join('Gemfile').open.each do |line|
|
12
|
+
line.sub!(source_regex, "DATAMAPPER = '#{datamapper}'")
|
13
|
+
line.sub!(gem_source_regex, ':path => "#{DATAMAPPER}/\1"')
|
14
|
+
f.puts line
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
data/tasks/spec.rake
CHANGED
metadata
CHANGED
@@ -1,7 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dm-aggregates
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
prerelease: true
|
5
|
+
segments:
|
6
|
+
- 1
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- rc1
|
10
|
+
version: 1.0.0.rc1
|
5
11
|
platform: ruby
|
6
12
|
authors:
|
7
13
|
- Foy Savas
|
@@ -9,39 +15,37 @@ autorequire:
|
|
9
15
|
bindir: bin
|
10
16
|
cert_chain: []
|
11
17
|
|
12
|
-
date:
|
18
|
+
date: 2010-05-19 00:00:00 -07:00
|
13
19
|
default_executable:
|
14
20
|
dependencies:
|
15
21
|
- !ruby/object:Gem::Dependency
|
16
22
|
name: dm-core
|
17
|
-
|
18
|
-
|
19
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
25
|
requirements:
|
21
26
|
- - ~>
|
22
27
|
- !ruby/object:Gem::Version
|
23
|
-
|
24
|
-
|
28
|
+
segments:
|
29
|
+
- 1
|
30
|
+
- 0
|
31
|
+
- 0
|
32
|
+
- rc1
|
33
|
+
version: 1.0.0.rc1
|
34
|
+
type: :runtime
|
35
|
+
version_requirements: *id001
|
25
36
|
- !ruby/object:Gem::Dependency
|
26
37
|
name: rspec
|
27
|
-
|
28
|
-
|
29
|
-
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
prerelease: false
|
39
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
40
|
requirements:
|
31
41
|
- - ~>
|
32
42
|
- !ruby/object:Gem::Version
|
33
|
-
|
34
|
-
|
35
|
-
-
|
36
|
-
|
43
|
+
segments:
|
44
|
+
- 1
|
45
|
+
- 3
|
46
|
+
version: "1.3"
|
37
47
|
type: :development
|
38
|
-
|
39
|
-
version_requirements: !ruby/object:Gem::Requirement
|
40
|
-
requirements:
|
41
|
-
- - ~>
|
42
|
-
- !ruby/object:Gem::Version
|
43
|
-
version: 0.4.0
|
44
|
-
version:
|
48
|
+
version_requirements: *id002
|
45
49
|
description: DataMapper plugin providing support for aggregates on collections
|
46
50
|
email: foysavas [a] gmail [d] com
|
47
51
|
executables: []
|
@@ -52,6 +56,8 @@ extra_rdoc_files:
|
|
52
56
|
- LICENSE
|
53
57
|
- README.rdoc
|
54
58
|
files:
|
59
|
+
- .gitignore
|
60
|
+
- Gemfile
|
55
61
|
- LICENSE
|
56
62
|
- README.rdoc
|
57
63
|
- Rakefile
|
@@ -72,6 +78,7 @@ files:
|
|
72
78
|
- spec/spec.opts
|
73
79
|
- spec/spec_helper.rb
|
74
80
|
- tasks/ci.rake
|
81
|
+
- tasks/local_gemfile.rake
|
75
82
|
- tasks/metrics.rake
|
76
83
|
- tasks/spec.rake
|
77
84
|
- tasks/yard.rake
|
@@ -89,20 +96,27 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
89
96
|
requirements:
|
90
97
|
- - ">="
|
91
98
|
- !ruby/object:Gem::Version
|
99
|
+
segments:
|
100
|
+
- 0
|
92
101
|
version: "0"
|
93
|
-
version:
|
94
102
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
103
|
requirements:
|
96
|
-
- - "
|
104
|
+
- - ">"
|
97
105
|
- !ruby/object:Gem::Version
|
98
|
-
|
99
|
-
|
106
|
+
segments:
|
107
|
+
- 1
|
108
|
+
- 3
|
109
|
+
- 1
|
110
|
+
version: 1.3.1
|
100
111
|
requirements: []
|
101
112
|
|
102
113
|
rubyforge_project: datamapper
|
103
|
-
rubygems_version: 1.3.
|
114
|
+
rubygems_version: 1.3.6
|
104
115
|
signing_key:
|
105
116
|
specification_version: 3
|
106
117
|
summary: DataMapper plugin providing support for aggregates on collections
|
107
|
-
test_files:
|
108
|
-
|
118
|
+
test_files:
|
119
|
+
- spec/public/collection_spec.rb
|
120
|
+
- spec/public/model_spec.rb
|
121
|
+
- spec/public/shared/aggregate_shared_spec.rb
|
122
|
+
- spec/spec_helper.rb
|