gom-couchdb-adapter 0.4.2 → 0.4.3

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,7 +1,8 @@
1
1
 
2
2
  module GOM::Storage::CouchDB::View
3
3
 
4
- autoload :Builder, File.join(File.dirname(__FILE__), "view", "builder")
4
+ autoload :BuilderFromClass, File.join(File.dirname(__FILE__), "view", "builder_from_class")
5
+ autoload :BuilderFromProperty, File.join(File.dirname(__FILE__), "view", "builder_from_property")
5
6
  autoload :Pusher, File.join(File.dirname(__FILE__), "view", "pusher")
6
7
 
7
8
  end
@@ -1,6 +1,6 @@
1
1
 
2
2
  # Builds a javascript map-reduce-view out of a class view.
3
- class GOM::Storage::CouchDB::View::Builder
3
+ class GOM::Storage::CouchDB::View::BuilderFromClass
4
4
 
5
5
  def initialize(class_view)
6
6
  @class_view = class_view
@@ -0,0 +1,57 @@
1
+
2
+ # Builds a javascript map-reduce-view out of a property view.
3
+ class GOM::Storage::CouchDB::View::BuilderFromProperty
4
+
5
+ def initialize(property_view)
6
+ @property_view = property_view
7
+ end
8
+
9
+ def map_reduce_view
10
+ build_conditions
11
+ build_emits
12
+ build_emit_string
13
+
14
+ GOM::Storage::Configuration::View::MapReduce.new(
15
+ "function(document) {\n if (#{@conditions.join(" && ")}) {\n emit(#{@emit_string}, null);\n }\n}",
16
+ nil
17
+ )
18
+ end
19
+
20
+ private
21
+
22
+ def build_conditions
23
+ @conditions = [ ]
24
+ @property_view.filter.each do |property_name, condition|
25
+ @conditions << condition(condition.first, property_name, condition.last)
26
+ end
27
+ end
28
+
29
+ def build_emits
30
+ @emits = [ @property_view.properties ].flatten.compact.map do |property|
31
+ "document['#{property}']"
32
+ end
33
+ end
34
+
35
+ def build_emit_string
36
+ @emit_string = case @emits.length
37
+ when 0
38
+ "null"
39
+ when 1
40
+ @emits.first
41
+ else
42
+ "[ " + @emits.join(", ") + " ]"
43
+ end
44
+ end
45
+
46
+ def condition(kind, property_name, value)
47
+ case kind
48
+ when :equals
49
+ "document['#{property_name}'] == " + (value.is_a?(String) ? "'#{value}'" : value.to_s)
50
+ when :greater_than
51
+ "document['#{property_name}'] > " + (value.is_a?(String) ? "'#{value}'" : value.to_s)
52
+ else
53
+ raise ArgumentError, "the specified condition kind '#{kind}' is not supported"
54
+ end
55
+ end
56
+
57
+ end
@@ -32,6 +32,8 @@ class GOM::Storage::CouchDB::View::Pusher
32
32
 
33
33
  def add_view(name, view)
34
34
  case view.class.to_s
35
+ when "GOM::Storage::Configuration::View::Property"
36
+ add_view_by_property_view name, view
35
37
  when "GOM::Storage::Configuration::View::Class"
36
38
  add_view_by_class_view name, view
37
39
  when "GOM::Storage::Configuration::View::MapReduce"
@@ -39,8 +41,13 @@ class GOM::Storage::CouchDB::View::Pusher
39
41
  end
40
42
  end
41
43
 
44
+ def add_view_by_property_view(name, property_view)
45
+ map_reduce_view = GOM::Storage::CouchDB::View::BuilderFromProperty.new(property_view).map_reduce_view
46
+ @design.views << ::CouchDB::Design::View.new(@design, name, map_reduce_view.map, map_reduce_view.reduce)
47
+ end
48
+
42
49
  def add_view_by_class_view(name, class_view)
43
- map_reduce_view = GOM::Storage::CouchDB::View::Builder.new(class_view).map_reduce_view
50
+ map_reduce_view = GOM::Storage::CouchDB::View::BuilderFromClass.new(class_view).map_reduce_view
44
51
  @design.views << ::CouchDB::Design::View.new(@design, name, map_reduce_view.map, map_reduce_view.reduce)
45
52
  end
46
53
 
@@ -1,6 +1,6 @@
1
1
  require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "..", "..", "spec_helper"))
2
2
 
3
- describe GOM::Storage::CouchDB::View::Builder do
3
+ describe GOM::Storage::CouchDB::View::BuilderFromClass do
4
4
 
5
5
  before :each do
6
6
  @class_view = mock GOM::Storage::Configuration::View::Class, :class_name => "Object"
@@ -0,0 +1,24 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "..", "..", "spec_helper"))
2
+
3
+ describe GOM::Storage::CouchDB::View::BuilderFromProperty do
4
+
5
+ before :each do
6
+ @view = mock GOM::Storage::Configuration::View::Property,
7
+ :filter => { :model_class => [ :equals, "GOM::Spec::Object" ], :number => [ :greater_than, 13 ] },
8
+ :properties => [ :_id, :number ]
9
+
10
+ @builder = described_class.new @view
11
+ end
12
+
13
+ describe "map_reduce_view" do
14
+
15
+ it "should return a map reduce view that emits the correct data" do
16
+ view = @builder.map_reduce_view
17
+ view.should be_instance_of(GOM::Storage::Configuration::View::MapReduce)
18
+ view.map.should == "function(document) {\n if (document['model_class'] == 'GOM::Spec::Object' && document['number'] > 13) {\n emit([ document['_id'], document['number'] ], null);\n }\n}"
19
+ view.reduce.should be_nil
20
+ end
21
+
22
+ end
23
+
24
+ end
@@ -5,9 +5,14 @@ describe GOM::Storage::CouchDB::View::Pusher do
5
5
  before :each do
6
6
  @database = mock CouchDB::Database
7
7
 
8
+ @property_view_configuration = GOM::Storage::Configuration::View::Property.new :filter, :properties
8
9
  @class_view_configuration = GOM::Storage::Configuration::View::Class.new "Object"
9
10
  @map_reduce_view_configuration = GOM::Storage::Configuration::View::MapReduce.new "function(document) { }", "function(key, values) { }"
10
- @view_hash = { "test_class_view" => @class_view_configuration, "test_map_reduce_view" => @map_reduce_view_configuration }
11
+ @view_hash = {
12
+ "test_property_view" => @property_view_configuration,
13
+ "test_class_view" => @class_view_configuration,
14
+ "test_map_reduce_view" => @map_reduce_view_configuration
15
+ }
11
16
 
12
17
  @pusher = described_class.new @database, @view_hash
13
18
  end
@@ -18,16 +23,26 @@ describe GOM::Storage::CouchDB::View::Pusher do
18
23
  @design = mock CouchDB::Design, :views => mock(CouchDB::Design::ViewsProxy, :<< => nil), :save => true
19
24
  CouchDB::Design.stub :new => @design
20
25
 
26
+ @property_view = mock CouchDB::Design::View
21
27
  @class_view = mock CouchDB::Design::View
22
28
  @map_reduce_view = mock CouchDB::Design::View
23
29
  CouchDB::Design::View.stub :new do |design, name, map_function, reduce_function|
24
- { "test_class_view" => @class_view, "test_map_reduce_view" => @map_reduce_view }[name]
30
+ {
31
+ "test_property_view" => @property_view,
32
+ "test_class_view" => @class_view,
33
+ "test_map_reduce_view" => @map_reduce_view
34
+ }[name]
25
35
  end
26
36
 
37
+ @property_map_reduce_view = mock GOM::Storage::Configuration::View::MapReduce,
38
+ :map => "function(document) { }", :reduce => nil
39
+ @builder_from_property = mock GOM::Storage::CouchDB::View::BuilderFromProperty, :map_reduce_view => @property_map_reduce_view
40
+ GOM::Storage::CouchDB::View::BuilderFromProperty.stub :new => @builder_from_property
41
+
27
42
  @class_map_reduce_view = mock GOM::Storage::Configuration::View::MapReduce,
28
43
  :map => "function(document) { }", :reduce => nil
29
- @builder = mock GOM::Storage::CouchDB::View::Builder, :map_reduce_view => @class_map_reduce_view
30
- GOM::Storage::CouchDB::View::Builder.stub :new => @builder
44
+ @builder_from_class = mock GOM::Storage::CouchDB::View::BuilderFromClass, :map_reduce_view => @class_map_reduce_view
45
+ GOM::Storage::CouchDB::View::BuilderFromClass.stub :new => @builder_from_class
31
46
  end
32
47
 
33
48
  it "should initialize the design document" do
