couch_rest_adapter 0.1.3 → 0.2.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: 2bf012dec6d067d4a5144daceaf14bfc26382c80
4
- data.tar.gz: 8677857029defad6d38982ff2ecb439e0afb39ce
3
+ metadata.gz: ce2435af2159efb62cb05eb28a983283e637549e
4
+ data.tar.gz: 73da822c0f41cbe0a25f0b36427ffca9c4138664
5
5
  SHA512:
6
- metadata.gz: 133ec2836ad03f0a93c1fceb60b2129f40150e3ae282f2c8d324859e944ba030e89a34e44272980ce94a694d2220a93aed96909fef35378ef5bc0b3ae9fe3626
7
- data.tar.gz: 2ddb54433b4c43631eb511470adbe1e76db88835bcdec02d61f52389f435d63382d2c4c28b3038cac2ae7cf85a66f3bc5bebb8aa3940ab6e81fcb04a300213f7
6
+ metadata.gz: b3d2ea59b7a6f3e4d82fad656758a52f5e1b909a984afb39a584fb80d36fc55756a0614d6dc2314c1ef6d588b6e9a3cd8111bcf0fba005af7a7d5731421d9500
7
+ data.tar.gz: 38e677ac00092705b26001b5cc52e6c99a7ad7d4d23e6e1d45f261a7b7202bc2c4ab32763b2b2ee2b58deb9c3fefcf2f4bb6fc6f668ec32ad060b96bf78a8941
data/README.rdoc CHANGED
@@ -1,5 +1,7 @@
1
1
  = CouchRestAdapter
2
2
 
3
+ This is a realy early version.
4
+
3
5
  This project rocks and uses MIT-LICENSE.
4
6
 
5
7
  == Requirements
@@ -13,7 +15,34 @@ This project rocks and uses MIT-LICENSE.
13
15
 
14
16
  === In Gemfile
15
17
 
16
- gem 'couch_rest_adapter', git: 'git://github.com/amco/couch_rest_adapter.git'
18
+ gem 'couch_rest_adapter'
19
+
20
+ === Sample config/database.yml
21
+
22
+ defaults: &defaults
23
+ host: localhost
24
+ port: 5984
25
+ protocol: http
26
+
27
+ development:
28
+ <<: *defaults
29
+ name: db
30
+
31
+ test:
32
+ <<: *defaults
33
+ name: db_test
34
+
35
+
36
+ === CouchViews
37
+
38
+ In order for ```Model.all``` to workyou need to add a view like (code in cs):
39
+
40
+ (d) ->
41
+ split_id = d._id.split('/')
42
+ t = split_id[0]
43
+ emit t, d
44
+
45
+ We are planning to add a rake task that setup this view on install.
17
46
 
18
47
  === Model Declaration
19
48
 
@@ -27,3 +56,4 @@ This project rocks and uses MIT-LICENSE.
27
56
  * Allow setting new attributes with ```model.attribute = value```
28
57
  * More test coverage
29
58
  * Add tasks for pushing design documents
59
+ * Add support for basic database http auth.
@@ -24,6 +24,10 @@ module CouchRestAdapter
24
24
  root = Rails.env == 'test' ? File.expand_path("../../../test/dummy/", __FILE__) : Rails.root
25
25
  File.join(root, 'config', 'database.yml')
26
26
  end
27
+
28
+ def default_design_doc
29
+ parse_config[Rails.env]['design_doc']
30
+ end
27
31
  end
28
32
  end
29
33
  end
@@ -3,7 +3,7 @@ module CouchRestAdapter
3
3
  UUID_DOC = '_uuids/?'
4
4
 
5
5
  def next_id
6
- File.join self.class.model_name, uuids.first
6
+ File.join self.class.object_name, uuids.first
7
7
  end
8
8
 
9
9
  def uuids opts = {}
@@ -13,9 +13,9 @@ module CouchRestAdapter
13
13
  #TODO: We can get this from Rails.application.class.name
14
14
  DEFAULT_DESIGN = 'amcoid'
15
15
 
16
- def query_view name
17
- doc = name.namespace_me DEFAULT_DESIGN
18
- view(doc, {key: model_name})['rows'].map{ |res| new res['doc'] }
16
+ def query_view name, doc_name = nil
17
+ doc = name.namespace_me(doc_name || DEFAULT_DESIGN)
18
+ view(doc, {key: object_name})['rows'].map{ |res| new res['doc'] }
19
19
  end
20
20
 
21
21
  #TODO: method for reduce, and filters
@@ -1,3 +1,3 @@
1
1
  module CouchRestAdapter
