dm-chunked_query 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,3 @@
1
+ -
2
+ ChangeLog.*
3
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour --format documentation
data/.yardopts ADDED
@@ -0,0 +1 @@
1
+ --markup markdown --title "dm-chunked_query Documentation" --protected
data/ChangeLog.md ADDED
@@ -0,0 +1,6 @@
1
+ ### 0.1.0 / 2011-03-02
2
+
3
+ * Initial release:
4
+ * Allows random-access to chunks within a query.
5
+ * Can iterate through all chunks within a query.
6
+
data/Gemfile ADDED
@@ -0,0 +1,142 @@
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
7
+ # use the same environment variables, like ADAPTER(S) or PLUGIN(S) when
8
+ # running
9
+ # bundle commands. Gemfile.local is added to .gitignore, so you don't need
10
+ # to worry about accidentally checking local development paths into git.
11
+ # In order to create a local Gemfile, all you need to do is run:
12
+ #
13
+ # bundle exec rake local_gemfile
14
+ #
15
+ # This will give you a Gemfile.local file that points to your local clones
16
+ # of the various datamapper gems. It's assumed that all datamapper repo
17
+ # clones reside in the same directory. You can use the Gemfile.local like
18
+ # so for running any bundle command:
19
+ #
20
+ # BUNDLE_GEMFILE=Gemfile.local bundle foo
21
+ #
22
+ # You can also specify which adapter(s) should be part of the bundle by
23
+ # setting an environment variable. This of course also works when using the
24
+ # Gemfile.local
25
+ #
26
+ # bundle foo # dm-sqlite-adapter
27
+ #
28
+ # ADAPTER=mysql bundle foo # dm-mysql-adapter
29
+ #
30
+ # ADAPTERS=sqlite,mysql bundle foo # dm-sqlite-adapter, dm-mysql-adapter
31
+ #
32
+ # Of course you can also use the ADAPTER(S) variable when using the
33
+ # Gemfile.local and running specs against selected adapters.
34
+ #
35
+ # For easily working with adapters supported on your machine, it's
36
+ # recommended that you first install all adapters that you are planning to
37
+ # use or work on by doing something like
38
+ #
39
+ # ADAPTERS=sqlite,mysql,postgres bundle install
40
+ #
41
+ # This will clone the various repositories and make them available to
42
+ # bundler. Once you have them installed you can easily switch between
43
+ # adapters for the various development tasks. Running something like
44
+ #
45
+ # ADAPTER=mysql bundle exec rake spec
46
+ #
47
+ # will make sure that the dm-mysql-adapter is part of the bundle, and will
48
+ # be used when running the specs.
49
+ #
50
+ # You can also specify which plugin(s) should be part of the bundle by
51
+ # setting an environment variable. This also works when using the
52
+ # Gemfile.local
53
+ #
54
+ # bundle foo # dm-migrations
55
+ #
56
+ # PLUGINS=dm-validations bundle foo # dm-migrations,
57
+ # # dm-validations
58
+ #
59
+ # PLUGINS=dm-validations,dm-types bundle foo # dm-migrations,
60
+ # # dm-validations,
61
+ # # dm-types
62
+ #
63
+ # Of course you can combine the PLUGIN(S) and ADAPTER(S) env vars to run
64
+ # specs for certain adapter/plugin combinations.
65
+ #
66
+ # Finally, to speed up running specs and other tasks, it's recommended to
67
+ # run
68
+ #
69
+ # bundle lock
70
+ #
71
+ # after running 'bundle install' for the first time. This will make
72
+ # 'bundle exec' run a lot faster compared to the unlocked version. With an
73
+ # unlocked bundle you would typically just run 'bundle install' from time
74
+ # to time to fetch the latest sources from upstream. When you locked your
75
+ # bundle, you need to run
76
+ #
77
+ # bundle install --relock
78
+ #
79
+ # to make sure to fetch the latest updates and then lock the bundle again.
80
+ # Gemfile.lock is added to the .gitignore file, so you don't need to worry
81
+ # about accidentally checking it into version control.
82
+
83
+ source :rubygems
84
+
85
+ DATAMAPPER = 'http://github.com/datamapper'
86
+ DM_VERSION = '~> 1.0'
87
+ DO_VERSION = '~> 0.10.2'
88
+ DM_DO_ADAPTERS = %w[sqlite postgres mysql oracle sqlserver]
89
+ RAILS = 'http://github.com/rails/rails.git'
90
+
91
+ if ENV['EXTLIB']
92
+ gem 'extlib', '~> 0.9.15', :git => "#{DATAMAPPER}/extlib.git",
93
+ :require => nil
94
+ else
95
+ gem 'activesupport', '~> 3.0.4', :require => nil
96
+ gem 'i18n', '~> 0.5.0'
97
+ end
98
+
99
+ gem 'dm-core', DM_VERSION, :git => "#{DATAMAPPER}/dm-core.git"
100
+
101
+ group :development do
102
+ gem 'rake', '~> 0.8.7'
103
+
104
+ gem 'ore-tasks', '~> 0.4'
105
+ gem 'rspec', '~> 2.4'
106
+
107
+ gem 'kramdown', '~> 0.12.0'
108
+ gem 'yard', '~> 0.6.0'
109
+ end
110
+
111
+ group :datamapper do
112
+ # We need this because we want to pin these dependencies to their git
113
+ # master sources
114
+
115
+ adapters = ENV['ADAPTER'] || ENV['ADAPTERS']
116
+ adapters = adapters.to_s.tr(',', ' ').split.uniq - %w[ in_memory ]
117
+
118
+ if (do_adapters = DM_DO_ADAPTERS & adapters).any?
119
+ options = {}
120
+ options[:git] = "#{DATAMAPPER}/do.git" if ENV['DO_GIT'] == 'true'
121
+
122
+ gem 'data_objects', DO_VERSION, options.dup
123
+
124
+ do_adapters.each do |adapter|
125
+ adapter = 'sqlite3' if adapter == 'sqlite'
126
+ gem "do_#{adapter}", DO_VERSION, options.dup
127
+ end
128
+
129
+ gem 'dm-do-adapter', DM_VERSION, :git => "#{DATAMAPPER}/dm-do-adapter.git"
130
+ end
131
+
132
+ adapters.each do |adapter|
133
+ gem "dm-#{adapter}-adapter", DM_VERSION, :git => "#{DATAMAPPER}/dm-#{adapter}-adapter.git"
134
+ end
135
+
136
+ plugins = ENV['PLUGINS'] || ENV['PLUGIN']
137
+ plugins = plugins.to_s.tr(',', ' ').split.push('dm-migrations').uniq
138
+
139
+ plugins.each do |plugin|
140
+ gem plugin, DM_VERSION, :git => "#{DATAMAPPER}/#{plugin}.git"
141
+ end
142
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Hal Brodigan
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,48 @@
1
+ # dm-chunked_query
2
+
3
+ * [Source](http://github.com/postmodern/dm-chunked_query)
4
+ * [Issues](http://github.com/postmodern/dm-chunked_query/issues)
5
+ * [Documentation](http://rubydoc.info/gems/dm-chunked_query/frames)
6
+ * [Email](mailto:postmodern.mod3 at gmail.com)
7
+
8
+ ## Description
9
+
10
+ Allows performing chunked queries on DataMapper Models or Collections.
11
+
12
+ ## Features
13
+
14
+ * Allows random-access to chunks within a query.
15
+ * Can iterate through all chunks within a query.
16
+ * Can be used for bulk-processing of resources.
17
+ * Can also be used for pagination of resources.
18
+
19
+ ## Examples
20
+
21
+ Enumerate over all chunks, 20 resources per-chunk:
22
+
23
+ require 'dm-chunked_query'
24
+
25
+ MyModel.chunks(20).each do |chunk|
26
+ chunk.each do |resource|
27
+ # ...
28
+ end
29
+ end
30
+
31
+ Get the 5th chunk, containing 10 resources:
32
+
33
+ MyModel.all(:foo => 'bar').chunks(10)[5]
34
+
35
+ ## Requirements
36
+
37
+ * [dm-core](http://github.com/datamapper/dm-core#readme) ~> 1.0
38
+ * [dm-aggregates](http://github.com/datamapper/dm-aggregates#readme) ~> 1.0
39
+
40
+ ## Install
41
+
42
+ $ gem install dm-chunked_query
43
+
44
+ ## Copyright
45
+
46
+ Copyright (c) 2011 Hal Brodigan
47
+
48
+ See {file:LICENSE.txt} for details.
data/Rakefile ADDED
@@ -0,0 +1,29 @@
1
+ require 'rubygems'
2
+
3
+ begin
4
+ require 'bundler'
5
+ rescue LoadError => e
6
+ STDERR.puts e.message
7
+ STDERR.puts "Run `gem install bundler` to install Bundler."
8
+ exit e.status_code
9
+ end
10
+
11
+ begin
12
+ Bundler.setup(:development)
13
+ rescue Bundler::BundlerError => e
14
+ STDERR.puts e.message
15
+ STDERR.puts "Run `bundle install` to install missing gems"
16
+ exit e.status_code
17
+ end
18
+
19
+ require 'rake'
20
+
21
+ require 'ore/tasks'
22
+ Ore::Tasks.new
23
+
24
+ require 'rspec/core/rake_task'
25
+ RSpec::Core::RakeTask.new
26
+ task :default => :spec
27
+
28
+ require 'yard'
29
+ YARD::Rake::YardocTask.new
@@ -0,0 +1,15 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ begin
4
+ Ore::Specification.new do |gemspec|
5
+ # custom logic here
6
+ end
7
+ rescue NameError
8
+ begin
9
+ require 'ore/specification'
10
+ retry
11
+ rescue LoadError
12
+ STDERR.puts "The '#{__FILE__}' file requires Ore."
13
+ STDERR.puts "Run `gem install ore-core` to install Ore."
14
+ end
15
+ end
data/gemspec.yml ADDED
@@ -0,0 +1,20 @@
1
+ name: dm-chunked_query
2
+ version: 0.1.0
3
+ summary: Allows performing chunked queries with DataMapper.
4
+ description:
5
+ Allows performing chunked queries on DataMapper Models or Collections.
6
+
7
+ license: MIT
8
+ authors: Postmodern
9
+ email: postmodern.mod3@gmail.com
10
+ homepage: http://github.com/postmodern/dm-chunked_query
11
+ has_yard: true
12
+
13
+ dependencies:
14
+ dm-core: ~> 1.0
15
+ dm-aggregates: ~> 1.0
16
+
17
+ development_dependencies:
18
+ ore-tasks: ~> 0.4
19
+ rspec: ~> 2.4
20
+ yard: ~> 0.6.0
@@ -0,0 +1,116 @@
1
+ module DataMapper
2
+ module ChunkedQuery
3
+ #
4
+ # Represents the abstract collection of Chunks.
5
+ #
6
+ class Chunks
7
+
8
+ include Enumerable
9
+
10
+ # The number of resources per chunk
11
+ attr_reader :per_chunk
12
+
13
+ #
14
+ # Creates a new collection of Chunks.
15
+ #
16
+ # @param [DataMapper::Model, DataMapper::Collection] query
17
+ # The model or collection to access via chunks.
18
+ #
19
+ # @param [Integer] per_chunk
20
+ # The number of records per-chunk.
21
+ #
22
+ def initialize(query,per_chunk)
23
+ @query = query
24
+ @per_chunk = per_chunk
25
+ end
26
+
27
+ #
28
+ # Provides random access to chunks.
29
+ #
30
+ # @param [Integer, Range<Integer>] key
31
+ # The index or range of indices to access.
32
+ #
33
+ # @return [DataMapper::Collection, Array<DataMapper::Resource>]
34
+ # A collection of resources at the given index or indices.
35
+ #
36
+ def [](key)
37
+ case key
38
+ when Range
39
+ index = key.first
40
+ span = key.to_a.size
41
+
42
+ chunk_at(index,span)
43
+ when Integer
44
+ chunk_at(key)
45
+ end
46
+ end
47
+
48
+ alias at []
49
+
50
+ #
51
+ # Enumerates over each chunk in the collection of Chunks.
52
+ #
53
+ # @yield [chunk]
54
+ # The given block will be passed each chunk.
55
+ #
56
+ # @yieldparam [DataMapper::Collection] chunk
57
+ # The collection of resources that makes up a chunk.
58
+ #
59
+ # @return [Enumerator]
60
+ # If no block is given, an Enumerator object will be returned.
61
+ #
62
+ def each
63
+ return enum_for(:each) unless block_given?
64
+
65
+ (0...length).each do |index|
66
+ yield chunk_at(index)
67
+ end
68
+
69
+ return self
70
+ end
71
+
72
+ #
73
+ # Counts how many underlying resources are available.
74
+ #
75
+ # @return [Integer]
76
+ # The total number of resources.
77
+ #
78
+ def count
79
+ @count ||= @query.count
80
+ end
81
+
82
+ #
83
+ # Calculate the number of Chunks.
84
+ #
85
+ # @return [Integer]
86
+ # The number of available Chunks.
87
+ #
88
+ def length
89
+ @length ||= (count.to_f / @per_chunk).ceil
90
+ end
91
+
92
+ alias size length
93
+
94
+ protected
95
+
96
+ #
97
+ # Creates a chunk of resources.
98
+ #
99
+ # @param [Integer] index
100
+ # The index of the chunk.
101
+ #
102
+ # @param [Integer] span
103
+ # The number of chunks the chunk should span.
104
+ #
105
+ # @return [DataMapper::Collection]
106
+ # The collection of resources that makes up the chunk.
107
+ #
108
+ def chunk_at(index,span=1)
109
+ if (index >= 0 && index < length)
110
+ @query[(index * @per_chunk), (span * @per_chunk)]
111
+ end
112
+ end
113
+
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,20 @@
1
+ require 'dm-chunked_query/chunks'
2
+
3
+ module DataMapper
4
+ module ChunkedQuery
5
+ module Mixin
6
+ #
7
+ # Allows chunked access to the resources from a query.
8
+ #
9
+ # @param [Integer] per_chunk
10
+ # The number of resources per-chunk.
11
+ #
12
+ # @return [Chunks]
13
+ # The abstract collection of chunks from the query.
14
+ #
15
+ def chunks(per_chunk)
16
+ Chunks.new(self,per_chunk)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,6 @@
1
+ require 'dm-chunked_query/mixin'
2
+
3
+ require 'dm-core'
4
+
5
+ DataMapper::Model.send :include, DataMapper::ChunkedQuery::Mixin
6
+ DataMapper::Collection.send :include, DataMapper::ChunkedQuery::Mixin
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe DataMapper::ChunkedQuery do
4
+ it "should allow chunked queries directly from Models" do
5
+ TestModel.chunks(1).length.should == 100
6
+ end
7
+
8
+ it "should allow chunked queries from other queries" do
9
+ TestModel.all(:number.lte => 50).chunks(1).length.should == 50
10
+ end
11
+ end
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ describe DataMapper::ChunkedQuery::Chunks do
4
+ let(:per_chunk) { 10 }
5
+
6
+ subject { TestModel.chunks(per_chunk) }
7
+
8
+ it "should count the number of total resources" do
9
+ subject.count.should == TestModel.count
10
+ end
11
+
12
+ it "should calculate how many chunks are available" do
13
+ subject.length.should == (TestModel.count / per_chunk)
14
+ end
15
+
16
+ it "should round-up when calculate the number of available chunks" do
17
+ TestModel.chunks(TestModel.count * 0.75).length.should == 2
18
+ end
19
+
20
+ it "should allow direct access to individual chunks" do
21
+ resources = subject[1]
22
+ numbers = resources.map { |resource| resource.number }
23
+
24
+ numbers.should == (11..20).to_a
25
+ end
26
+
27
+ it "should allow direct access to a range of chunks" do
28
+ resources = subject[1..2]
29
+ numbers = resources.map { |resource| resource.number }
30
+
31
+ numbers.should == (11..30).to_a
32
+ end
33
+
34
+ it "should allow enumerating through every chunk" do
35
+ resources = []
36
+ subject.each { |chunk| resources += chunk }
37
+
38
+ TestModel.all.should == resources
39
+ end
40
+ end
@@ -0,0 +1,27 @@
1
+ require 'rspec'
2
+ require 'dm-core/spec/setup'
3
+ require 'dm-core/spec/lib/adapter_helpers'
4
+
5
+ require 'dm-chunked_query'
6
+
7
+ class TestModel
8
+
9
+ include DataMapper::Resource
10
+
11
+ property :id, Serial
12
+
13
+ property :number, Integer
14
+
15
+ end
16
+
17
+ DataMapper::Spec.setup
18
+
19
+ RSpec.configure do |config|
20
+ config.extend(DataMapper::Spec::Adapters::Helpers)
21
+
22
+ config.before(:suite) do
23
+ TestModel.auto_migrate!
24
+
25
+ (1..100).each { |i| TestModel.create(:number => i) }
26
+ end
27
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dm-chunked_query
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Postmodern
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-03-02 00:00:00 -08:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: dm-core
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: "1.0"
24
+ type: :runtime
25
+ prerelease: false
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: dm-aggregates
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ~>
33
+ - !ruby/object:Gem::Version
34
+ version: "1.0"
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: ore-tasks
40
+ requirement: &id003 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: "0.4"
46
+ type: :development
47
+ prerelease: false
48
+ version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: rspec
51
+ requirement: &id004 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ~>
55
+ - !ruby/object:Gem::Version
56
+ version: "2.4"
57
+ type: :development
58
+ prerelease: false
59
+ version_requirements: *id004
60
+ - !ruby/object:Gem::Dependency
61
+ name: yard
62
+ requirement: &id005 !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ~>
66
+ - !ruby/object:Gem::Version
67
+ version: 0.6.0
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: *id005
71
+ description: Allows performing chunked queries on DataMapper Models or Collections.
72
+ email:
73
+ - postmodern.mod3@gmail.com
74
+ executables: []
75
+
76
+ extensions: []
77
+
78
+ extra_rdoc_files:
79
+ - README.md
80
+ - ChangeLog.md
81
+ - LICENSE.txt
82
+ files:
83
+ - .document
84
+ - .rspec
85
+ - .yardopts
86
+ - ChangeLog.md
87
+ - Gemfile
88
+ - LICENSE.txt
89
+ - README.md
90
+ - Rakefile
91
+ - dm-chunked_query.gemspec
92
+ - gemspec.yml
93
+ - lib/dm-chunked_query.rb
94
+ - lib/dm-chunked_query/chunks.rb
95
+ - lib/dm-chunked_query/mixin.rb
96
+ - spec/chunked_query_spec.rb
97
+ - spec/chunks_spec.rb
98
+ - spec/spec_helper.rb
99
+ has_rdoc: yard
100
+ homepage: http://github.com/postmodern/dm-chunked_query
101
+ licenses:
102
+ - MIT
103
+ post_install_message:
104
+ rdoc_options: []
105
+
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ none: false
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: "0"
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ none: false
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: 1.3.6
120
+ requirements: []
121
+
122
+ rubyforge_project: dm-chunked_query
123
+ rubygems_version: 1.5.2
124
+ signing_key:
125
+ specification_version: 3
126
+ summary: Allows performing chunked queries with DataMapper.
127
+ test_files:
128
+ - spec/chunks_spec.rb
129
+ - spec/chunked_query_spec.rb