undo 0.1.0 → 0.1.1

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: 20c52711aa6a7b2718cf79b9ecc238f1a77b53e2
4
- data.tar.gz: fd06dc16acb71cd5fde5d9bfe4b69c8f00272a1c
3
+ metadata.gz: 4898ed46bb0f2c3a761f82478c73b9d78c6e1a7d
4
+ data.tar.gz: 6a3b917da4a4ebd61757e227d1e54e090dd23c05
5
5
  SHA512:
6
- metadata.gz: 6a94e5461a9937c5141ca1bdd5cdbf3fb9d653de480ecf1a9c508580f0f1774060cb6902dc55bb4e74d4729ed3b5bc0e1b4b8873cca76cbe24ba617daee8cdf9
7
- data.tar.gz: 11a5d31f76ddced0163edfb1d382ed03a5d7c84dfd7378a0c805f562b34eb0ed5d05659449b56f59cfdb6e119d1115bc4a69d6f233e4f57eaa766ba8a3f5f172
6
+ metadata.gz: 5675e00c800a9e4e6a590e112fe331b211e6f57f97d1b851a335343c721e89c06c4d0325617a8696da4a8fb8e25f1cf7b38bee0c2d3e85d9de42428abfa23e86
7
+ data.tar.gz: 955338eac937b4825f4604c1601d43b0032d6bdf70b685ea1469d3ed3aef8329be1bf085284c3fd765ba03eb5338d8b967e08c33afa63d5b42755cd87f082095
data/README.md CHANGED
@@ -71,36 +71,21 @@ state on each hit to `mutator methods`. By default mutator_methods are
71
71
  place or using global configuration (see below).
72
72
 
73
73
  To use something more advanced rather plain memory storage and
74
- path though serializer, configure the Undo:
74
+ pass through serializer, configure the Undo:
75
75
 
76
76
  ``` ruby
77
77
  Undo.configure do |config|
78
- config.storage = Undo::Storage::RailsCache
79
- config.serializer = Undo::Serializer::ActiveModel
78
+ config.storage = Undo::Storage::RailsCache.new
79
+ config.serializer = Undo::Serializer::ActiveModel.new
80
80
  end
81
81
  ```
82
82
  gem `undo-storage-rails_cache` and gem `undo-serializer-active_model` are required for this.
83
83
  There are a bunch of other Rails free adapters. Read about them in configuration chapter below.
84
84
 
85
- To append custom mutator_methods use
86
-
87
- ``` ruby
88
- Undo.configure do |config|
89
- config.mutator_methods += [:put, :push, :pop]
90
- end
91
- ```
92
-
93
- To define custom uuid generator use `uuid_generator` option:
94
-
95
- ``` ruby
96
- Undo.configure do |config|
97
- config.uuid_generator = ->(object) { "#{object.class.name}_#{object.id}" }
98
- end
99
- ```
100
-
101
85
  ### UUID
102
86
 
103
- By default uuids are generated by `SecureRandom.uuid`.
87
+ By default uuids are generated by `SecureRandom.uuid`. The generator is managed by `uuid_generator`
88
+ setting (see below in configuration chapter).
104
89
 
105
90
  UUID can be provided to both #wrap and #store methods and it will be used instead of generated one:
106
91
 
@@ -141,7 +126,7 @@ end
141
126
 
142
127
  Currently available serializers:
143
128
  * `Undo::Serializer::Null` pass though serializer. It do nothing and returns whatever passed to it.
144
- * `gem undo-serializer-active_model`
129
+ * `gem undo-serializer-active_model` depends on #attributes method so can be used with Virtus and PORO objects as well as with ActiveRecord objects.
145
130
 
