couch-quilt 0.2.3 → 0.3.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.
data/Rakefile CHANGED
@@ -8,7 +8,7 @@ task :default => :spec
8
8
 
9
9
  desc "Run all examples"
10
10
  Spec::Rake::SpecTask.new(:spec) do |t|
11
- t.spec_files = FileList['spec/*_spec.rb']
11
+ t.spec_files = FileList['spec/**/*_spec.rb']
12
12
  t.spec_opts = ["--color"]
13
13
  end
14
14
 
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{couch-quilt}
8
- s.version = "0.2.3"
8
+ s.version = "0.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Johannes J\303\266rg Schmidt"]
12
- s.date = %q{2010-02-25}
12
+ s.date = %q{2010-02-26}
13
13
  s.default_executable = %q{couchquilt}
14
14
  s.description = %q{Access CouchDB JSON documents from filesystem.}
15
15
  s.email = %q{schmidt@netzmerk.com}
@@ -28,8 +28,10 @@ Gem::Specification.new do |s|
28
28
  "lib/couchquilt/couch_client.rb",
29
29
  "lib/couchquilt/debugged_fs.rb",
30
30
  "lib/couchquilt/fs.rb",
31
+ "lib/couchquilt/mapper.rb",
31
32
  "lib/couchquilt/version.rb",
32
- "spec/couchquilt_spec.rb",
33
+ "spec/couchquilt/fs_spec.rb",
34
+ "spec/couchquilt/mapper_spec.rb",
33
35
  "spec/spec_helper.rb"
34
36
  ]
