active_nomad 0.0.2 → 0.0.3

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.0.3 2010-09-29
2
+
3
+ * Don't open unnecessary database transactions on save.
4
+
1
5
  == 0.0.2 2010-09-28
2
6
 
3
7
  * Rename to Active Nomad.
data/README.markdown CHANGED
@@ -6,8 +6,8 @@ ActiveRecord objects with a customizable persistence strategy.
6
6
 
7
7
  Sometimes you want an Active Record object that does not live in the database.
8
8
  Perhaps it never needs to be persisted, or you'd like to store it in a cookie,
9
- or a file, but it would still be handy to have ActiveRecord's ability to cast
10
- values, run validations, or fire callbacks.
9
+ or some other store, but it would still be handy to have ActiveRecord's ability
10
+ to cast values, run validations, or fire callbacks.
11
11
 
12
12
  Ideally, the persistence strategy would be pluggable. With Active Nomad, it is!
13
13
 
@@ -41,6 +41,32 @@ Things to note:
41
41
  * You may alternatively override `persist` in a subclass if you
42
42
  don't want to register a proc for every instance.
43
43
 
44
+ ## Transactions
45
+
46
+ In addition to customizing persistence, you can also customize
47
+ transaction semantics by overriding the `transaction` class method in
48
+ a base class. Example:
49
+
50
+ class RedisNomad < ActiveNomad::Base
51
+ def self.transaction
52
+ redis.multi
53
+ begin
54
+ yield
55
+ redis.exec
56
+ rescue Exception => e
57
+ redis.discard
58
+ raise
59
+ end
60
+ end
61
+
62
+ def self.redis
63
+ @redis ||= Redis.new
64
+ end
65
+ end
66
+
67
+ `ActiveNomad::Base.transaction` simply calls the given block with no
68
+ real transaction semantics.
69
+
44
70
  ## Notes
45
71
 
46
72
  Only ActiveRecord 2.3 compatible. ActiveRecord 3.0 has a more modular
@@ -1,5 +1,5 @@
1
1
  module ActiveNomad
2
- VERSION = [0, 0, 2]
2
+ VERSION = [0, 0, 3]
3
3
 
4
4
  class << VERSION
5
5
  include Comparable
data/lib/active_nomad.rb CHANGED
@@ -27,6 +27,9 @@ module ActiveNomad
27
27
  end.compact.sort.join('&')
28
28
  end
29
29
 
30
+ #
31
+ # Recreate an object from a serialized string.
32
+ #
30
33
  def self.deserialize(string)
31
34
  params = string ? CGI.parse(string.strip) : {}
32
35
  instance = new
@@ -54,7 +57,7 @@ module ActiveNomad
54
57
 
55
58
  private
56
59
 
57
- class FakeAdapter < ActiveRecord::ConnectionAdapters::AbstractAdapter
60
+ class FakeAdapter < ActiveRecord::ConnectionAdapters::AbstractAdapter # :nodoc:
58
61
  def native_database_types
59
62
  @native_database_types ||= Hash.new{|h,k| h[k] = k.to_s}
60
63
  end
@@ -79,16 +82,25 @@ module ActiveNomad
79
82
  reset_column_information
80
83
  end
81
84
 
82
- def columns
85
+ def columns # :nodoc:
83
86
  @columns ||= []
84
87
  end
85
88
 
86
- # Reset everything, except the column information
87
- def reset_column_information
89
+ def reset_column_information # :nodoc:
90
+ # Reset everything, except the column information.
88
91
  columns = @columns
89
92
  super
90
93
  @columns = columns
91
94
  end
95
+
96
+ #
97
+ # Override to provide custom transaction semantics.
98
+ #
99
+ # The default #transaction simply yields to the given block.
100
+ #
101
+ def transaction
102
+ yield
103
+ end
92
104
  end
93
105
 
94
106
  self.abstract_class = true
data/spec/spec_helper.rb CHANGED
@@ -1,4 +1 @@
1
1
  require 'active_nomad'
2
-
3
- # TODO: This should not be necessary - we're not stubbing out enough.
4
- ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
@@ -235,4 +235,28 @@ describe ActiveNomad::Base do
235
235
  it_should_roundtrip :boolean, true
236
236
  it_should_roundtrip :boolean, false
237
237
  end
238
+
239
+ describe ".transaction" do
240
+ before do
241
+ @class = Class.new(ActiveNomad::Base) do
242
+ cattr_accessor :transaction_called
243
+ def self.transaction
244
+ self.transaction_called = true
245
+ end
246
+ end
247
+ end
248
+
249
+ it "should be overridable to provide custom transaction semantics" do
250
+ instance = @class.new
251
+ instance.transaction{}
252
+ instance.transaction_called.should be_true
253
+ end
254
+
255
+ it "should be called by #save" do
256
+ instance = @class.new
257
+ instance.transaction_called.should be_false
258
+ instance.save
259
+ instance.transaction_called.should be_true
260
+ end
261
+ end
238
262
  end
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: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
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-09-28 00:00:00 -04:00
18
+ date: 2010-09-29 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency