boombera 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.1.2
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{boombera}
8
- s.version = "0.1.1"
8
+ s.version = "0.1.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["John Wilger"]
@@ -30,6 +30,7 @@ Gem::Specification.new do |s|
30
30
  "boombera.gemspec",
31
31
  "lib/boombera.rb",
32
32
  "lib/boombera/content_item.rb",
33
+ "lib/boombera/information.rb",
33
34
  "spec/integration/boombera_spec.rb",
34
35
  "spec/lib/boombera/content_item_spec.rb",
35
36
  "spec/lib/boombera_spec.rb",
@@ -1,48 +1,18 @@
1
1
  $:.unshift(File.expand_path(File.dirname(__FILE__)))
2
2
  require 'couchrest'
3
+
4
+ # Boombera constant is defined first as `Class.new` so that the definitions in
5
+ # the required files can use `class Boombera::Whatever` even with the `require`
6
+ # statements appearing before the actual class definition.
7
+ Boombera = Class.new
8
+
3
9
  require 'boombera/content_item'
10
+ require 'boombera/information'
4
11
 
5
12
  class Boombera
6
13
  VersionMismatch = Class.new(StandardError)
7
14
 
8
- class << self
9
- def version
10
- File.read(File.expand_path(File.join(File.dirname(__FILE__), '..', 'VERSION')))
11
- end
12
-
13
- def database_version(db)
14
- db.get('_design/boombera')['gem_version']
15
- rescue RestClient::ResourceNotFound
16
- nil
17
- end
18
-
19
- def install_design_doc!(database)
20
- db = CouchRest.database!(database)
21
- design = design_doc
22
- existing = db.documents(:key => '_design/boombera')['rows'].first
23
- design['_rev'] = existing['value']['rev'] unless existing.nil?
24
- db.save_doc(design)
25
- end
26
-
27
- def design_doc
28
- {
29
- '_id' => '_design/boombera',
30
- 'language' => 'javascript',
31
- 'gem_version' => version,
32
- 'views' => {
33
- 'content_map' => {
34
- 'map' => <<-EOF
35
- function(doc) {
36
- if (doc['path']) {
37
- emit(doc.path, doc.path);
38
- }
39
- }
40
- EOF
41
- }
42
- }
43
- }
44
- end
45
- end
15
+ extend Boombera::Information
46
16
 
47
17
  attr_reader :db
48
18
 
@@ -52,8 +22,8 @@ class Boombera
52
22
  end
53
23
 
54
24
  def put(path, body)
55
- content_item = get(path) || ContentItem.new(:path => path, :database => db)
56
- content_item.body = body
25
+ content_item = get(path) and content_item.body = body
26
+ content_item ||= ContentItem.new(path, body, db)
57
27
  content_item.save
58
28
  end
59
29
 
@@ -1,33 +1,41 @@
1
- class Boombera
2
- class ContentItem < CouchRest::Document
3
- class << self
4
- def get(path, database)
5
- rows = database.view('boombera/content_map', :key => path)['rows']
6
- return nil if rows.empty?
7
- id = rows.first['id']
8
- new(database.get(id))
9
- end
1
+ class Boombera::ContentItem < CouchRest::Document
2
+ class << self
3
+ def get(path, database)
4
+ rows = database.view('boombera/content_map', :key => path)['rows']
5
+ return nil if rows.empty?
6
+ id = rows.first['id']
7
+ new(database.get(id))
10
8
  end
9
+ end
11
10
 
12
- def initialize(pkeys = {})
13
- @database = if pkeys.respond_to?(:database)
14
- pkeys.database
15
- else
16
- pkeys.delete(:database)
17
- end
18
- super
19
- end
11
+ attr_accessor :body
12
+ attr_reader :path
20
13
 