35
37
  s.homepage = %q{http://jo.github.com/quilt}
@@ -39,8 +41,9 @@ Gem::Specification.new do |s|
39
41
  s.rubygems_version = %q{1.3.5}
40
42
  s.summary = %q{Access CouchDB from filesystem.}
41
43
  s.test_files = [
42
- "spec/couchquilt_spec.rb",
43
- "spec/spec_helper.rb"
44
+ "spec/spec_helper.rb",
45
+ "spec/couchquilt/fs_spec.rb",
46
+ "spec/couchquilt/mapper_spec.rb"
44
47
  ]
45
48
 
46
49
  if s.respond_to? :specification_version then
@@ -28,6 +28,7 @@ $:.unshift File.dirname(__FILE__) unless
28
28
  $:.include?(File.dirname(__FILE__)) ||
29
29
  $:.include?(File.expand_path(File.dirname(__FILE__)))
30
30
 
31
+ require 'couchquilt/mapper'
31
32
  require 'couchquilt/couch_client'
32
33
  require 'couchquilt/fs'
33
34
 
@@ -3,6 +3,8 @@ require 'uri'
3
3
 
4
4
  module Couchquilt
5
5
  class FS
6
+ include Mapper
7
+
6
8
  # initializes Quilt FS with the database server name
7
9
  def initialize(server_name)
8
10
  @couch = CouchClient.new(server_name)
@@ -101,13 +103,13 @@ module Couchquilt
101
103
 
102
104
  content case named_path(path)
103
105
  when :database_info
104
- @couch.get(database)[remove_extname(id)]
106
+ @couch.get(database)[key_for(id)]
105
107
  when :show_function_result
106
108
  parts.shift
107
- @couch.get(remove_extname(File.join(database, id, "_show", *parts)))
109
+ @couch.get(key_for(File.join(database, id, "_show", *parts)))
108
110
  when :list_function_result
109
111
  parts.shift
110
- @couch.get(remove_extname(File.join(database, id, "_list", *parts)))
112
+ @couch.get(key_for(File.join(database, id, "_list", *parts)))
111
113
  when :view_function_result
112
114
  get_view_result_part(database, id, parts)
113
115
  when :uuid
@@ -134,11 +136,12 @@ module Couchquilt
134
136
 
135
137
  # writes content str to path
136
138
  def write_to(path, str)
139
+ str.strip!
137
140
  database, id, *parts = extract_parts(path)
138
141
  # fetch document
139
142
  doc = @couch.get("#{database}/#{id}")
140
143
  # update the value that the file at path holds
141
- update_value(doc, parts, str)
144
+ map_fs(doc, parts, str)
142
145
  # save document
143
146
  @couch.put("#{database}/#{id}", doc)
144
147
  end
@@ -163,7 +166,7 @@ module Couchquilt
163
166
  # fetch document
164
167
  doc = @couch.get("#{database}/#{id}")
165
168
  # remove object
166
- remove_object(doc, parts)
169
+ map_fs(doc, parts, nil)
167
170
  # save document
168
171
  @couch.put("#{database}/#{id}", doc)
169
172
  end
@@ -197,7 +200,7 @@ module Couchquilt
197
200
  # fetch document
198
201
  doc = @couch.get("#{database}/#{id}")
199
202
  # insert empty object
200
- update_value(doc, parts, {})
203
+ map_fs(doc, parts)
201
204
  # save document
202
205
  @couch.put("#{database}/#{id}", doc)
203
206
  end
@@ -223,7 +226,7 @@ module Couchquilt
223
226
  # fetch document
224
227
  doc = @couch.get("#{database}/#{id}")
225
228
  # remove object
226
- update_value(doc, parts, nil)
229
+ map_fs(doc, parts, nil)
227
230
  # save document
228
231
  @couch.put("#{database}/#{id}", doc)
229
232
  end
@@ -316,21 +319,10 @@ module Couchquilt
316
319
  end
317
320
  end
318
321
 
319
- # maps json contents into contents array
320
- def map_json(doc)
321
- case doc
322
- when Hash
323
- # Hash is mapped to directory
324
- doc.keys.sort.map { |k| append_extname(k, doc[k]) }
325
- when Array
326
- # Array is mapped to directory
327
- doc.map { |k| append_extname(doc.index(k), k) }
328
- end
329
- end
330
-
331
322
  # fetch part of document
332
323
  def get_document_part(database, id, parts = [])
333
324
  doc = @couch.get("#{database}/#{id}")
325
+ parts.map! { |p| key_for p }
334
326
  get_part(doc, parts)
335
327
  end
336
328
 
@@ -339,76 +331,21 @@ module Couchquilt
339
331
  a, view_function_name, *rest = parts
340
332
  view = [id.sub("_design/", ""), "_view", view_function_name].join("/")
341
333
  doc = @couch.get("#{database}/_design/#{view}")
334
+ rest.map! { |p| key_for p }
342
335
  get_part(doc, rest)
343
336
  end
344
337
 
345
338
  # get a part of the document
346
339
  # eg: get_part({ :a => { :b => :c}}, [:a, :b]) #=> :c
347
- # also removes the file extension and gets index of arrays
348
- def get_part(doc, parts)
340
+ def get_part(doc, keys = [])
349
341
  return if doc.nil?
350
342
  doc = doc.dup
351
- parts.each do |part|
352
- case doc
353
- when Hash
354
- doc = doc[remove_extname(part)]
355
- when Array
356
- doc = doc[part.to_i]
357
- end
343
+ keys.each do |key|
344
+ doc = doc[key]
358
345
  end
359
346
  doc
360
347
  end
361
348
 
362
- # updates a part of a hash
363
- # Example:
364
- # update_value({:a => { :b => 'c'}}, [:a, :b], 'cis') #=> {:a => { :b => 'cis'}}
365
- def update_value(hash, keys, value)
366
- key = remove_extname(keys.shift)
367
- if keys.empty?
368
- hash[key] = value
369
- else
370
- hash[key] = update_value(hash[key], keys, value)
371
- end
372
- hash
373
- end
374
-
375
- # removes an object from a hash
376
- # Example:
377
- # remove_object({:a => { :b => 'c'}}, [:a, :b]) #=> {:a => { }}
378
- def remove_object(hash, keys)
379
- key = remove_extname(keys.shift)
380
- if keys.empty?
381
- hash.delete(key)
382
- else
383
- hash[key] = remove_object(hash[key], keys)
384
- end
385
- hash
386
- end
387
-
388
- # remove extname to get the id
389
- def remove_extname(filename)
390
- filename.sub(/((\.(f|i|b))?\.js|\.html)\z/, "")
391
- end
392
-
393
- # Appends extname, that is: builds a filename from key and value.
394
- # Note: values are casted by extension.
395
- def append_extname(key, value)
396
- basename = key.is_a?(Integer) ? "%di" % key : key
397
-
398
- case value
399
- when Float
400
- "#{basename}.f.js"
401
- when Integer
402
- "#{basename}.i.js"
403
- when nil, String
404
- "#{basename}.js"
405
- when true, false
406
- "#{basename}.b.js"
407
- else
408
- basename
409
- end
410
- end
411
-
412
349
  # escapes the value for using as filename
413
350
  def list(array)
414
351
  return [] if array.nil?
@@ -0,0 +1,75 @@
1
+ module Couchquilt
2
+ module Mapper
3
+ # regexp to check wheather a key should be mapped as part of an array
4
+ ARRAY_MAPPING = /\A\d+i(\.js)?\z/
5
+
6
+ # maps json contents into contents array
7
+ def map_json(json = {})
8
+ case json
9
+ when Hash
10
+ json.keys.sort.map { |k| name_for(k, json[k]) }
11
+ when Array
12
+ # using zip for backwards compatibily:
13
+ # in Ruby 1.9 (and 1.8.7) we could simply use
14
+ # json.each_with_index.map { |k,i| ... }
15
+ json.zip((0..json.size).to_a).map { |v,i| name_for(i, v) }
16
+ end
17
+ end
18
+
19
+ # recursively updates a part of a json hash from fs
20
+ def map_fs(json, keys = [], value = :empty)
21
+ return {} if keys.empty?
22
+
23
+ insert_as_array = keys.last =~ /.+\.array\Z/
24
+ key = key_for(keys.shift)
25
+ if keys.empty?
26
+ if value.nil? && json.is_a?(Hash)
27
+ json.delete key
28
+ elsif value.nil? && json.is_a?(Array)
29
+ json.delete_at key
30
+ elsif value == :empty && insert_as_array
31
+ json[key] = []
32
+ elsif value == :empty
33
+ json[key] = {}
34
+ else
35
+ json[key] = value
36
+ end
37
+ else
38
+ json[key] = map_fs(json[key], keys, value)
39
+ end
40
+
41
+ json
42
+ end
43
+
44
+ # remove fs mapping extnames
45
+ # and converts array entry mappings
46
+ def key_for(name)
47
+ if name =~ ARRAY_MAPPING
48
+ name.to_i
49
+ else
50
+ name.sub(/((\.(f|i|b))?\.js|\.html|\.array)\z/, "")
51
+ end
52
+ end
53
+
54
+ # Appends extname, that is: builds a filename from key and value.
55
+ # Note: values are casted by extension.
56
+ def name_for(key, value = nil)
57
+ basename = key.is_a?(Integer) ? "%di" % key : key.to_s
58
+
59
+ case value
60
+ when Array
61
+ "#{basename}.array"
62
+ when Float
63
+ "#{basename}.f.js"
64
+ when Integer
65
+ "#{basename}.i.js"
66
+ when nil, String
67
+ "#{basename}.js"
68
+ when true, false
69
+ "#{basename}.b.js"
70
+ else
71
+ basename
72
+ end
73
+ end
74
+ end
75
+ end
@@ -1,3 +1,3 @@
1
1
  module Couchquilt
2
- VERSION = '0.2.3'
2
+ VERSION = '0.3.0'
3
3
  end
@@ -1,7 +1,7 @@
1
- require File.join(File.dirname(__FILE__), 'spec_helper.rb')
1
+ require File.join(File.dirname(__FILE__), '../spec_helper.rb')
2
2
 
3
3
  # Note: databases, documents and keys with slashes are not supported by now
4
- describe "Quilt" do
4
+ describe Couchquilt::FS do
5
5
  before :all do
6
6
  RestClient.put(File.join(SERVER_NAME, TESTDB), nil)
7
7
 
@@ -171,7 +171,7 @@ describe "Quilt" do
171
171
  @quilt.file?("/#{TESTDB}/document_id").should be_false
172
172
  end
173
173
  it "contents should list documents content" do
174
- @quilt.contents("/#{TESTDB}/document_id").should == ["_id.js", "_rev.js", "array", "boolean.b.js", "empty", "float.f.js", "integer.i.js", "name.js", "object"]
174
+ @quilt.contents("/#{TESTDB}/document_id").should == ["_id.js", "_rev.js", "array.array", "boolean.b.js", "empty", "float.f.js", "integer.i.js", "name.js", "object"]
175
175
  end
176
176
  it "can_mkdir? should return false" do
177
177
  @quilt.can_mkdir?("/#{TESTDB}/document_id").should be_false
@@ -265,7 +265,7 @@ describe "Quilt" do
265
265
  it "write_to should update document" do
266
266
  @quilt.write_to("/#{TESTDB}/document_id/boolean.b.js", "value").should be_true
267
267
  end
268
- it "can_delete? should return false" do
268
+ it "can_delete? should return true" do
269
269
  @quilt.can_delete?("/#{TESTDB}/document_id/boolean.b.js").should be_true
270
270
  end
271
271
  it "delete should update document" do
@@ -295,21 +295,45 @@ describe "Quilt" do
295
295
  end
296
296
 
297
297
  # /database_id/document_id/array
298
- describe "array/" do
298
+ describe "array.array/" do
299
299
  it "directory? should return true" do
300
- @quilt.directory?("/#{TESTDB}/document_id/array").should be_true
300
+ @quilt.directory?("/#{TESTDB}/document_id/array.array").should be_true
301
301
  end
302
302
  it "file? should return false" do
303
- @quilt.file?("/#{TESTDB}/document_id/array").should be_false
303
+ @quilt.file?("/#{TESTDB}/document_id/array.array").should be_false
304
304
  end
305
305
  it "contents should list array content" do
306
- @quilt.contents("/#{TESTDB}/document_id/array").should == ["0i.js"]
306
+ @quilt.contents("/#{TESTDB}/document_id/array.array").should == ["0i.js"]
307
307
  end
308
308
  it "can_mkdir? should return false" do
309
- @quilt.can_mkdir?("/#{TESTDB}/document_id/array").should be_false
309
+ @quilt.can_mkdir?("/#{TESTDB}/document_id/array.array").should be_false
310
310
  end
311
311
  it "can_rmdir? should return false when not empty" do
312
- @quilt.can_rmdir?("/#{TESTDB}/document_id/array").should be_false
312
+ @quilt.can_rmdir?("/#{TESTDB}/document_id/array.array").should be_false
313
+ end
314
+ describe "0i.js" do
315
+ it "directory? should return false" do
316
+ @quilt.directory?("/#{TESTDB}/document_id/array.array/0i.js").should be_false
317
+ end
318
+ it "file? should return true" do
319
+ @quilt.file?("/#{TESTDB}/document_id/array.array/0i.js").should be_true
320
+ end
321
+ it "read_file should return contents" do
322
+ @quilt.read_file("/#{TESTDB}/document_id/array.array/0i.js").should == @document["array"].first
323
+ end
324
+ it "can_write? should return true" do
325
+ @quilt.can_write?("/#{TESTDB}/document_id/array.array/0i.js").should be_true
326
+ end
327
+ it "write_to should update document" do
328
+ @quilt.write_to("/#{TESTDB}/document_id/array.array/0i.js", "value").should be_true
329
+ end
330
+ it "can_delete? should return true" do
331
+ @quilt.can_delete?("/#{TESTDB}/document_id/array.array/0i.js").should be_true
332
+ end
333
+ it "delete should update document" do
334
+ @quilt.delete("/#{TESTDB}/document_id/array.array/0i.js").should be_true
335
+ @quilt.file?("/#{TESTDB}/document_id/array.array/0i.js").should be_false
336
+ end
313
337
  end
314
338
  end
315
339
 
@@ -570,7 +594,7 @@ describe "Quilt" do
570
594
  @quilt.file?("/#{TESTDB}/_design/design_document_id/_view/view_function_name").should be_false
571
595
  end
572
596
  it "contents should list view result document contents" do
573
- @quilt.contents("/#{TESTDB}/_design/design_document_id/_view/view_function_name").should == ["offset.i.js", "rows", "total_rows.i.js"]
597
+ @quilt.contents("/#{TESTDB}/_design/design_document_id/_view/view_function_name").should == ["offset.i.js", "rows.array", "total_rows.i.js"]
574
598
  end
575
599
  it "can_mkdir? should return false" do
576
600
  @quilt.can_mkdir?("/#{TESTDB}/_design/design_document_id/_view/view_function_name").should be_false
@@ -600,87 +624,87 @@ describe "Quilt" do
600
624
  @quilt.can_delete?("/#{TESTDB}/_design/design_document_id/_view/view_function_name/total_rows.i.js").should be_false
601
625
  end
602
626
  end
603
- describe "rows/" do
627
+ describe "rows.array/" do
604
628
  it "directory? should return true" do
605
- @quilt.directory?("/#{TESTDB}/_design/design_document_id/_view/view_function_name/rows").should be_true
629
+ @quilt.directory?("/#{TESTDB}/_design/design_document_id/_view/view_function_name/rows.array").should be_true
606
630
  end
607
631
  it "file? should return false" do
608
- @quilt.file?("/#{TESTDB}/_design/design_document_id/_view/view_function_name/rows").should be_false
632
+ @quilt.file?("/#{TESTDB}/_design/design_document_id/_view/view_function_name/rows.array").should be_false
609
633
  end
610
634
  it "contents should list view rows" do
611
- @quilt.contents("/#{TESTDB}/_design/design_document_id/_view/view_function_name/rows").should == ["0i"]
635
+ @quilt.contents("/#{TESTDB}/_design/design_document_id/_view/view_function_name/rows.array").should == ["0i"]
612
636
  end
613
637
  it "can_mkdir? should return false" do
614
- @quilt.can_mkdir?("/#{TESTDB}/_design/design_document_id/_view/view_function_name/rows").should be_false
638
+ @quilt.can_mkdir?("/#{TESTDB}/_design/design_document_id/_view/view_function_name/rows.array").should be_false
615
639
  end
616
640
  it "can_rmdir? should return false" do
617
- @quilt.can_rmdir?("/#{TESTDB}/_design/design_document_id/_view/view_function_name/rows").should be_false
641
+ @quilt.can_rmdir?("/#{TESTDB}/_design/design_document_id/_view/view_function_name/rows.array").should be_false
618
642
  end
619
643
  describe "0i/" do
620
644
  it "directory? should return true" do
621
- @quilt.directory?("/#{TESTDB}/_design/design_document_id/_view/view_function_name/rows/0i").should be_true
645
+ @quilt.directory?("/#{TESTDB}/_design/design_document_id/_view/view_function_name/rows.array/0i").should be_true
622
646
  end
623
647
  it "file? should return false" do
624
- @quilt.file?("/#{TESTDB}/_design/design_document_id/_view/view_function_name/rows/0i").should be_false
648
+ @quilt.file?("/#{TESTDB}/_design/design_document_id/_view/view_function_name/rows.array/0i").should be_false
625
649
  end
626
650
  it "contents should list view result row content" do
627
- @quilt.contents("/#{TESTDB}/_design/design_document_id/_view/view_function_name/rows/0i").should == ["id.js", "key.js", "value.js"]
651
+ @quilt.contents("/#{TESTDB}/_design/design_document_id/_view/view_function_name/rows.array/0i").should == ["id.js", "key.js", "value.js"]
628
652
  end
629
653
  it "can_mkdir? should return false" do
630
- @quilt.can_mkdir?("/#{TESTDB}/_design/design_document_id/_view/view_function_name/rows/0i").should be_false
654
+ @quilt.can_mkdir?("/#{TESTDB}/_design/design_document_id/_view/view_function_name/rows.array/0i").should be_false
631
655
  end
632
656
  it "can_rmdir? should return false" do
633
- @quilt.can_rmdir?("/#{TESTDB}/_design/design_document_id/_view/view_function_name/rows/0i").should be_false
657
+ @quilt.can_rmdir?("/#{TESTDB}/_design/design_document_id/_view/view_function_name/rows.array/0i").should be_false
634
658
  end
635
659
  describe "id.js" do
636
660
  it "directory? should return false" do
637
- @quilt.directory?("/#{TESTDB}/_design/design_document_id/_view/view_function_name/rows/0i/id.js").should be_false
661
+ @quilt.directory?("/#{TESTDB}/_design/design_document_id/_view/view_function_name/rows.array/0i/id.js").should be_false
638
662
  end
639
663
  it "file? should return true" do
640
- @quilt.file?("/#{TESTDB}/_design/design_document_id/_view/view_function_name/rows/0i/id.js").should be_true
664
+ @quilt.file?("/#{TESTDB}/_design/design_document_id/_view/view_function_name/rows.array/0i/id.js").should be_true
641
665
  end
642
666
  it "read_file should return contents" do
643
- @quilt.read_file("/#{TESTDB}/_design/design_document_id/_view/view_function_name/rows/0i/id.js").should == "document_id"
667
+ @quilt.read_file("/#{TESTDB}/_design/design_document_id/_view/view_function_name/rows.array/0i/id.js").should == "document_id"
644
668
  end
645
669
  it "can_write? should return false" do
646
- @quilt.can_write?("/#{TESTDB}/_design/design_document_id/_view/view_function_name/rows/0i/id.js").should be_false
670
+ @quilt.can_write?("/#{TESTDB}/_design/design_document_id/_view/view_function_name/rows.array/0i/id.js").should be_false
647
671
  end
648
672
  it "can_delete? should return false" do
649
- @quilt.can_delete?("/#{TESTDB}/_design/design_document_id/_view/view_function_name/rows/0i/id.js").should be_false
673
+ @quilt.can_delete?("/#{TESTDB}/_design/design_document_id/_view/view_function_name/rows.array/0i/id.js").should be_false
650
674
  end
651
675
  end
652
676
  describe "key.js" do
653
677
  it "directory? should return false" do
654
- @quilt.directory?("/#{TESTDB}/_design/design_document_id/_view/view_function_name/rows/0i/key.js").should be_false
678
+ @quilt.directory?("/#{TESTDB}/_design/design_document_id/_view/view_function_name/rows.array/0i/key.js").should be_false
655
679
  end
656
680
  it "file? should return true" do
657
- @quilt.file?("/#{TESTDB}/_design/design_document_id/_view/view_function_name/rows/0i/key.js").should be_true
681
+ @quilt.file?("/#{TESTDB}/_design/design_document_id/_view/view_function_name/rows.array/0i/key.js").should be_true
658
682
  end
659
683
  it "read_file should return contents" do
660
- @quilt.read_file("/#{TESTDB}/_design/design_document_id/_view/view_function_name/rows/0i/key.js").should == "This is a name"
684
+ @quilt.read_file("/#{TESTDB}/_design/design_document_id/_view/view_function_name/rows.array/0i/key.js").should == "This is a name"
661
685
  end
662
686
  it "can_write? should return false" do
663
- @quilt.can_write?("/#{TESTDB}/_design/design_document_id/_view/view_function_name/rows/0i/key.js").should be_false
687
+ @quilt.can_write?("/#{TESTDB}/_design/design_document_id/_view/view_function_name/rows.array/0i/key.js").should be_false
664
688
  end
665
689
  it "can_delete? should return false" do
666
- @quilt.can_delete?("/#{TESTDB}/_design/design_document_id/_view/view_function_name/rows/0i/key.js").should be_false
690
+ @quilt.can_delete?("/#{TESTDB}/_design/design_document_id/_view/view_function_name/rows.array/0i/key.js").should be_false
667
691
  end
668
692
  end
669
693
  describe "value.js" do
670
694
  it "directory? should return false" do
671
- @quilt.directory?("/#{TESTDB}/_design/design_document_id/_view/view_function_name/rows/0i/value.js").should be_false
695
+ @quilt.directory?("/#{TESTDB}/_design/design_document_id/_view/view_function_name/rows.array/0i/value.js").should be_false
672
696
  end
673
697
  it "file? should return true" do
674
- @quilt.file?("/#{TESTDB}/_design/design_document_id/_view/view_function_name/rows/0i/value.js").should be_true
698
+ @quilt.file?("/#{TESTDB}/_design/design_document_id/_view/view_function_name/rows.array/0i/value.js").should be_true
675
699
  end
676
700
  it "read_file should return contents" do
677
- @quilt.read_file("/#{TESTDB}/_design/design_document_id/_view/view_function_name/rows/0i/value.js").should == ""
701
+ @quilt.read_file("/#{TESTDB}/_design/design_document_id/_view/view_function_name/rows.array/0i/value.js").should == ""
678
702
  end
679
703
  it "can_write? should return false" do
680
- @quilt.can_write?("/#{TESTDB}/_design/design_document_id/_view/view_function_name/rows/0i/value.js").should be_false
704
+ @quilt.can_write?("/#{TESTDB}/_design/design_document_id/_view/view_function_name/rows.array/0i/value.js").should be_false
681
705
  end
682
706
  it "can_delete? should return false" do
683
- @quilt.can_delete?("/#{TESTDB}/_design/design_document_id/_view/view_function_name/rows/0i/value.js").should be_false
707
+ @quilt.can_delete?("/#{TESTDB}/_design/design_document_id/_view/view_function_name/rows.array/0i/value.js").should be_false
684
708
  end
685
709
  end
686
710
  end
@@ -0,0 +1,184 @@
1
+ require File.join(File.dirname(__FILE__), '../spec_helper.rb')
2
+
3
+ describe Couchquilt::Mapper do
4
+ include Couchquilt::Mapper
5
+
6
+ describe "map_json" do
7
+ describe "value mapping" do
8
+ it "should map string value" do
9
+ result = map_json({ "key" => "value" })
10
+ result.should == ["key.js"]
11
+ end
12
+
13
+ it "should map integer value" do
14
+ result = map_json({ "key" => 1 })
15
+ result.should == ["key.i.js"]
16
+ end
17
+
18
+ it "should map float value" do
19
+ result = map_json({ "key" => 1.1 })
20
+ result.should == ["key.f.js"]
21
+ end
22
+
23
+ it "should map false value" do
24
+ result = map_json({ "key" => false })
25
+ result.should == ["key.b.js"]
26
+ end
27
+
28
+ it "should map true value" do
29
+ result = map_json({ "key" => true })
30
+ result.should == ["key.b.js"]
31
+ end
32
+
33
+ it "should map an array value" do
34
+ result = map_json(["value"])
35
+ result.should == ["0i.js"]
36
+ end
37
+
38
+ it "should map hash value" do
39
+ result = map_json({ "key" => {}})
40
+ result.should == ["key"]
41
+ end
42
+
43
+ it "should map array value" do
44
+ result = map_json({ "key" => []})
45
+ result.should == ["key.array"]
46
+ end
47
+ end
48
+
49
+ describe "nested mapping" do
50
+ it "should map only keys" do
51
+ result = map_json("key" => { "key" => "value" })
52
+ result.should == ["key"]
53
+ end
54
+ end
55
+
56
+ describe "array mapping" do
57
+ it "should map uniq array values" do
58
+ result = map_json(["value1", "value2"])
59
+ result.should == ["0i.js", "1i.js"]
60
+ end
61
+
62
+ it "should map equal array values" do
63
+ result = map_json(["value", "value"])
64
+ result.should == ["0i.js", "1i.js"]
65
+ end
66
+ end
67
+ end
68
+
69
+ describe "map_fs" do
70
+ before do
71
+ @json = { :a => 1, :b => 2}
72
+ end
73
+
74
+ it "should remove all contents for empty keys" do
75
+ result = map_fs(@json)
76
+ result.should == {}
77
+ end
78
+
79
+ # TODO: really spec this
80
+ end
81
+
82
+ describe "key_for" do
83
+ it "should return parsed integer value for 1i" do
84
+ result = key_for("1i")
85
+ result.should == 1
86
+ end
87
+
88
+ it "should return parsed integer value for 1i.js" do
89
+ result = key_for("1i.js")
90
+ result.should == 1
91
+ end
92
+
93
+ it "should remove js extension" do
94
+ result = key_for("key.js")
95
+ result.should == "key"
96
+ end
97
+
98
+ it "should remove f.js extension" do
99
+ result = key_for("key.f.js")
100
+ result.should == "key"
101
+ end
102
+
103
+ it "should remove i.js extension" do
104
+ result = key_for("key.i.js")
105
+ result.should == "key"
106
+ end
107
+
108
+ it "should remove b.js extension" do
109
+ result = key_for("key.b.js")
110
+ result.should == "key"
111
+ end
112
+
113
+ it "should remove html extension" do
114
+ result = key_for("key.html")
115
+ result.should == "key"
116
+ end
117
+ end
118
+
119
+ describe "name_for" do
120
+ describe "with integer key" do
121
+ it "should append i.js for nil value" do
122
+ result = name_for(1)
123
+ result.should == "1i.js"
124
+ end
125
+
126
+ it "should append i.js for string value" do
127
+ result = name_for(1, "value")
128
+ result.should == "1i.js"
129
+ end
130
+
131
+ it "should append i.f.js for float value" do
132
+ result = name_for(1, 1.1)
133
+ result.should == "1i.f.js"
134
+ end
135
+
136
+ it "should append i.i.js for integer value" do
137
+ result = name_for(1, 1)
138
+ result.should == "1i.i.js"
139
+ end
140
+
141
+ it "should append i.b.js for true value" do
142
+ result = name_for(1, true)
143
+ result.should == "1i.b.js"
144
+ end
145
+
146
+ it "should append i.b.js for false value" do
147
+ result = name_for(1, false)
148
+ result.should == "1i.b.js"
149
+ end
150
+ end
151
+
152
+ describe "with other key" do
153
+ it "should append js for nil value" do
154
+ result = name_for("key")
155
+ result.should == "key.js"
156
+ end
157
+
158
+ it "should append js for string value" do
159
+ result = name_for("key", "value")
160
+ result.should == "key.js"
161
+ end
162
+
163
+ it "should append f.js for float value" do
164
+ result = name_for("key", 1.1)
165
+ result.should == "key.f.js"
166
+ end
167
+
168
+ it "should append i.js for integer value" do
169
+ result = name_for("key", 1)
170
+ result.should == "key.i.js"
171
+ end
172
+
173
+ it "should append b.js for true value" do
174
+ result = name_for("key", true)
175
+ result.should == "key.b.js"
176
+ end
177
+
178
+ it "should append b.js for false value" do
179
+ result = name_for("key", false)
180
+ result.should == "key.b.js"
181
+ end
182
+ end
183
+ end
184
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: couch-quilt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - "Johannes J\xC3\xB6rg Schmidt"
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-02-25 00:00:00 +01:00
12
+ date: 2010-02-26 00:00:00 +01:00
13
13
  default_executable: couchquilt
14
14
  dependencies: []
15
15
 
@@ -32,8 +32,10 @@ files:
32
32
  - lib/couchquilt/couch_client.rb
33
33
  - lib/couchquilt/debugged_fs.rb
34
34
  - lib/couchquilt/fs.rb
35
+ - lib/couchquilt/mapper.rb
35
36
  - lib/couchquilt/version.rb
36
- - spec/couchquilt_spec.rb
37
+ - spec/couchquilt/fs_spec.rb
38
+ - spec/couchquilt/mapper_spec.rb
37
39
  - spec/spec_helper.rb
38
40
  has_rdoc: true
39
41
  homepage: http://jo.github.com/quilt
@@ -64,5 +66,6 @@ signing_key:
64
66
  specification_version: 3
65
67
  summary: Access CouchDB from filesystem.
66
68
  test_files:
67
- - spec/couchquilt_spec.rb
68
69
  - spec/spec_helper.rb
70
+ - spec/couchquilt/fs_spec.rb
71
+ - spec/couchquilt/mapper_spec.rb