dolly 0.5.7 → 0.6.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2a6f4cc93cbfb6e3efc584188c23775282d6bd95
4
- data.tar.gz: 2bf8db9298eee2e91925d1edc9e1536e1ce435db
3
+ metadata.gz: f310bb10ff18ec990f101c2f6ad8470ba4ecfe24
4
+ data.tar.gz: 25ac2e2e160160ddb1fbab3ec95d0554772fe4c1
5
5
  SHA512:
6
- metadata.gz: 90b551dbf530f8e36fa6f35e1ab307478096514db83bd78d603e4bdf62e93aedcb7c5d6b4a0c4822d5ff171f5521c306f60c477620a76b5df72c583d4e447f2b
7
- data.tar.gz: 2a3e019741cb1e7d6b193537bcce6bb8ade95b9a4231a40b27e094b4254bd47ac054f95fbe6d985d3232843ea75cc771a5bda35fc03755dbc77c7514805cecfa
6
+ metadata.gz: 190ec76bedcb882c2ca6e66e4f791861ad66773308f3994d664b9e3af301751924cb551c89bee17ac123984a45ebcd4e1aac3f88a8f2ff24b216efe10d590092
7
+ data.tar.gz: 54397eca72bcd6f50ab064e94cde974f48f6d5b5348a8e8eebe22b22aa92b8c225908444203ff041c13453306394b8c562584b15c67f2c513f222d40a7571c85
@@ -93,7 +93,8 @@ module Dolly
93
93
  property = Property.new(options)
94
94
 
95
95
  define_method(name) do
96
- property.value = @doc[name.to_s] || default_value
96
+ key = name.to_s
97
+ property.value = @doc.has_key?(key) ? @doc[key] : default_value
97
98
  property.value
98
99
  end
99
100
 
@@ -115,6 +116,7 @@ module Dolly
115
116
  end
116
117
 
117
118
  def init_properties options = {}
119
+ raise Dolly::ResourceNotFound if options['error'] == 'not_found'
118
120
  #TODO: right now not listed properties will be ignored
119
121
  options.each do |k, v|
120
122
  next unless respond_to? :"#{k}="
@@ -1,5 +1,6 @@
1
1
  require "active_model/naming"
2
2
 
3
+ #TODO: remove this module to be part of Dolly::Document
3
4
  module Dolly
4
5
  module NameSpace
5
6
  include ActiveModel::Naming
@@ -9,12 +10,12 @@ module Dolly
9
10
  end
10
11
 
11
12
  def base_id id
12
- return id unless id =~ /^#{name_paramitized}\//
13
- id.match("[^/]+[/](.+)")[1]
13
+ id = URI.unescape id
14
+ id.sub %r~^#{name_paramitized}/~, ''
14
15
  end
15
16
 
16
17
  def namespace id
17
- return id if id =~ /^#{name_paramitized}/
18
+ return id if id =~ %r~^#{name_paramitized}/~
18
19
  "#{name_paramitized}/#{id}"
19
20
  end
20
21
  end
data/lib/dolly/query.rb CHANGED
@@ -12,9 +12,14 @@ module Dolly
12
12
 
13
13
  DESIGN_DOC = "dolly"
14
14
 
15
- def find *ids
16
- response = default_view(keys: ids.map{ |id| [name_paramitized, base_id(id)] }).parsed_response
17
- ids.count > 1 ? Collection.new(response, name.constantize) : self.new.from_json(response)
15
+ def find *keys
16
+ query_hash = { keys: keys.map{|key| namespace key} }
17
+
18
+ if keys.count > 1
19
+ build_collection( query_hash )
20
+ else
21
+ self.new.from_json( database.all_docs(query_hash).parsed_response )
22
+ end
18
23
  rescue NoMethodError => err
19
24
  if err.message == "undefined method `[]' for nil:NilClass"
20
25
  raise Dolly::ResourceNotFound
@@ -23,22 +28,26 @@ module Dolly
23
28
  end
24
29
  end
25
30
 
31
+ def default_query_args
32
+ {startkey: "#{name_paramitized}/", endkey: "#{name_paramitized}/{}"}
33
+ end
34
+
26
35
  def all
27
- build_collection startkey: [name_paramitized,nil], endkey: [name_paramitized,{}]
36
+ build_collection default_query_args
28
37
  end
29
38
 
30
39
  def first limit = 1
31
- res = build_collection startkey: [name_paramitized,nil], endkey: [name_paramitized,{}], limit: limit
40
+ res = build_collection default_query_args.merge( limit: 1)
32
41
  limit == 1 ? res.first : res
33
42
  end
34
43
 
35
44
  def last limit = 1
36
- res = build_collection startkey: [name_paramitized,{}], endkey: [name_paramitized,nil], limit: limit, descending: true
45
+ res = build_collection({startkey: default_query_args[:endkey], endkey: default_query_args[:startkey], limit: limit, descending: true})
37
46
  limit == 1 ? res.first : res
38
47
  end
39
48
 
40
49
  def build_collection q