146
131
  Check [documentation](http://github.com/AlexParamonov/undo) on project
147
132
  repository for currently available serializers.
@@ -159,11 +144,11 @@ end
159
144
 
160
145
  `mutator methods` option defines a list of methods that will trigger storage#put
161
146
  By default mutator_methods are `update`, `delete`, `destroy`.
162
- To set custom mutator_methods use
147
+ To append custom mutator_methods use
163
148
 
164
149
  ``` ruby
165
150
  Undo.configure do |config|
166
- config.mutator_methods = [:put, :push, :pop]
151
+ config.mutator_methods += [:put, :push, :pop]
167
152
  end
168
153
  ```
169
154
 
@@ -191,6 +176,13 @@ post.destroy
191
176
  Undo.restore uuid, serializer: PostDeserializer.new(options)
192
177
  ```
193
178
 
179
+ Additionally when given serializer accepts additional options to
180
+ `#serialize` or `#deserialize`, those options can be set in place as well:
181
+
182
+ ``` ruby
183
+ Undo.store post, include: :comments
184
+ ```
185
+
194
186
 
195
187
  Contacts
196
188
  -------------
data/lib/undo.rb CHANGED
@@ -10,24 +10,24 @@ module Undo
10
10
  def self.store(object, options = {})
11
11
  config.with(options) do |config|
12
12
  uuid(object, options).tap do |uuid|
13
- config.storage.put uuid, config.serializer.serialize(object)
13
+ config.storage.put uuid,
14
+ config.serializer.serialize(object, config.filter(options))
14
15
  end
15
16
  end
16
17
  end
17
18
 
18
19
  def self.restore(uuid, options = {})
19
20
  config.with(options) do |config|
20
- config.serializer.deserialize config.storage.fetch(uuid)
21
+ config.serializer.deserialize config.storage.fetch(uuid),
22
+ config.filter(options)
23
+
21
24
  end
22
25
  end
23
26
 
24
27
  def self.wrap(object, options = {})
28
+ options[:uuid] ||= uuid object, options
25
29
  config.with(options) do |config|
26
- Wrapper.new(
27
- object,
28
- uuid(object, options),
29
- mutator_methods: config.mutator_methods
30
- )
30
+ Wrapper.new object, options.merge(mutator_methods: config.mutator_methods)
31
31
  end
32
32
  end
33
33
 
data/lib/undo/config.rb CHANGED
@@ -25,5 +25,11 @@ module Undo
25
25
 
26
26
  block_given? ? block.call(config) : config
27
27
  end
28
+
29
+ def filter(options)
30
+ options.delete_if do |key, _|
31
+ attributes.keys.include? key
32
+ end
33
+ end
28
34
  end
29
35
  end
@@ -1,11 +1,11 @@
1
1
  module Undo
2
2
  module Serializer
3
3
  class Null
4
- def serialize(object)
4
+ def serialize(object, options = {})
5
5
  object
6
6
  end
7
7
 
8
- def deserialize(object)
8
+ def deserialize(object, options = {})
9
9
  object
10
10
  end
11
11
  end
data/lib/undo/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Undo
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
data/lib/undo/wrapper.rb CHANGED
@@ -6,10 +6,11 @@ module Undo
6
6
  def_delegators :object, :class, :kind_of?
7
7
  attr_reader :uuid
8
8
 
9
- def initialize(object, uuid, options = {})
9
+ def initialize(object, options = {})
10
10
  @object = object
11
- @mutator_methods = options.fetch :mutator_methods, []
12
- @uuid = object.respond_to?(:uuid) ? object.uuid : uuid
11
+ @mutator_methods = options.delete(:mutator_methods) || []
12
+ @uuid = object.respond_to?(:uuid) ? object.uuid : options.fetch(:uuid)
13
+ @options = options
13
14
 
14
15
  super object
15
16
  end
@@ -20,10 +21,14 @@ module Undo
20
21
  end
21
22
 
22
23
  private
23
- attr_reader :object, :mutator_methods
24
+ attr_reader :object, :options
25
+
26
+ def mutator_methods
27
+ Kernel.Array(@mutator_methods)
28
+ end
24
29
 
25
30
  def store
26
- Undo.store object, uuid: uuid
31
+ Undo.store object, options.merge(uuid: uuid)
27
32
  end
28
33
  end
29
34
  end
@@ -2,14 +2,12 @@ require "spec_helper_lite"
2
2
 
3
3
  describe Undo::Wrapper do
4
4
  subject { described_class }
5
- let(:model) { subject.new object, uuid }
5
+ let(:model) { subject.new object, uuid: uuid, mutator_methods: mutator_methods }
6
+ let(:mutator_methods) { [:change] }
6
7
  let(:object) { double :object, change: true }
7
8
  let(:uuid) { double :uuid }
8
9
 
9
10
  describe "storage" do
10
- let(:model) { subject.new object, uuid, mutator_methods: mutator_methods }
11
- let(:mutator_methods) { [:change] }
12
-
13
11
  it "stores object when mutator method is called" do
14
12
  expect(model).to receive(:store)
15
13
  model.change
@@ -25,6 +23,8 @@ describe Undo::Wrapper do
25
23
  it "uses object#uuid instead" do
26
24
  expect(object).to receive(:uuid) { "123" }
27
25
  expect(model.uuid).to eq "123"
26
+ expect(Undo).to receive(:store).with(object, hash_including(uuid: "123"))
27
+ model.change
28
28
  end
29
29
  end
30
30
  end
data/spec/undo_spec.rb CHANGED
@@ -21,7 +21,7 @@ describe Undo do
21
21
  let(:serializer) { double :serializer }
22
22
 
23
23
  it "serializes data before storing" do
24
- expect(serializer).to receive(:serialize).with(object).ordered
24
+ expect(serializer).to receive(:serialize).with(object, anything).ordered
25
25
  expect(storage).to receive(:put).ordered
26
26
 
27
27
  subject.store object,
@@ -33,12 +33,41 @@ describe Undo do
33
33
  uuid = subject.store object
34
34
 
35
35
  expect(storage).to receive(:fetch).and_return(foo: :bar).ordered
36
- expect(serializer).to receive(:deserialize).with(foo: :bar).ordered
36
+ expect(serializer).to receive(:deserialize).with({ foo: :bar }, anything).ordered
37
37
 
38
38
  subject.restore uuid,
39
39
  storage: storage,
40
40
  serializer: serializer
41
41
  end
42
+
43
+ it "pass through options from store to serialize" do
44
+ expect(serializer).to receive(:serialize).with(object, foo: :bar)
45
+
46
+ subject.store object,
47
+ serializer: serializer,
48
+ foo: :bar
49
+ end
50
+
51
+ it "pass through options from wrap to serialize" do
52
+ expect(serializer).to receive(:serialize)
53
+
54
+ wrapper = subject.wrap object,
55
+ serializer: serializer,
56
+ mutator_methods: :change,
57
+ foo: :bar
58
+
59
+ wrapper.change
60
+ end
61
+
62
+
63
+ it "pass through options from restore to deserialize" do
64
+ uuid = subject.store object
65
+ expect(serializer).to receive(:deserialize).with(object, foo: :bar)
66
+
67
+ subject.restore uuid,
68
+ serializer: serializer,
69
+ foo: :bar
70
+ end
42
71
  end
43
72
 
44
73
  describe "#wrap" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: undo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Paramonov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-13 00:00:00.000000000 Z
11
+ date: 2014-03-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: virtus