21
- def path
22
- self[:path]
14
+ def initialize(doc_or_path, body = nil, database = nil)
15
+ case doc_or_path
16
+ when CouchRest::Document
17
+ @database = doc_or_path.database
18
+ super(doc_or_path)
19
+ when String
20
+ @database = database
21
+ super(:path => doc_or_path, :body => body)
22
+ else
23
+ raise ArgumentError, "doc_or_path must either be an instance of CouchRest::Document or a String"
23
24
  end
25
+ end
24
26
 
25
- def body
26
- self[:body]
27
- end
27
+ # :nodoc:
28
+ def path
29
+ self[:path]
30
+ end
28
31
 
29
- def body=(new_body)
30
- self[:body] = new_body
31
- end
32
+ # :nodoc:
33
+ def body
34
+ self[:body]
35
+ end
36
+
37
+ # :nodoc:
38
+ def body=(new_body)
39
+ self[:body] = new_body
32
40
  end
33
41
  end
@@ -0,0 +1,45 @@
1
+ module Boombera::Information
2
+ def version
3
+ File.read(File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'VERSION')))
4
+ end
5
+
6
+ def database_version(db)
7
+ doc = current_design_doc(db)
8
+ doc && doc['gem_version']
9
+ end
10
+
11
+ def install_design_doc!(database)
12
+ db = CouchRest.database!(database)
13
+ existing = current_design_doc(db)
14
+ design = design_doc
15
+ design['_rev'] = existing['_rev'] unless existing.nil?
16
+ db.save_doc(design)
17
+ end
18
+
19
+ def design_doc
20
+ {
21
+ '_id' => '_design/boombera',
22
+ 'language' => 'javascript',
23
+ 'gem_version' => version,
24
+ 'views' => {
25
+ 'content_map' => {
26
+ 'map' => <<-EOF
27
+ function(doc) {
28
+ if (doc['path']) {
29
+ emit(doc.path, doc.path);
30
+ }
31
+ }
32
+ EOF
33
+ }
34
+ }
35
+ }
36
+ end
37
+
38
+ private
39
+
40
+ def current_design_doc(db)
41
+ db.get('_design/boombera')
42
+ rescue RestClient::ResourceNotFound
43
+ nil
44
+ end
45
+ end
@@ -11,7 +11,7 @@ describe Boombera::ContentItem do
11
11
  .and_return(view_result)
12
12
  db.should_receive(:get) \
13
13
  .with('123') \
14
- .and_return({'path' => '/foo', 'body' => 'bar'})
14
+ .and_return(CouchRest::Document.new('path' => '/foo', 'body' => 'bar'))
15
15
  result = Boombera::ContentItem.get('/foo', db)
16
16
  result.path.should == '/foo'
17
17
  result.body.should == 'bar'
@@ -20,11 +20,9 @@ describe Boombera::ContentItem do
20
20
  end
21
21
 
22
22
  describe '.new' do
23
- context 'when passed a plain hash' do
24
- it 'sets the database from the params hash' do
25
- content_item = Boombera::ContentItem.new(:path => '/foo',
26
- :body => 'bar',
27
- :database => :the_database)
23
+ context 'when passed a path, body and database' do
24
+ it 'sets the database from the database argument' do
25
+ content_item = Boombera::ContentItem.new('/foo', 'bar', :the_database)
28
26
  content_item.database.should == :the_database
29
27
  end
30
28
  end
@@ -41,21 +39,21 @@ describe Boombera::ContentItem do
41
39
 
42
40
  describe '#path' do
43
41
  it 'returns the path from the associated document' do
44
- content = Boombera::ContentItem.new('path' => '/index.html')
42
+ content = Boombera::ContentItem.new('/index.html')
45
43
  content.path.should == '/index.html'
46
44
  end
47
45
  end
48
46
 
49
47
  describe '#body' do
50
48
  it 'returns the body from the associated document' do
51
- content = Boombera::ContentItem.new('body' => 'foo bar baz')
49
+ content = Boombera::ContentItem.new('/foo', 'foo bar baz')
52
50
  content.body.should == 'foo bar baz'
