dolly 0.7.5 → 0.7.6

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: 56886a385508910eeaa3205270cca2b4d1576930
4
- data.tar.gz: 2f0e1bb676c10ac81943456ddf81a4355d487698
3
+ metadata.gz: f1180bb4a834ab23599e09b0c3cbf2b5d748fe6e
4
+ data.tar.gz: 374645939a64f8bbb6720963052db1c1e4aa7a21
5
5
  SHA512:
6
- metadata.gz: 87161563ce0d86a810f216b73834071f1836d0ae3394066318ee80f01517353fb3390c8a0f31beeb047ed64c15dde9551ecd327d3179628b91d2e72629328f62
7
- data.tar.gz: 7bde8a8ec5b1bf9eb59ddb25ac12c90151dbfdfba287f7f70bab1e43697b68dd3b6426c9ddeeac02ec8db0ae44fb3737085fbc596c04d6e45c25b127ec03c9dd
6
+ metadata.gz: c4eeabef4f2cb353ae1f0c36f344662894122a5fb22aa45f2590805154a473e1b03b322abc04cb7c702ea8c1d9437b260a263fb6e8cf22729863f7aadc28c510
7
+ data.tar.gz: 7358d9fe47ef56297fbfeb07bd904fb1ed76941422876a6bd93a35914c22ab44deec9c8517a0fdb9f3019172112607eca88e4fbdabc4322bbf41cb667b6ce891
@@ -1,10 +1,12 @@
1
1
  require "dolly/query"
2
2
  require "dolly/property"
3
+ require 'dolly/timestamps'
3
4
 
4
5
  module Dolly
5
6
  class Document
6
7
  extend Dolly::Connection
7
8
  include Dolly::Query
9
+ extend Dolly::Timestamps
8
10
 
9
11
  attr_accessor :rows, :doc, :key
10
12
  class_attribute :properties
@@ -14,6 +16,18 @@ module Dolly
14
16
  init_properties options
15
17
  end
16
18
 
19
+ def update_properties options = {}
20
+ raise InvalidProperty unless valid_properties?(options)
21
+ options.each do |property, value|
22
+ send(:"#{property}=", value)
23
+ end
24
+ end
25
+
26
+ def update_properties! options = {}
27
+ update_properties(options)
28
+ save
29
+ end
30
+
17
31
  def id
18
32
  doc['_id'] ||= self.class.next_id
19
33
  end
@@ -34,6 +48,8 @@ module Dolly
34
48
  def save
35
49
  self.doc['_id'] = self.id if self.id.present?
36
50
  self.doc['_id'] = self.class.next_id if self.doc['_id'].blank?
51
+ set_created_at if respond_to? :set_created_at
52
+ set_updated_at if respond_to? :set_updated_at
37
53
  response = database.put(id_as_resource, self.doc.to_json)
38
54
  obj = JSON::parse response.parsed_response
39
55
  doc['_rev'] = obj['rev'] if obj['rev']
@@ -132,5 +148,12 @@ module Dolly
132
148
  self.doc['_id'] = self.class.namespace( normalized_id ) if normalized_id
133
149
  end
134
150
 
151
+ def valid_properties?(options)
152
+ options.keys.any?{ |option| properties_include?(option.to_s) }
153
+ end
154
+
155
+ def properties_include? property
156
+ _properties.map(&:name).include? property
157
+ end
135
158
  end
136
159
  end
@@ -2,6 +2,7 @@ module Dolly
2
2
  class Property
3
3
  attr_writer :value
4
4
  attr_accessor :name
5
+ attr_reader :class_name
5
6
 
6
7
  def initialize opts = {}
7
8
  @class_name = opts.delete(:class_name) if opts.present?
@@ -65,14 +65,6 @@ module Dolly
65
65
  JSON.parse database.get "_design/#{doc}/_view/#{view}", opts
66
66
  end
67
67
 
68
- def timestamps!
69
- %i/created_at updated_at/.each do |method|
70
- define_method(method){ @doc[method.to_s] ||= DateTime.now }
71
- define_method(:"[]"){|m| self.send(m.to_sym) }
72
- define_method(:"[]="){|m, v| self.send(:"#{m}=", v) }
73
- define_method(:"#{method}="){|val| @doc[method.to_s] = val }
74
- end
75
- end
76
68
  end