41
- res = default_view(q)
50
+ res = database.all_docs(q)
42
51
  Collection.new res.parsed_response, name.constantize
43
52
  end
44
53
 
@@ -47,10 +56,6 @@ module Dolly
47
56
  Collection.new res.parsed_response, name.constantize
48
57
  end
49
58
 
50
- def default_view options = {}
51
- view default_doc, options
52
- end
53
-
54
59
  def view doc, options = {}
55
60
  options.merge! include_docs: true
56
61
  database.get doc, options
@@ -69,5 +74,6 @@ module Dolly
69
74
  def self.included(base)
70
75
  base.extend ClassMethods
71
76
  end
77
+
72
78
  end
73
79
  end
data/lib/dolly/request.rb CHANGED
@@ -47,6 +47,11 @@ module Dolly
47
47
  tools("_uuids", opts)["uuids"]
48
48
  end
49
49
 
50
+ def all_docs data = {}
51
+ data = values_to_json data.merge( include_docs: true )
52
+ request :get, '_all_docs', {query: data}
53
+ end
54
+
50
55
  private
51
56
  def tools path, opts = nil
52
57
  data = {}
data/lib/dolly/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Dolly
2
- VERSION = "0.5.7"
2
+ VERSION = "0.6.0"
3
3
  end
data/lib/tasks/db.rake CHANGED
@@ -1,30 +1,8 @@
1
1
  namespace :db do
2
2
  desc "Will create if missing database and add default views"
3
3
  task setup: :environment do
4
- FIND_VIEW = { "find" => { "map" => "(d)->\n if d._id\n [str, t, id] = d._id.match /([^/]+)[/](.+)/\n emit [t, id], 1 if t and id"} }
5
-
6
- default_doc = {
7
- "language" => "coffeescript",
8
- "views" => FIND_VIEW
9
- }.freeze
10
-
11
4
  Dolly::Document.database.put "", nil
12
-
13
- remote_doc = begin
14
- JSON.parse Dolly::Document.database.get(Dolly::Document.design_doc).parsed_response
15
- rescue Dolly::ResourceNotFound
16
- nil
17
- end
18
-
19
- doc = if remote_doc
20
- remote_doc["views"].merge! FIND_VIEW
21
- remote_doc
22
- else
23
- default_doc
24
- end
25
-
26
- res = Dolly::Document.database.put Dolly::Document.design_doc, doc.to_json
27
- puts "design document #{Dolly::Document.design_doc} was created/updated."
5
+ puts "Database created"
28
6
  end
29
7
 
30
8
  desc "Will update design document with what is on db/designs/*.coffee"
@@ -9,8 +9,7 @@ class FooBar < Dolly::Document
9
9
  end
10
10
 
11
11
  class DocumentTest < ActiveSupport::TestCase
12
- DB_BASE_PATH = "http://localhost:5984/test/_design/test/_view/".freeze
13
- VIEW_DOC = "find".freeze
12
+ DB_BASE_PATH = "http://localhost:5984/test".freeze
14
13
 
15
14
  def setup
16
15
  data = {foo: 'Foo', bar: 'Bar', type: 'foo_bar'}
@@ -19,17 +18,24 @@ class DocumentTest < ActiveSupport::TestCase
19
18
 
20
19
  view_resp = build_view_response [data]
21
20
  empty_resp = build_view_response []
21
+ not_found_resp = generic_response [{ key: "foo_bar/2", error: "not_found" }]
22
22
  @multi_resp = build_view_response all_docs
23
23
 
24
24
  build_request [["foo_bar","1"]], view_resp
25
25
  build_request [["foo_bar","2"]], empty_resp
26
26
  build_request [["foo_bar","1"],["foo_bar","2"]], @multi_resp