2
- VERSION = "0.1.3"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -1,3 +1,4 @@
1
+ require 'active_model'
1
2
  require 'couchrest'
2
3
  require 'couch_rest_adapter/attribute_method'
3
4
  require 'couch_rest_adapter/query_views'
@@ -10,30 +11,38 @@ using CouchRestAdapter::Helpers
10
11
 
11
12
  module CouchRestAdapter
12
13
  class Base < CouchRest::Document
14
+ extend ActiveModel::Naming
15
+ include ActiveModel::Validations
16
+ include ActiveModel::Conversion
13
17
  include ActiveSupport::Callbacks
14
18
  include AttributeMethod
15
- include QueryViews
19
+ include DbConfig
16
20
  include DocumentManagement
17
21
  include DbConfig
22
+ include QueryViews
18
23
 
19
24
  #TODO: add custom callback calls.
20
- define_callbacks :before_save
25
+ define_model_callbacks :save
26
+
27
+ #TODO set_id not be a callback. Need a better way to do this, possibilty using class methods
28
+ before_save :set_id
21
29
 
22
- def initialize attrs = nil
30
+ def initialize attributes = {}
31
+ @attributes = attributes
23
32
  raise NotImplementedError if abstract?
24
- super attrs
33
+ super attributes
25
34
  end
26
35
 
27
36
  def self.all
28
- query_view('all')
37
+ query_view('all', default_design_doc)
29
38
  end
30
39
 
31
40
  def self.all_by_type
32
- query_view('by_type')
41
+ query_view('by_type', default_design_doc)
33
42
  end
34
43
 
35
44
  def self.find doc_id
36
- new database.get( doc_id.namespace_me(model_name) ).to_hash
45
+ new database.get( doc_id.namespace_me(object_name) ).to_hash
37
46
  end
38
47
 
39
48
  def self.use_default_database
@@ -41,10 +50,9 @@ module CouchRestAdapter
41
50
  end
42
51
 
43
52
  def save
44
- run_callbacks :before_save do
45
- self['_id'] = next_id if self['_id'].blank?
46
- super
47
- end
53
+ return false if invalid?
54
+ return false unless run_callbacks(:save)
55
+ super
48
56
  end
49
57
 
50
58
  def method_missing method, *args, &block
@@ -55,14 +63,25 @@ module CouchRestAdapter
55
63
  end
56
64
  end
57
65
 
66
+ def persisted?
67
+ true
68
+ end
69
+
70
+ def read_attribute_for_validation(key)
71
+ @attributes[key]
72
+ end
73
+
58
74
  protected
59
75
  def abstract?
60
76
  self.class.to_s == 'CouchRestAdapter::Base'
61
77
  end
62
78
 
63
- def self.model_name
64
- name.underscore
79
+ def self.object_name
80
+ self.model_name.singular
65
81
  end
66
82
 
83
+ def set_id
84
+ self['_id'] = next_id if self['_id'].blank?
85
+ end
67
86
  end
68
87
  end
@@ -98,3 +98,10 @@ class CouchRestAdapterTest < ActiveSupport::TestCase
98
98
 
99
99
  end
100
100
 
101
+ class LintTest < ActiveModel::TestCase
102
+ include ActiveModel::Lint::Tests
103
+
104
+ def setup
105
+ @model = FooBar.new
106
+ end
107
+ end
@@ -2,6 +2,7 @@ defaults: &defaults
2
2
  host: localhost
3
3
  port: 5984
4
4
  protocol: http
5
+ design_doc: 'my_app'
5
6
 
6
7
  development:
7
8
  <<: *defaults
@@ -1312,3 +1312,183 @@ CouchRestAdapterTest: test_update_to_attr=_will_persist
1312
1312
  --------------------------------------------------------------
1313
1313
  CouchRestAdapterTest: test_will_add_class_underscorename_to_id
1314
1314
  --------------------------------------------------------------