53
51
  end
54
52
  end
55
53
 
56
54
  describe '#body=' do
57
55
  it 'overwrites the current contents of the document body' do
58
- content = Boombera::ContentItem.new('body' => 'foo')
56
+ content = Boombera::ContentItem.new('/foo', 'not bar')
59
57
  content.body = 'bar'
60
58
  content.body.should == 'bar'
61
59
  end
@@ -38,7 +38,6 @@ describe Boombera do
38
38
  let(:content_item) { mock(Boombera::ContentItem) }
39
39
  let(:content_item_save_expectations) do
40
40
  lambda {
41
- content_item.should_receive(:body=).with('bar')
42
41
  content_item.should_receive(:save).and_return(true)
43
42
  boombera = Boombera.new('boombera_test')
44
43
  boombera.put('/foo', 'bar').should == true
@@ -48,6 +47,7 @@ describe Boombera do
48
47
  context "to an existing path" do
49
48
  it 'updates and saves the existing content item' do
50
49
  Boombera::ContentItem.should_receive(:get).with('/foo', db).and_return(content_item)
50
+ content_item.should_receive(:body=).with('bar')
51
51
  content_item_save_expectations.call
52
52
  end
53
53
  end
@@ -56,7 +56,7 @@ describe Boombera do
56
56
  it 'creates and saves the existing content item' do
57
57
  Boombera::ContentItem.stub!(:get => nil)
58
58
  Boombera::ContentItem.should_receive(:new) \
59
- .with(:path => '/foo', :database => db) \
59
+ .with('/foo', 'bar', db) \
60
60
  .and_return(content_item)
61
61
  content_item_save_expectations.call
62
62
  end
@@ -78,10 +78,11 @@ describe Boombera do
78
78
  CouchRest.should_receive(:database!) \
79
79
  .with('boombera_test') \
80
80
  .and_return(db)
81
- db.should_receive(:documents) \
82
- .with(:key => '_design/boombera') \
83
- .and_return({'rows' => []})
84
- db.should_receive(:save_doc).with(Boombera.design_doc)
81
+ db.should_receive(:get) \
82
+ .with('_design/boombera') \
83
+ .and_raise(RestClient::ResourceNotFound)
84
+ db.should_receive(:save_doc) \
85
+ .with(Boombera.design_doc)
85
86
  Boombera.install_design_doc!('boombera_test')
86
87
  end
87
88
  end
@@ -91,9 +92,9 @@ describe Boombera do
91
92
  CouchRest.should_receive(:database!) \
92
93
  .with('boombera_test') \
93
94
  .and_return(db)
94
- db.should_receive(:documents) \
95
- .with(:key => '_design/boombera') \
96
- .and_return({'rows' => [{'value' => {'rev' => '123'}}]})
95
+ db.should_receive(:get) \
96
+ .with('_design/boombera') \
97
+ .and_return({'_id' => '_design/boombera', '_rev' => '123'})
97
98
  db.should_receive(:save_doc).with(Boombera.design_doc.merge('_rev' => '123'))
98
99
  Boombera.install_design_doc!('boombera_test')
99
100
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: boombera
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.1
5
+ version: 0.1.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - John Wilger
@@ -146,6 +146,7 @@ files:
146
146
  - boombera.gemspec
147
147
  - lib/boombera.rb
148
148
  - lib/boombera/content_item.rb
149
+ - lib/boombera/information.rb
149
150
  - spec/integration/boombera_spec.rb
150
151
  - spec/lib/boombera/content_item_spec.rb
151
152
  - spec/lib/boombera_spec.rb
@@ -164,7 +165,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
164
165
  requirements:
165
166
  - - ">="
166
167
  - !ruby/object:Gem::Version
167
- hash: -3756736400322567909
168
+ hash: 2330631431022327628
168
169
  segments:
169
170
  - 0
170
171
  version: "0"