27
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
29
- FakeWeb.register_uri :get, "#{view_base_path}?startkey=%5B%22foo_bar%22%2Cnull%5D&endkey=%5B%22foo_bar%22%2C%7B%7D%5D&limit=1&include_docs=true", body: view_resp.to_json
30
- FakeWeb.register_uri :get, "#{view_base_path}?startkey=%5B%22foo_bar%22%2Cnull%5D&endkey=%5B%22foo_bar%22%2C%7B%7D%5D&limit=2&include_docs=true", body: @multi_resp.to_json
31
- FakeWeb.register_uri :get, "#{view_base_path}?startkey=%5B%22foo_bar%22%2C%7B%7D%5D&endkey=%5B%22foo_bar%22%2Cnull%5D&limit=1&descending=true&include_docs=true", body: view_resp.to_json
32
- FakeWeb.register_uri :get, "#{view_base_path}?startkey=%5B%22foo_bar%22%2C%7B%7D%5D&endkey=%5B%22foo_bar%22%2Cnull%5D&limit=2&descending=true&include_docs=true", body: @multi_resp.to_json
28
+ #TODO: Mock Dolly::Request to return helper with expected response. request builder can be tested by itself.
29
+ FakeWeb.register_uri :get, "#{query_base_path}?startkey=%22foo_bar%2F%22&endkey=%22foo_bar%2F%7B%7D%22&include_docs=true", body: @multi_resp.to_json
30
+ FakeWeb.register_uri :get, "#{query_base_path}?startkey=%22foo_bar%2F%22&endkey=%22foo_bar%2F%7B%7D%22&limit=1&include_docs=true", body: view_resp.to_json
31
+ FakeWeb.register_uri :get, "#{query_base_path}?endkey=%22foo_bar%2F%22&startkey=%22foo_bar%2F%7B%7D%22&limit=1&descending=true&include_docs=true", body: view_resp.to_json
32
+ FakeWeb.register_uri :get, "#{query_base_path}?startkey=%22foo_bar%2F%22&endkey=%22foo_bar%22%2C%7B%7D&limit=2&include_docs=true", body: @multi_resp.to_json
33
+ FakeWeb.register_uri :get, "#{query_base_path}?endkey=%22foo_bar%2F%22&startkey=%22foo_bar%2F%7B%7D%22&limit=2&descending=true&include_docs=true", body: @multi_resp.to_json
34
+ FakeWeb.register_uri :get, "#{query_base_path}?keys=%5B%22foo_bar%2F1%22%5D&include_docs=true", body: view_resp.to_json
35
+ FakeWeb.register_uri :get, "#{query_base_path}?keys=%5B%5D&include_docs=true", body: not_found_resp.to_json
36
+ FakeWeb.register_uri :get, "#{query_base_path}?keys=%5B%22foo_bar%2Ferror%22%5D&include_docs=true", body: 'error', status: ["500", "Error"]
37
+ FakeWeb.register_uri :get, "#{query_base_path}?keys=%5B%22foo_bar%2F1%22%2C%22foo_bar%2F2%22%5D&include_docs=true", body: @multi_resp.to_json
38
+ FakeWeb.register_uri :get, "#{query_base_path}?keys=%5B%22foo_bar%2F2%22%5D&include_docs=true", body: not_found_resp.to_json
33
39
  end
34
40
 
35
41
  test 'with timestamps!' do
@@ -62,15 +68,15 @@ class DocumentTest < ActiveSupport::TestCase
62
68
 
63
69
  test 'empty find should raise error' do
64
70
  assert_raise Dolly::ResourceNotFound do
65
- FakeWeb.register_uri :get, "#{view_base_path}?keys=%5B%5D&include_docs=true", :status => ["404", "Not Found"]
71
+ FakeWeb.register_uri :get, "#{query_base_path}?keys=%5B%5D&include_docs=true", :status => ["404", "Not Found"]
66
72
  foo = FooBar.find
67
73
  end
68
74
  end
69
75
 
70
76
  test 'error on server raises Dolly::ServerError' do
71
77
  assert_raise Dolly::ServerError do
72
- FakeWeb.register_uri :get, "#{view_base_path}?keys=%5B%5D&include_docs=true", :status => ["500", "Error"]
73
- foo = FooBar.find
78
+ FakeWeb.register_uri :get, "#{query_base_path}?keys=", :status => ["500", "Error"]
79
+ foo = FooBar.find 'error'
74
80
  end
75
81
  end
76
82
 
@@ -100,6 +106,11 @@ class DocumentTest < ActiveSupport::TestCase
100
106
  assert_equal false, foo.respond_to?(:type)
101
107
  end
102
108
 
109
+ test 'can find with fixnum id' do
110
+ foo = FooBar.find 1
111
+ assert_equal 'Foo', foo.foo
112
+ end
113
+
103
114
  test 'with default will return default value on nil' do
104
115
  foo = FooBar.find "1"
105
116
  assert_equal 1, foo.with_default
@@ -201,6 +212,10 @@ class DocumentTest < ActiveSupport::TestCase
201
212
  end
202
213
 
203
214
  private
215
+ def generic_response rows, count = 1
216
+ {total_rows: count, offset:0, rows: rows}
217
+ end
218
+
204
219
  def build_view_response properties
205
220
  rows = properties.map.with_index do |v, i|
206
221
  {
@@ -210,16 +225,16 @@ class DocumentTest < ActiveSupport::TestCase
210
225
  doc: {_id: "foo_bar/#{i}", _rev: SecureRandom.hex}.merge!(v)
211
226
  }
212
227
  end
213
- {total_rows: properties.count, offset:0, rows: rows}
228
+ generic_response rows, properties.count
214
229
  end
215
230
 
216
231
  def build_request keys, body, view_name = 'foo_bar'
217
232
  query = "keys=#{CGI::escape keys.to_s.gsub(' ','')}&" unless keys.blank?
218
- FakeWeb.register_uri :get, "#{view_base_path}?#{query.to_s}include_docs=true", body: body.to_json
233
+ FakeWeb.register_uri :get, "#{query_base_path}?#{query.to_s}include_docs=true", body: body.to_json
219
234
  end
220
235
 
221
- def view_base_path
222
- "#{DB_BASE_PATH}#{VIEW_DOC}"
236
+ def query_base_path
237
+ "#{DB_BASE_PATH}/_all_docs"
223
238
  end
224
239
 
225
240
  end