77
69
 
78
70
  def self.included(base)
@@ -0,0 +1,19 @@
1
+ module Dolly
2
+ module Timestamps
3
+
4
+ def timestamps!
5
+ Dolly::Document.class_eval do
6
+
7
+ property :created_at, :updated_at, class_name: DateTime
8
+
9
+ def set_created_at
10
+ self.created_at ||= DateTime.now
11
+ end
12
+
13
+ def set_updated_at
14
+ self.updated_at = DateTime.now
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -1,3 +1,3 @@
1
1
  module Dolly
2
- VERSION = "0.7.5"
2
+ VERSION = "0.7.6"
3
3
  end
@@ -24,4 +24,9 @@ module Dolly
24
24
  "Invalid config file at #{filename}"
25
25
  end
26
26
  end
27
+ class InvalidProperty < RuntimeError
28
+ def to_s
29
+ "Trying to set an undefined property."
30
+ end
31
+ end
27
32
  end
@@ -44,21 +44,6 @@ class DocumentTest < ActiveSupport::TestCase
44
44
  FakeWeb.register_uri :get, "#{query_base_path}?keys=%5B%22foo_bar%2F2%22%5D&include_docs=true", body: not_found_resp.to_json
45
45
  end
46
46
 
47
- test 'with timestamps!' do
48
- later = DateTime.new 1963, 1, 1
49
- now = DateTime.now
50
- DateTime.stubs(:now).returns(now)
51
- foo = FooBar.find "1"
52
- assert_equal now, foo.created_at
53
- assert_equal now, foo['created_at']
54
- assert_equal now, foo.updated_at
55
- assert foo.updated_at = later
56
- assert_equal later, foo.updated_at
57
- assert foo['created_at'] = later
58
- assert_equal later, foo['created_at']
59
- assert_equal foo['created_at'], foo.created_at
60
- end
61
-
62
47
  test 'new in memory document' do
63
48
  #TODO: clean up all the fake request creation
64
49
  resp = {ok: true, id: "foo_bar/1", rev: "FF0000"}
@@ -270,6 +255,41 @@ class DocumentTest < ActiveSupport::TestCase
270
255
  assert foo.id.match(uuid)
271
256
  end
272
257
 
258
+ test 'update document properties' do
259
+ foo = FooBar.new 'id' => 'a', foo: 'ab', bar: 'ba'
260
+ assert_equal 'ab', foo.foo
261
+ foo.update_properties foo: 'c'
262
+ assert_equal 'c', foo.foo
263
+ end
264
+
265
+ test 'update document propertys with bang' do
266
+ foo = FooBar.new 'id' => 'a', foo: 'ab', bar: 'ba'
267
+ foo.expects(:save).once
268
+ foo.update_properties! foo: 'c'
269
+ end
270
+
271
+ test 'trying to update invalid property' do
272
+ foo = FooBar.new 'id' => 'a', foo: 'ab', bar: 'ba'
273
+ assert_raise Dolly::InvalidProperty do
274
+ foo.update_properties key_to_success: false
275
+ end
276
+ end
277
+
278
+ test 'set updated at' do
279
+ foo = FooBar.new 'id' => 'a', foo: 'ab'
280
+ foo.update_properties! foo: 'c'
281
+ assert_equal DateTime.now.to_s, foo.updated_at.to_s
282
+ end
283
+
284
+ test 'created at is set' do
285
+ resp = {ok: true, id: "foo_bar/1", rev: "FF0000"}
286
+ FakeWeb.register_uri :put, /http:\/\/localhost:5984\/test\/foo_bar%2F.+/, body: resp.to_json
287
+ properties = {foo: 1, bar: 2, boolean: false}
288
+ foo = FooBar.new properties
289
+ foo.save
290
+ assert_equal DateTime.now.to_s, foo.created_at.to_s
291
+ end
292
+
273
293
  test 'reader :bar is not calling the writer :bar=' do
274
294
  foo = FooBar.new
275
295
  foo.bar = 'bar'
@@ -67957,3 +67957,249 @@ DocumentTest: test_with_default_will_return_default_value_on_nil
67957
67957
  -----------------------------------
