couch_rest_adapter 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 80e7938a0be83ff4164281d505b5d390c9aaa8c5
4
- data.tar.gz: 7fbc6b79a8c57daa074c63fa587e89a77777e851
3
+ metadata.gz: e7b96cd3e242f65785013feb23396a745182ba30
4
+ data.tar.gz: 557bf704b3d52af56e3978b8d645ac9ee8bd6530
5
5
  SHA512:
6
- metadata.gz: 073bead5da7207675919c12a47a4e69e7b7c7717f24eed043cece1b771b8916319a26447b3bfcf9adf13307145d418ef2c732ecb1555c2f4dedc3039af21e5af
7
- data.tar.gz: 1fa69ac85f78e035b2bf87a10e86806bd0c823eca93ca53359b68c015044fa3b80a9b752cba4904b160fcf7e4e5acd21c942abe5d6b0e366d1be13f5b64cbe5d
6
+ metadata.gz: 6d166a2f936e01cf0bf6772693328c7b99676c3cd39194a61a6756f82db4c4fe6a3b4f004648b1ab6e9f9b0670c1f51c2cbd1f76e3260498da3cde69992bbf2e
7
+ data.tar.gz: dd4816b8a31ad8acf54ea95ac72d46907b9f0bad9fdaf1e10d3f6557179a7446345d49b4dd8cd3fb745feed1cac7909615bd27747fa0c383b94955a25850adee
@@ -32,6 +32,9 @@ module CouchRestAdapter
32
32
  _attributes.keys.map{ |attr| [attr, "#{attr}="] }.flatten
33
33
  end
34
34
 
35
+ def base_id
36
+ File.basename self['_id']
37
+ end
35
38
  end
36
39
  end
37
40
 
@@ -1,9 +1,31 @@
1
+ using CouchRestAdapter::Helpers
2
+
3
+ #TODO write some tests for this module
1
4
  module CouchRestAdapter
2
5
  module DocumentManagement
6
+ module ClassMethods
7
+ def namespace=(namespace)
8
+ @@_namespace = namespace
9
+ end
10
+
11
+ def namespace
12
+ @@_namespace ||= object_name
13
+ end
14
+
15
+ def object_name
16
+ model_name.singular
17
+ end
18
+ end
19
+
20
+ def self.included(base)
21
+ base.extend(ClassMethods)
22
+ end
23
+
3
24
  UUID_DOC = '_uuids/?'
4
25
 
5
- def next_id
6
- File.join self.class.object_name, uuids.first
26
+ #override this method if you want to set your own id
27
+ def set_id
28
+ uuids.first
7
29
  end
8
30
 
9
31
  def uuids opts = {}
@@ -16,6 +38,11 @@ module CouchRestAdapter
16
38
  CGI.unescape(opts.to_query)
17
39
  end
18
40
 
41
+ def _set_id_and_namespace
42
+ self['_id'] = set_id if self['_id'].blank?
43
+ self['_id'] = self['_id'].namespace_me self.class.namespace
44
+ end
45
+
19
46
  end
20
47
  end
21
48
 
@@ -10,8 +10,8 @@ module CouchRestAdapter
10
10
  end
11
11
 
12
12
  module ClassMethods
13
- #TODO: We can get this from Rails.application.class.name
14
- DEFAULT_DESIGN = 'amcoid'
13
+
14
+ DEFAULT_DESIGN = Rails.application.class.to_s.split("::").first.downcase
15
15
 
16
16
  def find_by_attribute attr_name, value, doc_name = nil
17
17
  document_name = 'by_attribute'
@@ -1,3 +1,3 @@
1
1
  module CouchRestAdapter
2
- VERSION = "0.3.1"
2
+ VERSION = "0.3.2"
3
3
  end
@@ -26,9 +26,6 @@ module CouchRestAdapter
26
26
  #TODO: add custom callback calls.
27
27
  define_model_callbacks :save
28
28
 
29
- #TODO set_id not be a callback. Need a better way to do this, possibilty using class methods
30
- before_save :set_id
31
-
32
29
  def initialize attributes = {}
33
30
  raise NotImplementedError if abstract?
34
31
  super attributes
@@ -65,6 +62,7 @@ module CouchRestAdapter
65
62
  def save
66
63
  return false if invalid?
67
64
  return false unless run_callbacks(:save)
65
+ _set_id_and_namespace
68
66
  super
69
67
  end
70
68
 
@@ -88,13 +86,5 @@ module CouchRestAdapter
88
86
  def abstract?
89
87
  self.class.to_s == 'CouchRestAdapter::Base'
90
88
  end
91
-
92
- def self.object_name
93
- self.model_name.singular
94
- end
95
-
96
- def set_id
97
- self['_id'] = next_id if self['_id'].blank?
98
- end
99
89
  end
100
90
  end
@@ -0,0 +1,18 @@
1
+ require 'test_helper'
2
+
3
+ class CouchRestAdapter::AttributeMethodTest < ActiveSupport::TestCase
4
+
5
+ def setup
6
+ @foo = FooBar.new
7
+ end
8
+
9
+ test 'base_id' do
10
+ @foo['_id'] = 'some_namespace/value_desired'
11
+ assert_equal @foo.base_id, 'value_desired'
12
+ end
13
+
14
+ end
15
+
16
+ class FooBar < CouchRestAdapter::Base
17
+ use_default_database
18
+ end
@@ -0,0 +1,33 @@
1
+ require 'test_helper'
2
+
3
+ class CouchRestAdapter::DocumentManagementTest < ActiveSupport::TestCase
4
+
5
+ def setup
6
+ @foo = FooBar.new
7
+ @klass = @foo.class
8
+ end
9
+
10
+ test 'class_method object_name returns model_name in singular form' do
11
+ assert_equal @klass.object_name, @klass.model_name.singular
12
+ end
13
+
14
+ test 'class_method namespace defaults to object_name' do
15
+ assert_equal @klass.namespace, @klass.object_name
16
+ end
17
+
18
+ test 'class_method namespace= allows override of namespace' do
19
+ @klass.namespace = 'something'
20
+ assert_equal @klass.namespace, 'something'
21
+ @klass.namespace = @klass.object_name
22
+ end
23
+
24
+ test '_set_id_and_namespace will set the _id variable with an id and namespace' do
25
+ @foo._set_id_and_namespace
26
+ assert @foo['_id'].start_with?("foo_bar/")
27
+ end
28
+ end
29
+
30
+ class FooBar < CouchRestAdapter::Base
31
+ use_default_database
32
+ end
33
+