1315
+ --------------------------------------------------------------
1316
+ CouchRestAdapterTest: test_attributes_are_available_as_methods
1317
+ --------------------------------------------------------------
1318
+ ---------------------------------------------------------
1319
+ CouchRestAdapterTest: test_can_not_instantiate_base_class
1320
+ ---------------------------------------------------------
1321
+ ---------------------------------------------------------
1322
+ CouchRestAdapterTest: test_find_will_work_with_partial_id
1323
+ ---------------------------------------------------------
1324
+ -------------------------------------------------------------
1325
+ CouchRestAdapterTest: test_one_can_update_existing_attributes
1326
+ -------------------------------------------------------------
1327
+ --------------------------------------------------------------------------------------------------
1328
+ CouchRestAdapterTest: test_query_all_by_type_will_return_array_of_docs_with_type_set_to_model_name
1329
+ --------------------------------------------------------------------------------------------------
1330
+ ----------------------------------------------------------------------
1331
+ CouchRestAdapterTest: test_query_all_will_bring_array_of_Foo_instances
1332
+ ----------------------------------------------------------------------
1333
+ -------------------------------------------------------
1334
+ CouchRestAdapterTest: test_update_to_attr=_will_persist
1335
+ -------------------------------------------------------
1336
+ --------------------------------------------------------------
1337
+ CouchRestAdapterTest: test_will_add_class_underscorename_to_id
1338
+ --------------------------------------------------------------
1339
+ --------------------------------------------------------------
1340
+ CouchRestAdapterTest: test_attributes_are_available_as_methods
1341
+ --------------------------------------------------------------
1342
+ ---------------------------------------------------------
1343
+ CouchRestAdapterTest: test_can_not_instantiate_base_class
1344
+ ---------------------------------------------------------
1345
+ ---------------------------------------------------------
1346
+ CouchRestAdapterTest: test_find_will_work_with_partial_id
1347
+ ---------------------------------------------------------
1348
+ -------------------------------------------------------------
1349
+ CouchRestAdapterTest: test_one_can_update_existing_attributes
1350
+ -------------------------------------------------------------
1351
+ --------------------------------------------------------------------------------------------------
1352
+ CouchRestAdapterTest: test_query_all_by_type_will_return_array_of_docs_with_type_set_to_model_name
1353
+ --------------------------------------------------------------------------------------------------
1354
+ ----------------------------------------------------------------------
1355
+ CouchRestAdapterTest: test_query_all_will_bring_array_of_Foo_instances
1356
+ ----------------------------------------------------------------------
1357
+ -------------------------------------------------------
1358
+ CouchRestAdapterTest: test_update_to_attr=_will_persist
1359
+ -------------------------------------------------------
1360
+ --------------------------------------------------------------
1361
+ CouchRestAdapterTest: test_will_add_class_underscorename_to_id
1362
+ --------------------------------------------------------------
1363
+ --------------------------------------------------------------
1364
+ CouchRestAdapterTest: test_attributes_are_available_as_methods
1365
+ --------------------------------------------------------------
1366
+ ---------------------------------------------------------
1367
+ CouchRestAdapterTest: test_can_not_instantiate_base_class
1368
+ ---------------------------------------------------------
1369
+ ---------------------------------------------------------
1370
+ CouchRestAdapterTest: test_find_will_work_with_partial_id
1371
+ ---------------------------------------------------------
1372
+ -------------------------------------------------------------
1373
+ CouchRestAdapterTest: test_one_can_update_existing_attributes
1374
+ -------------------------------------------------------------
1375
+ --------------------------------------------------------------------------------------------------
1376
+ CouchRestAdapterTest: test_query_all_by_type_will_return_array_of_docs_with_type_set_to_model_name
1377
+ --------------------------------------------------------------------------------------------------
1378
+ ----------------------------------------------------------------------
1379
+ CouchRestAdapterTest: test_query_all_will_bring_array_of_Foo_instances
1380
+ ----------------------------------------------------------------------
1381
+ -------------------------------------------------------
1382
+ CouchRestAdapterTest: test_update_to_attr=_will_persist
1383
+ -------------------------------------------------------
1384
+ --------------------------------------------------------------
1385
+ CouchRestAdapterTest: test_will_add_class_underscorename_to_id
1386
+ --------------------------------------------------------------
1387
+ --------------------------------------------------------------
1388
+ CouchRestAdapterTest: test_attributes_are_available_as_methods
1389
+ --------------------------------------------------------------
1390
+ ---------------------------------------------------------
1391
+ CouchRestAdapterTest: test_can_not_instantiate_base_class
1392
+ ---------------------------------------------------------
1393
+ ---------------------------------------------------------
1394
+ CouchRestAdapterTest: test_find_will_work_with_partial_id
1395
+ ---------------------------------------------------------
1396
+ -------------------------------------------------------------
1397
+ CouchRestAdapterTest: test_one_can_update_existing_attributes
1398
+ -------------------------------------------------------------
1399
+ --------------------------------------------------------------------------------------------------
1400
+ CouchRestAdapterTest: test_query_all_by_type_will_return_array_of_docs_with_type_set_to_model_name
1401
+ --------------------------------------------------------------------------------------------------
1402
+ ----------------------------------------------------------------------
1403
+ CouchRestAdapterTest: test_query_all_will_bring_array_of_Foo_instances
1404
+ ----------------------------------------------------------------------
1405
+ -------------------------------------------------------
1406
+ CouchRestAdapterTest: test_update_to_attr=_will_persist
1407
+ -------------------------------------------------------
1408
+ --------------------------------------------------------------
1409
+ CouchRestAdapterTest: test_will_add_class_underscorename_to_id
1410
+ --------------------------------------------------------------
1411
+ --------------------------------------------------------------
1412
+ CouchRestAdapterTest: test_attributes_are_available_as_methods
1413
+ --------------------------------------------------------------
1414
+ ---------------------------------------------------------
1415
+ CouchRestAdapterTest: test_can_not_instantiate_base_class
1416
+ ---------------------------------------------------------
1417
+ ---------------------------------------------------------
1418
+ CouchRestAdapterTest: test_find_will_work_with_partial_id
1419
+ ---------------------------------------------------------
1420
+ -------------------------------------------------------------
1421
+ CouchRestAdapterTest: test_one_can_update_existing_attributes
1422
+ -------------------------------------------------------------
1423
+ --------------------------------------------------------------------------------------------------
1424
+ CouchRestAdapterTest: test_query_all_by_type_will_return_array_of_docs_with_type_set_to_model_name
1425
+ --------------------------------------------------------------------------------------------------
1426
+ ----------------------------------------------------------------------
1427
+ CouchRestAdapterTest: test_query_all_will_bring_array_of_Foo_instances
1428
+ ----------------------------------------------------------------------
1429
+ -------------------------------------------------------
1430
+ CouchRestAdapterTest: test_update_to_attr=_will_persist
1431
+ -------------------------------------------------------
1432
+ --------------------------------------------------------------
1433
+ CouchRestAdapterTest: test_will_add_class_underscorename_to_id
1434
+ --------------------------------------------------------------
1435
+ --------------------------
1436
+ LintTest: test_errors_aref
1437
+ --------------------------
1438
+ ---------------------------
1439
+ LintTest: test_model_naming
1440
+ ---------------------------
1441
+ -------------------------
1442
+ LintTest: test_persisted?
1443
+ -------------------------
1444
+ ---------------------
1445
+ LintTest: test_to_key
1446
+ ---------------------
1447
+ -----------------------
1448
+ LintTest: test_to_param
1449
+ -----------------------
1450
+ ------------------------------
1451
+ LintTest: test_to_partial_path
1452
+ ------------------------------
1453
+ --------------------------------------------------------------
1454
+ CouchRestAdapterTest: test_attributes_are_available_as_methods
1455
+ --------------------------------------------------------------
1456
+ ---------------------------------------------------------
1457
+ CouchRestAdapterTest: test_can_not_instantiate_base_class
1458
+ ---------------------------------------------------------
1459
+ ---------------------------------------------------------
1460
+ CouchRestAdapterTest: test_find_will_work_with_partial_id
1461
+ ---------------------------------------------------------
1462
+ -------------------------------------------------------------
1463
+ CouchRestAdapterTest: test_one_can_update_existing_attributes
1464
+ -------------------------------------------------------------
1465
+ --------------------------------------------------------------------------------------------------
1466
+ CouchRestAdapterTest: test_query_all_by_type_will_return_array_of_docs_with_type_set_to_model_name
1467
+ --------------------------------------------------------------------------------------------------
1468
+ ----------------------------------------------------------------------
1469
+ CouchRestAdapterTest: test_query_all_will_bring_array_of_Foo_instances
1470
+ ----------------------------------------------------------------------
1471
+ -------------------------------------------------------
1472
+ CouchRestAdapterTest: test_update_to_attr=_will_persist
1473
+ -------------------------------------------------------
1474
+ --------------------------------------------------------------
1475
+ CouchRestAdapterTest: test_will_add_class_underscorename_to_id
1476
+ --------------------------------------------------------------
1477
+ --------------------------
1478
+ LintTest: test_errors_aref
1479
+ --------------------------
1480
+ ---------------------------
1481
+ LintTest: test_model_naming
1482
+ ---------------------------
1483
+ -------------------------
1484
+ LintTest: test_persisted?
1485
+ -------------------------
1486
+ ---------------------
1487
+ LintTest: test_to_key
1488
+ ---------------------
1489
+ -----------------------
1490
+ LintTest: test_to_param
1491
+ -----------------------
1492
+ ------------------------------
1493
+ LintTest: test_to_partial_path
1494
+ ------------------------------
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: couch_rest_adapter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Javier Guerra
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-27 00:00:00.000000000 Z
11
+ date: 2013-07-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails