dolly 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/dolly/connection.rb +8 -7
- data/lib/dolly/db_config.rb +18 -0
- data/lib/dolly/name_space.rb +5 -0
- data/lib/dolly/query.rb +3 -2
- data/lib/dolly/railtie.rb +12 -0
- data/lib/dolly/request.rb +16 -6
- data/lib/dolly/version.rb +1 -1
- data/lib/dolly.rb +1 -0
- data/lib/exceptions/dolly.rb +5 -0
- data/lib/tasks/db.rake +26 -0
- data/test/document_test.rb +15 -11
- data/test/dummy/config/couchdb.yml +1 -3
- data/test/dummy/log/test.log +3579 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b43649cf960832da484ca9d972ad118313156e01
|
4
|
+
data.tar.gz: ae8cbcfa0414e80e2a7ba881737f810e3582c232
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 94107541b5742c644dcb657d35ef95a68ae79beeaf17a41ead87eb54d5371e52fa1d6ec9db65d6d6384103a2d9ed0f748df82a2d52cf4689010f45304976b354
|
7
|
+
data.tar.gz: fcc8db3b3e45d28d670e98dc851c79d7ab06f977f932795f00b443e06d750539eb6a20a5c752e6eefe666b57528a4bd5aaef1c3c7eda61b3a482a0a8d3c836f9
|
data/lib/dolly/connection.rb
CHANGED
@@ -1,28 +1,29 @@
|
|
1
1
|
require "dolly/request"
|
2
2
|
require "dolly/name_space"
|
3
|
+
require "dolly/db_config"
|
3
4
|
|
4
5
|
module Dolly
|
5
6
|
module Connection
|
6
7
|
include Dolly::NameSpace
|
8
|
+
include Dolly::DbConfig
|
9
|
+
|
10
|
+
@@design_doc = nil
|
7
11
|
|
8
12
|
def database
|
9
|
-
@database ||= Request.new(
|
13
|
+
@database ||= Request.new(env)
|
10
14
|
end
|
11
15
|
|
12
16
|
def database_name value
|
13
|
-
|
17
|
+
@@database_name ||= value
|
14
18
|
end
|
15
19
|
|
16
20
|
def default_doc
|
17
|
-
"#{design_doc}/_view
|
21
|
+
"#{design_doc}/_view/find"
|
18
22
|
end
|
19
23
|
|
20
24
|
def design_doc
|
21
|
-
"_design/#{
|
25
|
+
"_design/#{env["design"]}"
|
22
26
|
end
|
23
27
|
|
24
|
-
def set_design_doc value
|
25
|
-
@@design_doc = value
|
26
|
-
end
|
27
28
|
end
|
28
29
|
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Dolly
|
2
|
+
module DbConfig
|
3
|
+
attr_accessor :config_file
|
4
|
+
|
5
|
+
def parse_config
|
6
|
+
YAML::load( ERB.new( File.read(config_file) ).result)
|
7
|
+
end
|
8
|
+
|
9
|
+
def env
|
10
|
+
parse_config[Rails.env]
|
11
|
+
end
|
12
|
+
|
13
|
+
def config_file
|
14
|
+
root = Rails.root || File.expand_path("../../../test/dummy/", __FILE__)
|
15
|
+
File.join(root, 'config', 'couchdb.yml')
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/dolly/name_space.rb
CHANGED
data/lib/dolly/query.rb
CHANGED
@@ -17,7 +17,7 @@ module Dolly
|
|
17
17
|
DESIGN_DOC = "dolly"
|
18
18
|
|
19
19
|
def find *ids
|
20
|
-
response = default_view(keys: ids.map{|id|
|
20
|
+
response = default_view(keys: ids.map{ |id| [name_paramitized, base_id(id)] }).parsed_response
|
21
21
|
ids.count > 1 ? Collection.new(response, name.constantize) : self.new.from_json(response)
|
22
22
|
rescue NoMethodError => err
|
23
23
|
if err.message == "undefined method `[]' for nil:NilClass"
|
@@ -28,7 +28,8 @@ module Dolly
|
|
28
28
|
end
|
29
29
|
|
30
30
|
def all
|
31
|
-
|
31
|
+
q = {startkey: [name_paramitized,nil], endkey: [name_paramitized,{}]}
|
32
|
+
Collection.new default_view(q).parsed_response, name.constantize
|
32
33
|
end
|
33
34
|
|
34
35
|
def default_view options = {}
|
data/lib/dolly/request.rb
CHANGED
@@ -10,15 +10,19 @@ module Dolly
|
|
10
10
|
attr_accessor :database_name, :host, :port
|
11
11
|
|
12
12
|
def initialize options = {}
|
13
|
-
@host = options[
|
14
|
-
@port = options[
|
15
|
-
@database_name = options[:database_name]
|
13
|
+
@host = options["host"] || DEFAULT_HOST
|
14
|
+
@port = options["port"] || DEFAULT_PORT
|
16
15
|
|
17
|
-
|
16
|
+
@database_name = options["name"]
|
17
|
+
@username = options["username"]
|
18
|
+
@password = options["password"]
|
19
|
+
|
20
|
+
self.class.base_uri "#{protocol}://#{auth_info}#{host}:#{port}"
|
18
21
|
end
|
19
22
|
|
20
|
-
def get resource, data
|
21
|
-
|
23
|
+
def get resource, data = nil
|
24
|
+
q = {query: values_to_json(data)} if data
|
25
|
+
request :get, resource, q
|
22
26
|
end
|
23
27
|
|
24
28
|
def put resource, data
|
@@ -34,6 +38,11 @@ module Dolly
|
|
34
38
|
end
|
35
39
|
|
36
40
|
private
|
41
|
+
def auth_info
|
42
|
+
return "" unless @username.present?
|
43
|
+
"#{@username}:#{@password}@"
|
44
|
+
end
|
45
|
+
|
37
46
|
def values_to_json hash
|
38
47
|
hash.reduce({}){|h, v| h[v.first] = v.last.to_json; h}
|
39
48
|
end
|
@@ -43,6 +52,7 @@ module Dolly
|
|
43
52
|
end
|
44
53
|
|
45
54
|
def request method, resource, data = nil
|
55
|
+
data ||= {}
|
46
56
|
headers = { 'Content-Type' => 'application/json' }
|
47
57
|
response = self.class.send method, full_path(resource), data.merge(headers: headers)
|
48
58
|
if response.code == 404
|
data/lib/dolly/version.rb
CHANGED
data/lib/dolly.rb
CHANGED
data/lib/exceptions/dolly.rb
CHANGED
data/lib/tasks/db.rake
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
namespace :db do
|
2
|
+
desc "Will create if missing database and add default views"
|
3
|
+
task setup: :environment do
|
4
|
+
VIEW_DOC = {
|
5
|
+
language: "coffeescript",
|
6
|
+
views: {
|
7
|
+
find: {
|
8
|
+
map: "(d)->\n if d._id\n [str, t, id] = d._id.match /([^/]+)[/](.+)/\n emit [t, id], 1 if t and id"
|
9
|
+
}
|
10
|
+
}
|
11
|
+
}.freeze
|
12
|
+
|
13
|
+
Dolly::Document.database.put "", nil
|
14
|
+
|
15
|
+
remote_doc = begin
|
16
|
+
JSON.parse Dolly::Document.database.get(Dolly::Document.design_doc).parsed_response
|
17
|
+
rescue Dolly::ResourceNotFound
|
18
|
+
{}
|
19
|
+
end
|
20
|
+
|
21
|
+
doc = VIEW_DOC.merge remote_doc
|
22
|
+
|
23
|
+
Dolly::Document.database.put Dolly::Document.design_doc, doc.to_json
|
24
|
+
puts "design document #{Dolly::Document.design_doc} was created/updated."
|
25
|
+
end
|
26
|
+
end
|
data/test/document_test.rb
CHANGED
@@ -1,9 +1,6 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
class FooBar < Dolly::Document
|
4
|
-
database_name 'test'
|
5
|
-
set_design_doc 'test'
|
6
|
-
|
7
4
|
property :foo, :bar
|
8
5
|
property :with_default, default: 1
|
9
6
|
property :boolean, class_name: TrueClass, default: true
|
@@ -12,19 +9,23 @@ class FooBar < Dolly::Document
|
|
12
9
|
end
|
13
10
|
|
14
11
|
class DocumentTest < ActiveSupport::TestCase
|
12
|
+
DB_BASE_PATH = "http://localhost:5984/test/_design/test/_view/".freeze
|
13
|
+
VIEW_DOC = "find".freeze
|
15
14
|
|
16
15
|
def setup
|
17
16
|
data = {foo: 'Foo', bar: 'Bar', type: 'foo_bar'}
|
17
|
+
|
18
18
|
all_docs = [ {foo: 'Foo B', bar: 'Bar B', type: 'foo_bar'}, {foo: 'Foo A', bar: 'Bar A', type: 'foo_bar'}]
|
19
19
|
|
20
20
|
view_resp = build_view_response [data]
|
21
21
|
empty_resp = build_view_response []
|
22
22
|
@multi_resp = build_view_response all_docs
|
23
23
|
|
24
|
-
build_request ["foo_bar
|
25
|
-
build_request ["foo_bar
|
26
|
-
build_request ["foo_bar
|
27
|
-
|
24
|
+
build_request [["foo_bar","1"]], view_resp
|
25
|
+
build_request [["foo_bar","2"]], empty_resp
|
26
|
+
build_request [["foo_bar","1"],["foo_bar","2"]], @multi_resp
|
27
|
+
|
28
|
+
FakeWeb.register_uri :get, "#{view_base_path}?startkey=%5B%22foo_bar%22%2Cnull%5D&endkey=%5B%22foo_bar%22%2C%7B%7D%5D&include_docs=true", body: @multi_resp.to_json
|
28
29
|
end
|
29
30
|
|
30
31
|
test 'with timestamps!' do
|
@@ -44,14 +45,14 @@ class DocumentTest < ActiveSupport::TestCase
|
|
44
45
|
|
45
46
|
test 'empty find should raise error' do
|
46
47
|
assert_raise Dolly::ResourceNotFound do
|
47
|
-
FakeWeb.register_uri :get, "
|
48
|
+
FakeWeb.register_uri :get, "#{view_base_path}?keys=%5B%5D&include_docs=true", :status => ["404", "Not Found"]
|
48
49
|
foo = FooBar.find
|
49
50
|
end
|
50
51
|
end
|
51
52
|
|
52
53
|
test 'error on server raises Dolly::ServerError' do
|
53
54
|
assert_raise Dolly::ServerError do
|
54
|
-
FakeWeb.register_uri :get, "
|
55
|
+
FakeWeb.register_uri :get, "#{view_base_path}?keys=%5B%5D&include_docs=true", :status => ["500", "Error"]
|
55
56
|
foo = FooBar.find
|
56
57
|
end
|
57
58
|
end
|
@@ -139,9 +140,12 @@ class DocumentTest < ActiveSupport::TestCase
|
|
139
140
|
end
|
140
141
|
|
141
142
|
def build_request keys, body, view_name = 'foo_bar'
|
142
|
-
base_url = "http://localhost:5984/test/_design/test/_view/foo_bar"
|
143
143
|
query = "keys=#{CGI::escape keys.to_s.gsub(' ','')}&" unless keys.blank?
|
144
|
-
FakeWeb.register_uri :get, "#{
|
144
|
+
FakeWeb.register_uri :get, "#{view_base_path}?#{query.to_s}include_docs=true", body: body.to_json
|
145
|
+
end
|
146
|
+
|
147
|
+
def view_base_path
|
148
|
+
"#{DB_BASE_PATH}#{VIEW_DOC}"
|
145
149
|
end
|
146
150
|
|
147
151
|
end
|