dm-chunked_query 0.1.2 → 0.2.0

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.
@@ -1,3 +1,8 @@
1
+ ### 0.2.0 / 2011-04-19
2
+
3
+ * Added {DataMapper::ChunkedQuery::Chunks#first}.
4
+ * Added {DataMapper::ChunkedQuery::Mixin#each_chunk}.
5
+
1
6
  ### 0.1.2 / 2011-03-03
2
7
 
3
8
  * Fixed a typo in {DataMapper::ChunkedQuery::Chunks#[]}.
@@ -1,5 +1,5 @@
1
1
  name: dm-chunked_query
2
- version: 0.1.2
2
+ version: 0.2.0
3
3
  summary: Allows performing chunked queries with DataMapper.
4
4
  description:
5
5
  Allows performing chunked queries on DataMapper Models or Collections.
@@ -30,9 +30,8 @@ module DataMapper
30
30
  # @param [Range<Integer>, Integer] key
31
31
  # The index or range of indices to access.
32
32
  #
33
- # @return [DataMapper::Collection, nil]
33
+ # @return [DataMapper::Collection]
34
34
  # A collection of resources at the given index or indices.
35
- # If the index is out of bounds, `nil` will be returned.
36
35
  #
37
36
  def [](key)
38
37
  case key
@@ -52,14 +51,35 @@ module DataMapper
52
51
  # @param [#to_i] index
53
52
  # The index to access.
54
53
  #
55
- # @return [DataMapper::Collection, nil]
56
- # The chunk of resources at the given index. If the index is out of
57
- # bounds, `nil` will be returned.
54
+ # @return [DataMapper::Collection]
55
+ # The chunk of resources at the given index.
58
56
  #
59
57
  def at(index)
60
58
  chunk_at(index.to_i)
61
59
  end
62
60
 
61
+ #
62
+ # Returns the first chunk(s).
63
+ #
64
+ # @param [Integer] n
65
+ # The number of sub-chunks to include.
66
+ #
67
+ # @return [DataMapper::Collection]
68
+ # The first chunk of resources.
69
+ #
70
+ # @raise [ArgumentError]
71
+ # The number of sub-chunks was negative.
72
+ #
73
+ # @since 0.2.0
74
+ #
75
+ def first(n=1)
76
+ if n >= 0
77
+ chunk_at(0,n)
78
+ else
79
+ raise(ArgumentError,"negative array size")
80
+ end
81
+ end
82
+
63
83
  #
64
84
  # Enumerates over each chunk in the collection of Chunks.
65
85
  #
@@ -75,7 +95,7 @@ module DataMapper
75
95
  def each
76
96
  return enum_for(:each) unless block_given?
77
97
 
78
- (0...length).each do |index|
98
+ length.times do |index|
79
99
  yield chunk_at(index)
80
100
  end
81
101
 
@@ -119,9 +139,7 @@ module DataMapper
119
139
  # The collection of resources that makes up the chunk.
120
140
  #
121
141
  def chunk_at(index,span=1)
122
- if (index >= 0 && index < length)
123
- @query[(index * @per_chunk), (span * @per_chunk)]
124
- end
142
+ @query[(index * @per_chunk), (span * @per_chunk)]
125
143
  end
126
144
 
127
145
  end
@@ -15,6 +15,34 @@ module DataMapper
15
15
  def chunks(per_chunk)
16
16
  Chunks.new(self,per_chunk)
17
17
  end
18
+
19
+ #
20
+ # @see chunks
21
+ #
22
+ def chunks_of(per_chunk)
23
+ chunks(per_chunk)
24
+ end
25
+
26
+ #
27
+ # Enumerate over every chunk.
28
+ #
29
+ # @param [Integer] per_chunk
30
+ # The number of resources per-chunk.
31
+ #
32
+ # @yield [chunk]
33
+ # A chunk of resources within the query.
34
+ #
35
+ # @yieldparam [DataMapper::Collection] chunk
36
+ # A collection of resources that makes up the chunk.
37
+ #
38
+ # @return [Chunks]
39
+ # The abstract collection of chunks from the query.
40
+ #
41
+ # @since 0.2.0
42
+ #
43
+ def each_chunk(per_chunk,&block)
44
+ chunks.each(&block)
45
+ end
18
46
  end
19
47
  end
20
48
  end
@@ -51,6 +51,26 @@ describe DataMapper::ChunkedQuery::Chunks do
51
51
  end
52
52
  end
53
53
 
54
+ describe "#first" do
55
+ it "should return the first chunk by default" do
56
+ resources = subject.first
57
+ numbers = resources.map { |resource| resource.number }
58
+
59
+ numbers.should == (1..10).to_a
60
+ end
61
+
62
+ it "should return multiple chunks when n > 1" do
63
+ resources = subject.first(2)
64
+ numbers = resources.map { |resource| resource.number }
65
+
66
+ numbers.should == (1..20).to_a
67
+ end
68
+
69
+ it "should return an empty collection when n=0" do
70
+ subject.first(0).should be_empty
71
+ end
72
+ end
73
+
54
74
  it "should allow enumerating through every chunk" do
55
75
  resources = []
56
76
  subject.each { |chunk| resources += chunk }
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: dm-chunked_query
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.2
5
+ version: 0.2.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Postmodern
@@ -10,8 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-03-03 00:00:00 -08:00
14
- default_executable:
13
+ date: 2011-04-19 00:00:00 Z
15
14
  dependencies:
16
15
  - !ruby/object:Gem::Dependency
17
16
  name: dm-core
@@ -96,7 +95,6 @@ files:
96
95
  - spec/chunked_query_spec.rb
97
96
  - spec/chunks_spec.rb
98
97
  - spec/spec_helper.rb
99
- has_rdoc: yard
100
98
  homepage: http://github.com/postmodern/dm-chunked_query
101
99
  licenses:
102
100
  - MIT
@@ -120,7 +118,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
120
118
  requirements: []
121
119
 
122
120
  rubyforge_project: dm-chunked_query
123
- rubygems_version: 1.5.2
121
+ rubygems_version: 1.7.2
124
122
  signing_key:
125
123
  specification_version: 3
126
124
  summary: Allows performing chunked queries with DataMapper.