@@ -35,13 +50,23 @@ describe GOM::Storage::CouchDB::View::Pusher do
35
50
  @pusher.perform
36
51
  end
37
52
 
53
+ it "should initialize the builder with the property view" do
54
+ GOM::Storage::CouchDB::View::BuilderFromProperty.should_receive(:new).with(@property_view_configuration).and_return(@builder_from_property)
55
+ @pusher.perform
56
+ end
57
+
58
+ it "should use the builder to generate a map reduce view out of the property view" do
59
+ @builder_from_property.should_receive(:map_reduce_view).and_return(@property_map_reduce_view)
60
+ @pusher.perform
61
+ end
62
+
38
63
  it "should initialize the builder with the class view" do
39
- GOM::Storage::CouchDB::View::Builder.should_receive(:new).with(@class_view_configuration).and_return(@builder)
64
+ GOM::Storage::CouchDB::View::BuilderFromClass.should_receive(:new).with(@class_view_configuration).and_return(@builder_from_class)
40
65
  @pusher.perform
41
66
  end
42
67
 
43
68
  it "should use the builder to generate a map reduce view out of the class view" do
44
- @builder.should_receive(:map_reduce_view).and_return(@class_map_reduce_view)
69
+ @builder_from_class.should_receive(:map_reduce_view).and_return(@class_map_reduce_view)
45
70
  @pusher.perform
46
71
  end
47
72
 
@@ -5,45 +5,4 @@ require 'rspec'
5
5
  require 'gom/spec'
6
6
 
7
7
  require File.expand_path(File.join(File.dirname(__FILE__), "..", "lib", "gom", "couchdb-adapter"))
8
-
9
- GOM::Storage.configure {
10
- storage {
11
- name :test_storage
12
- adapter :couchdb
13
- database "test"
14
- delete_database_if_exists true
15
- create_database_if_missing true
16
- view {
17
- name :test_object_class_view
18
- kind :class
19
- model_class GOM::Spec::Object
20
- }
21
- view {
22
- name :test_map_view
23
- kind :map_reduce
24
- map_function """
25
- function(document) {
26
- if (document['number'] == 11) {
27
- emit(document['_id'], null);
28
- }
29
- }
30
- """
31
- }
32
- view {
33
- name :test_map_reduce_view
34
- kind :map_reduce
35
- map_function """
36
- function(document) {
37
- if (document['number']) {
38
- emit(document['_id'], document['number']);
39
- }
40
- }
41
- """
42
- reduce_function """
43
- function(keys, values, rereduce) {
44
- return sum(values);
45
- }
46
- """
47
- }
48
- }
49
- }
8
+ require File.join(File.dirname(__FILE__), "storage_configuration")
@@ -0,0 +1,51 @@
1
+
2
+ GOM::Storage.configure {
3
+ storage {
4
+ name :test_storage
5
+ adapter :couchdb
6
+ database "test"
7
+ delete_database_if_exists true
8
+ create_database_if_missing true
9
+ view {
10
+ name :test_property_view
11
+ kind :property
12
+ filter {
13
+ model_class :equals, "GOM::Spec::Object"
14
+ number :greater_than, 13
15
+ }
16
+ properties :_id
17
+ }
18
+ view {
19
+ name :test_object_class_view
20
+ kind :class
21
+ model_class GOM::Spec::Object
22
+ }
23
+ view {
24
+ name :test_map_view
25
+ kind :map_reduce
26
+ map_function """
27
+ function(document) {
28
+ if (document['number'] == 11) {
29
+ emit(document['_id'], null);
30
+ }
31
+ }
32
+ """
33
+ }
34
+ view {
35
+ name :test_map_reduce_view
36
+ kind :map_reduce
37
+ map_function """
38
+ function(document) {
39
+ if (document['number']) {
40
+ emit(document['_id'], document['number']);
41
+ }
42
+ }
43
+ """
44
+ reduce_function """
45
+ function(keys, values, rereduce) {
46
+ return sum(values);
47
+ }
48
+ """
49
+ }
50
+ }
51
+ }
metadata CHANGED
@@ -1,140 +1,139 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: gom-couchdb-adapter
3
- version: !ruby/object:Gem::Version
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.3
4
5
  prerelease:
5
- version: 0.4.2
6
6
  platform: ruby
7
- authors:
8
- - "Philipp Br\xC3\xBCll"
7
+ authors:
8
+ - Philipp Brüll
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
-
13
- date: 2011-05-30 00:00:00 +02:00
12
+ date: 2011-08-15 00:00:00.000000000 +02:00
14
13
  default_executable:
