active_nomad 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
+ == 0.2.1 2010-10-06
2
+
3
+ * from_json noops on invalid JSON strings.
4
+
1
5
  == 0.2.0 2010-10-05
2
6
 
3
7
  * No longer overriding ActiveRecord::Base#to_json.
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
@@ -1,5 +1,5 @@
1
1
  module ActiveNomad
2
- VERSION = [0, 2, 0]
2
+ VERSION = [0, 2, 1]
3
3
 
4
4
  class << VERSION
5
5
  include Comparable
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
- serialized_attributes = JSON.parse(string)
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: 23
4
+ hash: 21
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 0
10
- version: 0.2.0
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-05 00:00:00 -04:00
18
+ date: 2010-10-06 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency