redis_cache_mailer_delivery 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/Guardfile CHANGED
@@ -6,6 +6,7 @@ guard :rspectacle, :cli => '--format documentation --backtrace' do
6
6
  watch('spec/spec_helper.rb') { %w(spec/spec_helper spec) }
7
7
 
8
8
  watch(%r{^spec/.+_spec\.rb$})
9
-
10
- watch(%r{^lib/(.+)\.rb$}) { |m| ["lib/#{m[1]}.rb", "spec/lib/#{m[1]}_spec.rb"] }
9
+ watch('lib/redis_cache_mailer_delivery.rb') { ['spec/redis_cache_mailer_delivery_spec.rb', 'lib/redis_cache_mailer_delivery.rb' ] }
10
+ watch(/lib\/redis_cache_mailer_delivery\/converters\/(.+).rb/) {|m| ["spec/redis_cache_mailer_delivery/converters/#{m[1]}_spec.rb", "lib/redis_cache_mailer_delivery/converters/#{m[1]}.rb" ] }
11
+ watch(/lib\/redis_cache_mailer_delivery\/(.+).rb/) {|m| ["spec/redis_cache_mailer_delivery/#{m[1]}_spec.rb", "lib/redis_cache_mailer_delivery/#{m[1]}.rb" ] }
11
12
  end
data/README.md CHANGED
@@ -31,15 +31,20 @@ In a Rails project, in your environment files such as config/environments/cucumb
31
31
 
32
32
  ```ruby
33
33
  config.action_mailer.delivery_method = :redis_cache
34
- config.action_mailer.redis_cache_settings = { :redis_key_name => "a_key_name_for_all_stored_emails" }
34
+ config.action_mailer.redis_cache_settings = { :redis_key_name => "a_key_name_for_all_stored_emails",
35
+ :marshallable_converters => [:sequel_record_marshallable, Marshallable::Attrubute1] }
35
36
  ```
36
37
 
37
- You don't have to define the redis_key_name, the default is
38
+ You don't have to define the settings, the default ```redis_key_name``` is
38
39
 
39
40
  ```ruby
40
41
  "redis_cache_mailer_delivery:mail_messages"
41
42
  ```
42
43
 
44
+ the default ```marshallable_converters``` is an empty array
45
+
46
+ The meaning of ```marshallable_converters``` will be discussed in the next section.
47
+
43
48
  All the mails being delivered will be written into the redis storage. You can use
44
49
 
45
50
  ```ruby
@@ -48,13 +53,31 @@ RedisCacheMailerDelivery::Deliveries.all
48
53
 
49
54
  to access all the mails
50
55
 
51
- ## Sequel
56
+ ## Marshallable Converters
57
+
58
+ If you use Sequel and you set a sequel record into the Mail::Message (```mail.record = record```), the chances are you will encounter an error where you cannot serialize the message since the record might be a singleton.
59
+
60
+ The remedy provided by this gem is to provide a mechanism to make the mail to be marshallable before it marshals it, as well as some built-in class for making the object to be marshallable.
61
+
62
+ ```marshallable_converters``` setting accepts an array. Each element could be a symbol, or a class.
63
+
64
+ When it is a class, the class must have a class method ```marshallable``` which accpets the mail object as the only param. The method will take the mail and convert it into a marshallable object.
52
65
 
53
- If you use Sequel and you set a sequel record into the Mail::Message (```mail.record = record```), the chances are you will encounter an error where you cannot serialize the message since the record might be a singleton. The gem has a builtin fix for this by using marshallable! method provied by Sequel since 3.6.0
66
+ When it is a symbol, it means it is a built-in converter. For example ```:sequel_record_marshallable``` will convert a Mail::Message containg a singlton Sequel record into marshallable one by use Sequel's ```marshallable!``` method.
54
67
 
55
- ## Other unmarshallable objects
68
+ Currently the built-in only converter is ```:sequel_record_marshallable```
56
69
 
57
- The code will find if the mail being passed in responds to ```marshallable``` method, if it does, the code will call ```marshallable``` method before send it to the redis store
70
+ The order of the ```:marshallable_converters``` could be important. The gem always goes through the converters in the order.
71
+
72
+ So for example, if you have:
73
+
74
+ ```ruby
75
+ config.action_mailer.redis_cache_settings = { :marshallable_converters => [Marshallable::Attrubute2, Marshallable::Attrubute1] }
76
+ ```
77
+
78
+ The gem calls: ```object1 = Marshallable::Attrubute2.marshallable(mail)```, then it calls ```object2 = Marshallable::Attrubute1.marshallable(object1)```
79
+
80
+ So if you must have ```attrubute1``` to be marshallable first, then make the ```attrubute2``` marshallable. You have to switch the order in this example.
58
81
 
59
82
  ## Contributing
60
83
 
@@ -1,14 +1,16 @@
1
1
  require 'action_mailer'
2
2
  require 'redis_cache_mailer_delivery/version'
3
- require 'redis_cache_mailer_delivery/mail/cache_delivery'
4
- require 'redis_cache_mailer_delivery/mail/sequel_marshallable_mail_message'
3
+ require 'redis_cache_mailer_delivery/marshallable_converter_setting'
4
+ require 'redis_cache_mailer_delivery/cache_delivery'
5
+ require 'redis_cache_mailer_delivery/converters/sequel_marshallable_mail_message'
5
6
  require 'redis_cache_mailer_delivery/deliveries'
6
7
 
7
8
  module RedisCacheMailerDelivery
8
9
  class << self
9
10
  def install
10
- ActionMailer::Base.add_delivery_method :redis_cache, Mail::CacheDelivery,
11
- :redis_key_name => "redis_cache_mailer_delivery:mail_messages"
11
+ ActionMailer::Base.add_delivery_method :redis_cache, RedisCacheMailerDelivery::CacheDelivery,
12
+ :redis_key_name => "redis_cache_mailer_delivery:mail_messages",
13
+ :marshallable_converters => []
12
14
  end
13
15
 
14
16
  end # << self
@@ -1,4 +1,4 @@
1
- module Mail
1
+ module RedisCacheMailerDelivery
2
2
  #
3
3
  # Performs deliveries to redis storage, so mails can accessed from
4
4
  # other processes.
@@ -17,9 +17,10 @@ module Mail
17
17
  # @api private
18
18
  def deliver!(mail)
19
19
  list = Redis::List.new settings[:redis_key_name], :marshal => true
20
- mail = SequelMarshallableMailMessage.marshallable mail
21
- object = mail.respond_to?(:marshallable) ? mail.marshallable : mail
22
- list << object
20
+ settings[:marshallable_converters].each do |setting|
21
+ mail = MarshallableConverterSetting.new(setting).marshallable_class.marshallable(mail)
22
+ end
23
+ list << mail
23
24
  end
24
25
 
25
26
  end # CacheDelivery
@@ -0,0 +1,13 @@
1
+ module RedisCacheMailerDelivery
2
+ module Converters
3
+ module SequelMarshallableMailMessage
4
+ class << self
5
+ def marshallable(object)
6
+ return object unless object.respond_to?(:record)
7
+ object.record.marshallable! if object.record.respond_to?(:marshallable!)
8
+ object
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,19 @@
1
+ module RedisCacheMailerDelivery
2
+ class MarshallableConverterSetting
3
+ attr_reader :converter_name
4
+ class << self
5
+ def builtin_settings
6
+ { :sequel_record_marshallable => RedisCacheMailerDelivery::Converters::SequelMarshallableMailMessage }
7
+ end
8
+ end
9
+
10
+ def initialize(converter_name)
11
+ @converter_name = converter_name
12
+ end
13
+
14
+ def marshallable_class
15
+ builtin_class = self.class.builtin_settings[converter_name]
16
+ builtin_class.nil? ? converter_name : builtin_class
17
+ end
18
+ end
19
+ end
@@ -1,3 +1,3 @@
1
1
  module RedisCacheMailerDelivery
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -0,0 +1,30 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
2
+
3
+ module RedisCacheMailerDelivery
4
+ describe CacheDelivery do
5
+ subject {described_class.new :redis_key_name => "a-name", :marshallable_converters => []}
6
+ describe "#deliver!" do
7
+ let(:object) {Mail::Message.new}
8
+
9
+ after(:each) do
10
+ Redis.current.del "a-name"
11
+ end
12
+ it "puts a object into the redis store" do
13
+ subject.deliver!(object)
14
+ Redis::List.new("a-name", :marshal => true).values[0].should eq(object)
15
+ end
16
+
17
+ context "with marshallable_converters" do
18
+ let(:class1) {double :class1, :marshallable => object1}
19
+ let(:class2) {double :class1, :marshallable => object2}
20
+ subject {described_class.new :redis_key_name => "a-name", :marshallable_converters => [class1, class2]}
21
+ let(:object1) {double :object1}
22
+ let(:object2) {Mail::Message.new}
23
+ it "calls the converters and marshal the last object" do
24
+ subject.deliver!(object)
25
+ Redis::List.new("a-name", :marshal => true).values[0].should eq(object2)
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Mail::SequelMarshallableMailMessage do
3
+ describe RedisCacheMailerDelivery::Converters::SequelMarshallableMailMessage do
4
4
  let(:object) {Class.new.new}
5
5
  describe "#marshallable" do
6
6
  context "when the message responds to the record message" do
@@ -0,0 +1,29 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
2
+
3
+ module RedisCacheMailerDelivery
4
+ describe MarshallableConverterSetting do
5
+ describe ".builtin_settings" do
6
+ it "returns the hash of builtin marshallable converters" do
7
+ described_class.builtin_settings.should eq({:sequel_record_marshallable => RedisCacheMailerDelivery::Converters::SequelMarshallableMailMessage})
8
+ end
9
+ end
10
+ describe "#marshallable_class" do
11
+ before(:each) do
12
+ described_class.stub(:builtin_settings) {{:name => Object}}
13
+ end
14
+ context "when the converter name is one of builtins" do
15
+ subject {described_class.new :name}
16
+ it "returns the corresponding class" do
17
+ subject.marshallable_class.should eq(Object)
18
+ end
19
+ end
20
+ context "when the converter name is not one of builtins" do
21
+ subject {described_class.new Module}
22
+ it "returns the name" do
23
+ subject.marshallable_class.should eq(Module)
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+
@@ -4,7 +4,17 @@ describe RedisCacheMailerDelivery do
4
4
  describe ".install" do
5
5
  it "adds a delivery method to the ActionMailer" do
6
6
  described_class.install
7
- ActionMailer::Base.delivery_methods.should include(:redis_cache => Mail::CacheDelivery)
7
+ ActionMailer::Base.delivery_methods.should include(:redis_cache => RedisCacheMailerDelivery::CacheDelivery)
8
+ end
9
+
10
+ it "defaults the redis_key_name to redis_cache_mailer_delivery:mail_messages" do
11
+ described_class.install
12
+ ActionMailer::Base.redis_cache_settings[:redis_key_name].should eq('redis_cache_mailer_delivery:mail_messages')
13
+ end
14
+
15
+ it "defaults the marshallable converters to empty array" do
16
+ described_class.install
17
+ ActionMailer::Base.redis_cache_settings[:marshallable_converters].should eq([])
8
18
  end
9
19
  end
10
20
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redis_cache_mailer_delivery
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 4
10
- version: 0.0.4
9
+ - 5
10
+ version: 0.0.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - Yi Wen
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-03-25 00:00:00 Z
18
+ date: 2012-03-26 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: redis
@@ -150,15 +150,17 @@ files:
150
150
  - README.md
151
151
  - Rakefile
152
152
  - lib/redis_cache_mailer_delivery.rb
153
+ - lib/redis_cache_mailer_delivery/cache_delivery.rb
154
+ - lib/redis_cache_mailer_delivery/converters/sequel_marshallable_mail_message.rb
153
155
  - lib/redis_cache_mailer_delivery/deliveries.rb
154
- - lib/redis_cache_mailer_delivery/mail/cache_delivery.rb
155
- - lib/redis_cache_mailer_delivery/mail/sequel_marshallable_mail_message.rb
156
+ - lib/redis_cache_mailer_delivery/marshallable_converter_setting.rb
156
157
  - lib/redis_cache_mailer_delivery/railtie.rb
157
158
  - lib/redis_cache_mailer_delivery/version.rb
158
159
  - redis_cache_mailer_delivery.gemspec
160
+ - spec/redis_cache_mailer_delivery/cache_delivery_spec.rb
161
+ - spec/redis_cache_mailer_delivery/converters/sequel_marshallable_mail_message_spec.rb
159
162
  - spec/redis_cache_mailer_delivery/deliveries_spec.rb
160
- - spec/redis_cache_mailer_delivery/mail/cache_delivery_spec.rb
161
- - spec/redis_cache_mailer_delivery/mail/sequel_marshallable_mail_message_spec.rb
163
+ - spec/redis_cache_mailer_delivery/marshallable_converter_setting_spec.rb
162
164
  - spec/redis_cache_mailer_delivery_spec.rb
163
165
  - spec/spec_helper.rb
164
166
  - spec/support/faked_rails.rb
@@ -197,9 +199,10 @@ signing_key:
197
199
  specification_version: 3
198
200
  summary: Use Redis to store the Mail::Message when using in Rails
199
201
  test_files:
202
+ - spec/redis_cache_mailer_delivery/cache_delivery_spec.rb
203
+ - spec/redis_cache_mailer_delivery/converters/sequel_marshallable_mail_message_spec.rb
200
204
  - spec/redis_cache_mailer_delivery/deliveries_spec.rb
201
- - spec/redis_cache_mailer_delivery/mail/cache_delivery_spec.rb
202
- - spec/redis_cache_mailer_delivery/mail/sequel_marshallable_mail_message_spec.rb
205
+ - spec/redis_cache_mailer_delivery/marshallable_converter_setting_spec.rb
203
206
  - spec/redis_cache_mailer_delivery_spec.rb
204
207
  - spec/spec_helper.rb
205
208
  - spec/support/faked_rails.rb
@@ -1,11 +0,0 @@
1
- module Mail
2
- module SequelMarshallableMailMessage
3
- class << self
4
- def marshallable(object)
5
- return object unless object.respond_to?(:record)
6
- object.record.marshallable! if object.record.respond_to?(:marshallable!)
7
- object
8
- end
9
- end
10
- end
11
- end
@@ -1,36 +0,0 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "spec_helper"))
2
-
3
- module Mail
4
- describe CacheDelivery do
5
- subject {described_class.new :redis_key_name => "a-name"}
6
- describe "#deliver!" do
7
- let(:object) {Mail::Message.new}
8
-
9
- after(:each) do
10
- Redis.current.del "a-name"
11
- end
12
- it "puts a object into the redis store" do
13
- subject.deliver!(object)
14
- Redis::List.new("a-name", :marshal => true).values[0].should eq(object)
15
- end
16
-
17
- it "tries to make it marshallable first by passing it through SequelMarshallableMailMessage" do
18
- SequelMarshallableMailMessage.should_receive(:marshallable).with(object).and_return object
19
- subject.deliver!(object)
20
- end
21
-
22
- context "when the object respond to marshallable" do
23
- before(:each) do
24
- def object.marshallable
25
- Mail::Message.new
26
- end
27
- end
28
-
29
- it "calls the method before send to redis" do
30
- object.should_receive(:marshallable).and_return Mail::Message.new
31
- subject.deliver!(object)
32
- end
33
- end
34
- end
35
- end
36
- end