15
- dependencies:
16
- - !ruby/object:Gem::Dependency
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
17
16
  name: gom
18
- prerelease: false
19
- requirement: &id001 !ruby/object:Gem::Requirement
17
+ requirement: &16512820 !ruby/object:Gem::Requirement
20
18
  none: false
21
- requirements:
22
- - - ">="
23
- - !ruby/object:Gem::Version
24
- version: 0.4.1
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: 0.4.2
25
23
  type: :runtime
26
- version_requirements: *id001
27
- - !ruby/object:Gem::Dependency
28
- name: couchdb
29
24
  prerelease: false
30
- requirement: &id002 !ruby/object:Gem::Requirement
25
+ version_requirements: *16512820
26
+ - !ruby/object:Gem::Dependency
27
+ name: couchdb
28
+ requirement: &16512320 !ruby/object:Gem::Requirement
31
29
  none: false
32
- requirements:
33
- - - ">="
34
- - !ruby/object:Gem::Version
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
35
33
  version: 0.1.3
36
34
  type: :runtime
37
- version_requirements: *id002
38
- - !ruby/object:Gem::Dependency
39
- name: rspec
40
35
  prerelease: false
41
- requirement: &id003 !ruby/object:Gem::Requirement
36
+ version_requirements: *16512320
37
+ - !ruby/object:Gem::Dependency
38
+ name: rspec
39
+ requirement: &16511840 !ruby/object:Gem::Requirement
42
40
  none: false
43
- requirements:
44
- - - ">="
45
- - !ruby/object:Gem::Version
46
- version: "2"
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '2'
47
45
  type: :development
48
- version_requirements: *id003
49
- - !ruby/object:Gem::Dependency
50
- name: reek
51
46
  prerelease: false
52
- requirement: &id004 !ruby/object:Gem::Requirement
47
+ version_requirements: *16511840
48
+ - !ruby/object:Gem::Dependency
49
+ name: reek
50
+ requirement: &16511340 !ruby/object:Gem::Requirement
53
51
  none: false
54
- requirements:
55
- - - ">="
56
- - !ruby/object:Gem::Version
57
- version: "1.2"
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '1.2'
58
56
  type: :development
59
- version_requirements: *id004
60
- description: CouchDB storage adapter for the General Object Mapper. Currently, version 1.0 of CouchDB is supported.
57
+ prerelease: false
58
+ version_requirements: *16511340
59
+ description: CouchDB storage adapter for the General Object Mapper. Currently, version
60
+ 1.0 of CouchDB is supported.
61
61
  email: b.phifty@gmail.com
62
62
  executables: []
63
-
64
63
  extensions: []
65
-
66
- extra_rdoc_files:
64
+ extra_rdoc_files:
67
65
  - README.rdoc
68
- files:
66
+ files:
69
67
  - README.rdoc
70
68
  - LICENSE
71
69
  - Rakefile
72
- - lib/gom/storage/couchdb/adapter.rb
73
- - lib/gom/storage/couchdb/view/builder.rb
70
+ - lib/gom/storage/couchdb/view/builder_from_class.rb
74
71
  - lib/gom/storage/couchdb/view/pusher.rb
75
- - lib/gom/storage/couchdb/counter.rb
72
+ - lib/gom/storage/couchdb/view/builder_from_property.rb
76
73
  - lib/gom/storage/couchdb/view.rb
74
+ - lib/gom/storage/couchdb/collection/fetcher.rb
75
+ - lib/gom/storage/couchdb/counter.rb
76
+ - lib/gom/storage/couchdb/adapter.rb
77
77
  - lib/gom/storage/couchdb/saver.rb
78
- - lib/gom/storage/couchdb/fetcher.rb
78
+ - lib/gom/storage/couchdb/remover.rb
79
79
  - lib/gom/storage/couchdb/draft/builder.rb
80
80
  - lib/gom/storage/couchdb/draft.rb
81
- - lib/gom/storage/couchdb/remover.rb
82
- - lib/gom/storage/couchdb/collection/fetcher.rb
81
+ - lib/gom/storage/couchdb/fetcher.rb
83
82
  - lib/gom/storage/couchdb/collection.rb
84
83
  - lib/gom/storage/couchdb.rb
85
- - lib/gom/couchdb-adapter.rb
86
84
  - lib/gom/storage.rb