67958
67958
  DocumentTest: test_with_timestamps!
67959
67959
  -----------------------------------
67960
+ --------------------------------------------------
67961
+ BulkDocumentTest: test_adding_document_to_bulk_doc
67962
+ --------------------------------------------------
67963
+ -----------------------------------------------------------------
67964
+ BulkDocumentTest: test_bulk_document_intialize_with_empty_payload
67965
+ -----------------------------------------------------------------
67966
+ ------------------------------------------------------------------
67967
+ BulkDocumentTest: test_save_document_will_remove_docs_from_payload
67968
+ ------------------------------------------------------------------
67969
+ --------------------------------------------------------------
67970
+ DocumentTest: test_Dolly::Document_have_bulk_document_instance
67971
+ --------------------------------------------------------------
67972
+ -------------------------------------------
67973
+ DocumentTest: test_all_first_returns_FooBar
67974
+ -------------------------------------------
67975
+ ------------------------------------------
67976
+ DocumentTest: test_all_last_returns_FooBar
67977
+ ------------------------------------------
67978
+ --------------------------------------
67979
+ DocumentTest: test_all_will_get_2_docs
67980
+ --------------------------------------
67981
+ ------------------------------------------------
67982
+ DocumentTest: test_all_will_get_FooBar_documents
67983
+ ------------------------------------------------
67984
+ ------------------------------------------
67985
+ DocumentTest: test_can_find_with_fixnum_id
67986
+ ------------------------------------------
67987
+ ----------------------------------------------
67988
+ DocumentTest: test_default_will_be_avoerwriten
67989
+ ----------------------------------------------
67990
+ --------------------------------------------
67991
+ DocumentTest: test_delete_method_on_document
67992
+ --------------------------------------------
67993
+ ------------------------------------------------
67994
+ DocumentTest: test_empty_find_should_raise_error
67995
+ ------------------------------------------------
67996
+ ------------------------------------------------------------
67997
+ DocumentTest: test_error_on_server_raises_Dolly::ServerError
67998
+ ------------------------------------------------------------
67999
+ --------------------------------------------------
68000
+ DocumentTest: test_find_will_get_a_FooBar_document
68001
+ --------------------------------------------------
68002
+ ----------------------------------------------------------------
68003
+ DocumentTest: test_find_with_multiple_ids_will_return_Collection
68004
+ ----------------------------------------------------------------
68005
+ --------------------------------------------------------
68006
+ DocumentTest: test_first_class_method_returns_collection
68007
+ --------------------------------------------------------
68008
+ ---------------------------------------------------------
68009
+ DocumentTest: test_first_class_method_returns_single_item
68010
+ ---------------------------------------------------------
68011
+ ---------------------------------------------
68012
+ DocumentTest: test_getting_not_found_document
68013
+ ---------------------------------------------
68014
+ -------------------------------------------------------
68015
+ DocumentTest: test_last_class_method_returns_collection
68016
+ -------------------------------------------------------
68017
+ --------------------------------------------------------
68018
+ DocumentTest: test_last_class_method_returns_single_item
68019
+ --------------------------------------------------------
68020
+ -------------------------------------------------
68021
+ DocumentTest: test_multi_response_with_right_data
68022
+ -------------------------------------------------
68023
+ ---------------------------------------
68024
+ DocumentTest: test_new_document_have_id
68025
+ ---------------------------------------
68026
+ -------------------------------------------------------------------
68027
+ DocumentTest: test_new_document_will_have_id_from__id_or_id_strings
68028
+ -------------------------------------------------------------------
68029
+ -------------------------------------------------------------------
68030
+ DocumentTest: test_new_document_will_have_id_from__id_or_id_symbols
68031
+ -------------------------------------------------------------------
68032
+ ------------------------------------------
68033
+ DocumentTest: test_new_document_with_no_id
68034
+ ------------------------------------------
68035
+ -----------------------------------------
68036
+ DocumentTest: test_new_in_memory_document
68037
+ -----------------------------------------
68038
+ ------------------------------------
68039
+ DocumentTest: test_query_custom_view
68040
+ ------------------------------------
68041
+ ----------------------------------------------
68042
+ DocumentTest: test_query_custom_view_collation
68043
+ ----------------------------------------------
68044
+ --------------------------------------------------------------
68045
+ DocumentTest: test_reader_:bar_is_not_calling_the_writer_:bar=
68046
+ --------------------------------------------------------------
68047
+ ------------------------------------------
68048
+ DocumentTest: test_soft_delete_on_document
68049
+ ------------------------------------------
68050
+ ----------------------------------------------------
68051
+ DocumentTest: test_trying_to_update_invalid_property
68052
+ ----------------------------------------------------
68053
+ ---------------------------------------------
68054
+ DocumentTest: test_update_document_properties
68055
+ ---------------------------------------------
68056
+ ------------------------------------------------------
68057
+ DocumentTest: test_update_document_propertys_with_bang
68058
+ ------------------------------------------------------
68059
+ -------------------------------------------
68060
+ DocumentTest: test_will_have_key_properties
68061
+ -------------------------------------------
68062
+ --------------------------------------------------------
68063
+ DocumentTest: test_will_have_object_with_DateTime_method
68064
+ --------------------------------------------------------
68065
+ ----------------------------------------------------
68066
+ DocumentTest: test_will_have_object_with_Date_method
68067
+ ----------------------------------------------------
68068
+ ----------------------------------------------------
68069
+ DocumentTest: test_will_have_object_with_Time_method
68070
+ ----------------------------------------------------
68071
+ --------------------------------------------------------
68072
+ DocumentTest: test_will_have_object_with_boolean?_method
68073
+ --------------------------------------------------------
68074
+ ------------------------------------------------
68075
+ DocumentTest: test_will_have_only_set_properties
68076
+ ------------------------------------------------
68077
+ ----------------------------------------------------------------
68078
+ DocumentTest: test_with_default_will_return_default_value_on_nil
68079
+ ----------------------------------------------------------------
68080
+ -----------------------------------
68081
+ DocumentTest: test_with_timestamps!
68082
+ -----------------------------------
68083
+ --------------------------------------------------
68084
+ BulkDocumentTest: test_adding_document_to_bulk_doc
68085
+ --------------------------------------------------
68086
+ -----------------------------------------------------------------
68087
+ BulkDocumentTest: test_bulk_document_intialize_with_empty_payload
68088
+ -----------------------------------------------------------------
68089
+ ------------------------------------------------------------------
68090
+ BulkDocumentTest: test_save_document_will_remove_docs_from_payload
68091
+ ------------------------------------------------------------------
68092
+ --------------------------------------------------------------
68093
+ DocumentTest: test_Dolly::Document_have_bulk_document_instance
68094
+ --------------------------------------------------------------
68095
+ -------------------------------------------
68096
+ DocumentTest: test_all_first_returns_FooBar
68097
+ -------------------------------------------
68098
+ ------------------------------------------
68099
+ DocumentTest: test_all_last_returns_FooBar
68100
+ ------------------------------------------
68101
+ --------------------------------------
68102
+ DocumentTest: test_all_will_get_2_docs
68103
+ --------------------------------------
68104
+ ------------------------------------------------
68105
+ DocumentTest: test_all_will_get_FooBar_documents
68106
+ ------------------------------------------------
68107
+ ------------------------------------------
68108
+ DocumentTest: test_can_find_with_fixnum_id
68109
+ ------------------------------------------
68110
+ ----------------------------------------------
68111
+ DocumentTest: test_default_will_be_avoerwriten
68112
+ ----------------------------------------------
68113
+ --------------------------------------------
68114
+ DocumentTest: test_delete_method_on_document
68115
+ --------------------------------------------
68116
+ ------------------------------------------------
68117
+ DocumentTest: test_empty_find_should_raise_error
68118
+ ------------------------------------------------
68119
+ ------------------------------------------------------------
68120
+ DocumentTest: test_error_on_server_raises_Dolly::ServerError
68121
+ ------------------------------------------------------------
68122
+ --------------------------------------------------
68123
+ DocumentTest: test_find_will_get_a_FooBar_document
68124
+ --------------------------------------------------
68125
+ ----------------------------------------------------------------
68126
+ DocumentTest: test_find_with_multiple_ids_will_return_Collection
68127
+ ----------------------------------------------------------------
68128
+ --------------------------------------------------------
68129
+ DocumentTest: test_first_class_method_returns_collection
68130
+ --------------------------------------------------------
68131
+ ---------------------------------------------------------
68132
+ DocumentTest: test_first_class_method_returns_single_item
68133
+ ---------------------------------------------------------
68134
+ ---------------------------------------------
68135
+ DocumentTest: test_getting_not_found_document
68136
+ ---------------------------------------------
68137
+ -------------------------------------------------------
68138
+ DocumentTest: test_last_class_method_returns_collection
68139
+ -------------------------------------------------------
68140
+ --------------------------------------------------------
68141
+ DocumentTest: test_last_class_method_returns_single_item
68142
+ --------------------------------------------------------
68143
+ -------------------------------------------------
68144
+ DocumentTest: test_multi_response_with_right_data
68145
+ -------------------------------------------------
68146
+ ---------------------------------------
68147
+ DocumentTest: test_new_document_have_id
68148
+ ---------------------------------------
68149
+ -------------------------------------------------------------------
68150
+ DocumentTest: test_new_document_will_have_id_from__id_or_id_strings
68151
+ -------------------------------------------------------------------
68152
+ -------------------------------------------------------------------
68153
+ DocumentTest: test_new_document_will_have_id_from__id_or_id_symbols
68154
+ -------------------------------------------------------------------
68155
+ ------------------------------------------
68156
+ DocumentTest: test_new_document_with_no_id
68157
+ ------------------------------------------
68158
+ -----------------------------------------
68159
+ DocumentTest: test_new_in_memory_document
68160
+ -----------------------------------------
68161
+ ------------------------------------
68162
+ DocumentTest: test_query_custom_view
68163
+ ------------------------------------
68164
+ ----------------------------------------------
68165
+ DocumentTest: test_query_custom_view_collation
68166
+ ----------------------------------------------
68167
+ --------------------------------------------------------------
68168
+ DocumentTest: test_reader_:bar_is_not_calling_the_writer_:bar=
68169
+ --------------------------------------------------------------
68170
+ ------------------------------------------
68171
+ DocumentTest: test_soft_delete_on_document
68172
+ ------------------------------------------
68173
+ ----------------------------------------------------
68174
+ DocumentTest: test_trying_to_update_invalid_property
68175
+ ----------------------------------------------------
68176
+ ---------------------------------------------
68177
+ DocumentTest: test_update_document_properties
68178
+ ---------------------------------------------
68179
+ ------------------------------------------------------
68180
+ DocumentTest: test_update_document_propertys_with_bang
68181
+ ------------------------------------------------------
68182
+ -------------------------------------------
68183
+ DocumentTest: test_will_have_key_properties
68184
+ -------------------------------------------
68185
+ --------------------------------------------------------
68186
+ DocumentTest: test_will_have_object_with_DateTime_method
68187
+ --------------------------------------------------------
68188
+ ----------------------------------------------------
68189
+ DocumentTest: test_will_have_object_with_Date_method
68190
+ ----------------------------------------------------
68191
+ ----------------------------------------------------
68192
+ DocumentTest: test_will_have_object_with_Time_method
68193
+ ----------------------------------------------------
68194
+ --------------------------------------------------------
68195
+ DocumentTest: test_will_have_object_with_boolean?_method
68196
+ --------------------------------------------------------
68197
+ ------------------------------------------------
68198
+ DocumentTest: test_will_have_only_set_properties
68199
+ ------------------------------------------------
68200
+ ----------------------------------------------------------------
68201
+ DocumentTest: test_with_default_will_return_default_value_on_nil
68202
+ ----------------------------------------------------------------
68203
+ -----------------------------------
68204
+ DocumentTest: test_with_timestamps!
68205
+ -----------------------------------
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dolly
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.5
4
+ version: 0.7.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - javierg
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-23 00:00:00.000000000 Z
11
+ date: 2015-01-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
@@ -141,6 +141,7 @@ files:
141
141
  - lib/dolly/query.rb
142
142
  - lib/dolly/railtie.rb
143
143
  - lib/dolly/request.rb
144
+ - lib/dolly/timestamps.rb
144
145
  - lib/dolly/version.rb
145
146
  - lib/dolly.rb
146
147
  - lib/exceptions/dolly.rb