rufus-cloche 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/CHANGELOG.txt +8 -0
  2. data/lib/rufus/cloche.rb +26 -8
  3. data/test/test.rb +21 -15
  4. metadata +2 -2
data/CHANGELOG.txt CHANGED
@@ -1,9 +1,17 @@
1
1
 
2
2
  = rufus-cloche CHANGELOG.txt
3
3
 
4
+
5
+ == rufus-cloche - 0.1.3 released 2009/12/10
6
+
7
+ - added the :limit option to Cloche#get_many
8
+ - Cloche#get_many sorting by filenames (not by id anymore)
9
+
10
+
4
11
  == rufus-cloche - 0.1.2 released 2009/12/09
5
12
 
6
13
  - avoiding dir beginning with a '.' (dot)
7
14
 
15
+
8
16
  == rufus-cloche - 0.1.1 released 2009/11/16
9
17
 
data/lib/rufus/cloche.rb CHANGED
@@ -57,7 +57,7 @@ module Rufus
57
57
  end
58
58
  end
59
59
 
60
- VERSION = '0.1.2'
60
+ VERSION = '0.1.3'
61
61
 
62
62
  attr_reader :dir
63
63
 
@@ -161,26 +161,44 @@ module Rufus
161
161
  #
162
162
  # Will return an empty Hash if there is no documents for a given type.
163
163
  #
164
- def get_many (type, key_match=nil)
164
+ # == opts
165
+ #
166
+ # The only option know for now is :limit, which limits the number of
167
+ # documents returned.
168
+ #
169
+ def get_many (type, key_match=nil, opts={})
165
170
 
166
171
  d = dir_for(type)
167
172
 
168
173
  return [] unless File.exist?(d)
169
174
 
170
- Dir[File.join(d, '**', '*.json')].inject([]) { |a, fn|
175
+ docs = []
176
+ limit = opts[:limit]
177
+
178
+ files = Dir[File.join(d, '**', '*.json')].sort { |p0, p1|
179
+ File.basename(p0) <=> File.basename(p1)
180
+ }
181
+
182
+ files.each do |fn|
171
183
 
172
184
  key = File.basename(fn, '.json')
173
185
 
174
186
  if (not key_match) || key.match(key_match)
175
187
 
176
188
  doc = get(type, key)
177
- a << doc if doc
189
+ docs << doc if doc
190
+
191
+ break if limit && (docs.size >= limit)
178
192
  end
193
+ end
179
194
 
180
- a
181
- }.sort { |doc0, doc1|
182
- doc0['_id'] <=> doc1['_id']
183
- }
195
+ # WARNING : there is a twist here, the filenames may have a different
196
+ # sort order from actual _ids...
197
+
198
+ #docs.sort { |doc0, doc1| doc0['_id'] <=> doc1['_id'] }
199
+ # let's trust filename order
200
+
201
+ docs
184
202
  end
185
203
 
186
204
  protected
data/test/test.rb CHANGED
@@ -106,11 +106,7 @@ class ClocheTest < Test::Unit::TestCase
106
106
 
107
107
  def test_get_many
108
108
 
109
- @c.put({ '_id' => 'john', 'type' => 'person', 'eyes' => 'green' })
110
- @c.put({ '_id' => 'jami', 'type' => 'person', 'eyes' => 'blue' })
111
- @c.put({ '_id' => 'minehiko', 'type' => 'person', 'eyes' => 'brown' })
112
- @c.put({ '_id' => 'hiro', 'type' => 'person', 'eyes' => 'brown' })
113
- @c.put({ '_id' => 'chicko-chan', 'type' => 'animal', 'eyes' => 'black' })
109
+ load_people
114
110
 
115
111
  assert_equal(
116
112
  %w[ blue brown brown green ],
@@ -119,28 +115,29 @@ class ClocheTest < Test::Unit::TestCase
119
115
 
120
116
  def test_get_many_with_key_match
121
117
 
122
- @c.put({ '_id' => 'john', 'type' => 'person', 'eyes' => 'green' })
123
- @c.put({ '_id' => 'jami', 'type' => 'person', 'eyes' => 'blue' })
124
- @c.put({ '_id' => 'minehiko', 'type' => 'person', 'eyes' => 'brown' })
125
- @c.put({ '_id' => 'hiro', 'type' => 'person', 'eyes' => 'brown' })
126
- @c.put({ '_id' => 'chicko-chan', 'type' => 'animal', 'eyes' => 'black' })
118
+ load_people
127
119
 
128
120
  assert_equal 2, @c.get_many('person', /^j/).size
129
121
  end
130
122
 
131
123
  def test_get_many_key_order
132
124
 
133
- @c.put({ '_id' => 'john', 'type' => 'person', 'eyes' => 'green' })
134
- @c.put({ '_id' => 'jami', 'type' => 'person', 'eyes' => 'blue' })
135
- @c.put({ '_id' => 'minehiko', 'type' => 'person', 'eyes' => 'brown' })
136
- @c.put({ '_id' => 'hiro', 'type' => 'person', 'eyes' => 'brown' })
137
- @c.put({ '_id' => 'chicko-chan', 'type' => 'animal', 'eyes' => 'black' })
125
+ load_people
138
126
 
139
127
  assert_equal(
140
128
  %w[ hiro jami john minehiko ],
141
129
  @c.get_many('person').collect { |e| e['_id'] })
142
130
  end
143
131
 
132
+ def test_get_many_limit
133
+
134
+ load_people
135
+
136
+ assert_equal(
137
+ %w[ hiro jami ],
138
+ @c.get_many('person', nil, :limit => 2).collect { |e| e['_id'] })
139
+ end
140
+
144
141
  def test_dot_id
145
142
 
146
143
  @c.put({ '_id' => 'something.0', 'type' => 'nothing', 'color' => 'blue' })
@@ -152,6 +149,15 @@ class ClocheTest < Test::Unit::TestCase
152
149
 
153
150
  protected
154
151
 
152
+ def load_people
153
+
154
+ @c.put({ '_id' => 'john', 'type' => 'person', 'eyes' => 'green' })
155
+ @c.put({ '_id' => 'jami', 'type' => 'person', 'eyes' => 'blue' })
156
+ @c.put({ '_id' => 'minehiko', 'type' => 'person', 'eyes' => 'brown' })
157
+ @c.put({ '_id' => 'hiro', 'type' => 'person', 'eyes' => 'brown' })
158
+ @c.put({ '_id' => 'chicko-chan', 'type' => 'animal', 'eyes' => 'black' })
159
+ end
160
+
155
161
  def fetch (type, key)
156
162
 
157
163
  s = File.read(File.join(ROOT, 'tcloche', type, key[-2, 2], "#{key}.json"))
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rufus-cloche
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Mettraux
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-09 00:00:00 +09:00
12
+ date: 2009-12-10 00:00:00 +09:00
13
13
  default_executable:
14
14
  dependencies: []
15
15