87
- - spec/lib/gom/storage/couchdb/fetcher_spec.rb
85
+ - lib/gom/couchdb-adapter.rb
86
+ - spec/lib/gom/storage/couchdb/view/builder_from_class_spec.rb
87
+ - spec/lib/gom/storage/couchdb/view/builder_from_property_spec.rb
88
88
  - spec/lib/gom/storage/couchdb/view/pusher_spec.rb
89
- - spec/lib/gom/storage/couchdb/view/builder_spec.rb
90
- - spec/lib/gom/storage/couchdb/draft/builder_spec.rb
91
- - spec/lib/gom/storage/couchdb/counter_spec.rb
92
89
  - spec/lib/gom/storage/couchdb/collection/fetcher_spec.rb
90
+ - spec/lib/gom/storage/couchdb/adapter_spec.rb
91
+ - spec/lib/gom/storage/couchdb/counter_spec.rb
92
+ - spec/lib/gom/storage/couchdb/draft/builder_spec.rb
93
+ - spec/lib/gom/storage/couchdb/fetcher_spec.rb
93
94
  - spec/lib/gom/storage/couchdb/remover_spec.rb
94
95
  - spec/lib/gom/storage/couchdb/saver_spec.rb
95
- - spec/lib/gom/storage/couchdb/adapter_spec.rb
96
96
  - spec/spec_helper.rb
97
- - spec/acceptance/map_spec.rb
98
- - spec/acceptance/map_reduce_spec.rb
97
+ - spec/storage_configuration.rb
99
98
  - spec/acceptance/adapter_spec.rb
99
+ - spec/acceptance/map_reduce_spec.rb
100
+ - spec/acceptance/map_spec.rb
100
101
  has_rdoc: true
101
102
  homepage: http://github.com/phifty/gom-couchdb-adapter
102
103
  licenses: []
103
-
104
104
  post_install_message:
105
105
  rdoc_options: []
106
-
107
- require_paths:
106
+ require_paths:
108
107
  - lib
109
- required_ruby_version: !ruby/object:Gem::Requirement
108
+ required_ruby_version: !ruby/object:Gem::Requirement
110
109
  none: false
111
- requirements:
112
- - - ">="
113
- - !ruby/object:Gem::Version
114
- version: "0"
115
- required_rubygems_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ! '>='
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
115
  none: false
117
- requirements:
118
- - - ">="
119
- - !ruby/object:Gem::Version
120
- version: "0"
116
+ requirements:
117
+ - - ! '>='
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
121
120
  requirements: []
122
-
123
121
  rubyforge_project: gom-couchdb-adapter
124
- rubygems_version: 1.6.1
122
+ rubygems_version: 1.6.2
125
123
  signing_key:
126
124
  specification_version: 3
127
125
  summary: CouchDB storage adapter for the General Object Mapper.
128
- test_files:
129
- - spec/lib/gom/storage/couchdb/fetcher_spec.rb
126
+ test_files:
127
+ - spec/lib/gom/storage/couchdb/view/builder_from_class_spec.rb
128
+ - spec/lib/gom/storage/couchdb/view/builder_from_property_spec.rb
130
129
  - spec/lib/gom/storage/couchdb/view/pusher_spec.rb
131
- - spec/lib/gom/storage/couchdb/view/builder_spec.rb
132
- - spec/lib/gom/storage/couchdb/draft/builder_spec.rb
133
- - spec/lib/gom/storage/couchdb/counter_spec.rb
134
130
  - spec/lib/gom/storage/couchdb/collection/fetcher_spec.rb
131
+ - spec/lib/gom/storage/couchdb/adapter_spec.rb
132
+ - spec/lib/gom/storage/couchdb/counter_spec.rb
133
+ - spec/lib/gom/storage/couchdb/draft/builder_spec.rb
134
+ - spec/lib/gom/storage/couchdb/fetcher_spec.rb
135
135
  - spec/lib/gom/storage/couchdb/remover_spec.rb
136
136
  - spec/lib/gom/storage/couchdb/saver_spec.rb
137
- - spec/lib/gom/storage/couchdb/adapter_spec.rb
138
- - spec/acceptance/map_spec.rb
139
- - spec/acceptance/map_reduce_spec.rb
140
137
  - spec/acceptance/adapter_spec.rb
138
+ - spec/acceptance/map_reduce_spec.rb
139
+ - spec/acceptance/map_spec.rb