active_nomad 0.2.0 → 0.2.1
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.
- data/CHANGELOG +4 -0
- data/README.markdown +11 -0
- data/lib/active_nomad/version.rb +1 -1
- data/lib/active_nomad.rb +24 -1
- data/spec/unit/active_nomad_spec.rb +10 -0
- metadata +4 -4
data/CHANGELOG
CHANGED
data/README.markdown
CHANGED
@@ -51,6 +51,17 @@ You can define your own formats easily using `to_serialized_attributes` and
|
|
51
51
|
`from_serialized_attributes`. The former returns an `ActiveSupport::OrderedHash`
|
52
52
|
of attribute names to serialized values (`String`s or `nil`s).
|
53
53
|
|
54
|
+
### Destruction
|
55
|
+
|
56
|
+
You may use #to_destroy to define a destruction strategy:
|
57
|
+
|
58
|
+
thing = Thing.from_json(cookies[:thing])
|
59
|
+
thing.to_destroy do |thing|
|
60
|
+
cookies.delete(:thing)
|
61
|
+
end
|
62
|
+
|
63
|
+
Or simply override #destroy in a subclass.
|
64
|
+
|
54
65
|
### Transactions
|
55
66
|
|
56
67
|
In addition to customizing persistence, you can also customize transaction
|
data/lib/active_nomad/version.rb
CHANGED
data/lib/active_nomad.rb
CHANGED
@@ -12,6 +12,13 @@ module ActiveNomad
|
|
12
12
|
@save_proc = proc
|
13
13
|
end
|
14
14
|
|
15
|
+
#
|
16
|
+
# Tell this record how to destroy itself.
|
17
|
+
#
|
18
|
+
def to_destroy(&proc)
|
19
|
+
@destroy_proc = proc
|
20
|
+
end
|
21
|
+
|
15
22
|
#
|
16
23
|
# Return an ActiveSupport::OrderedHash of serialized attributes.
|
17
24
|
#
|
@@ -82,7 +89,11 @@ module ActiveNomad
|
|
82
89
|
#
|
83
90
|
def self.from_json(string)
|
84
91
|
return new if string.blank?
|
85
|
-
|
92
|
+
begin
|
93
|
+
serialized_attributes = JSON.parse(string)
|
94
|
+
rescue JSON::ParserError
|
95
|
+
serialized_attributes = {}
|
96
|
+
end
|
86
97
|
from_serialized_attributes(serialized_attributes)
|
87
98
|
end
|
88
99
|
|
@@ -100,6 +111,18 @@ module ActiveNomad
|
|
100
111
|
@save_proc.call(self)
|
101
112
|
end
|
102
113
|
|
114
|
+
#
|
115
|
+
# Destroy the object.
|
116
|
+
#
|
117
|
+
# The default is to call the block registered with
|
118
|
+
# #to_destroy. Override if you don't want to use #to_destroy.
|
119
|
+
#
|
120
|
+
def destroy
|
121
|
+
@destroy_proc or
|
122
|
+
raise NoDestructionStrategy, "no destruction strategy - use #to_destroy to define one"
|
123
|
+
@destroy_proc.call(self)
|
124
|
+
end
|
125
|
+
|
103
126
|
private
|
104
127
|
|
105
128
|
class FakeAdapter < ActiveRecord::ConnectionAdapters::AbstractAdapter # :nodoc:
|
@@ -283,6 +283,16 @@ describe ActiveNomad::Base do
|
|
283
283
|
end
|
284
284
|
end
|
285
285
|
|
286
|
+
describe ".from_json" do
|
287
|
+
it "should not set any attributes if an invalid JSON string is given" do
|
288
|
+
klass = Class.new(ActiveNomad::Base) do
|
289
|
+
attribute :name, :string
|
290
|
+
end
|
291
|
+
instance = klass.from_json("invalid")
|
292
|
+
instance.name.should be_nil
|
293
|
+
end
|
294
|
+
end
|
295
|
+
|
286
296
|
def self.it_should_roundtrip_through(serializer, deserializer, &block)
|
287
297
|
describe "roundtripping through ##{serializer} and .#{deserializer}" do
|
288
298
|
class_eval(&block) if block
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_nomad
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 21
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 2
|
9
|
-
-
|
10
|
-
version: 0.2.
|
9
|
+
- 1
|
10
|
+
version: 0.2.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- George Ogata
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-10-
|
18
|
+
date: 2010-10-06 00:00:00 -04:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|