outbox 0.1.1 → 0.1.2

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: 0b94d8233a2c331495060c6d33e8e94b2d3d0cae
4
- data.tar.gz: eb43a9e7d7b3ced480238c6b47ac0ca6eb5af721
3
+ metadata.gz: 7b0251b3437146e36c621521831de3f1b1f66754
4
+ data.tar.gz: edf0111ebfff3b8cf1d7a6716ac3fb72c59f934a
5
5
  SHA512:
6
- metadata.gz: 14d61db45eff14c42092302401dd1317bfeffb13fa500f3ad86f7d387b3a766049b36dcf69e525e63f3c9f6605c34d8e2d87cfe217bd67f7da40d712cd19b9a1
7
- data.tar.gz: 5fcf9d9938dbece72b956402b0644a08b7c7bb7dd2ec51d36b16c49d3aa7cc1e1d677b246b18b81fe02b12e7311181adbc42de07110849e2af03b1a3dffb21ce
6
+ metadata.gz: 0fab54a11397b3fdd99407adcd41f16f10f624eba332686cf3c8c0fb3924605b5590f226da3fe2a51602a1aaad1ab8a48a5d1bb5cf2b7b9b664e0f9966fed1ed
7
+ data.tar.gz: b3228876ee7f25bd9dfe9d53983fb691c7ad907c628ba227d0a58b621a4482f013cf1c50efc9578dea886ac81e3cf6ea04da91179fbb37cf0a2a60b7a00dbae2
@@ -21,7 +21,7 @@ module Outbox
21
21
  # }
22
22
  # client.delivery_method_settings #=> { location: '/usr/bin/sendmail' }
23
23
  def delivery_method_settings
24
- settings[:delivery_method_settings] || settings[:"#{delivery_method}_settings"]
24
+ settings[:delivery_method_settings] || settings[:"#{delivery_method}_settings"] || {}
25
25
  end
26
26
 
27
27
  def deliver(email)
@@ -4,6 +4,14 @@ module Outbox
4
4
 
5
5
  register_message_type :email, Outbox::Messages::Email
6
6
 
7
+ # Use the Outbox::Clients::TestClient for all message types. This is
8
+ # useful for testing or working an a development environment.
9
+ def self.use_test_client
10
+ message_types.each_value do |message_type|
11
+ message_type.default_client(:test)
12
+ end
13
+ end
14
+
7
15
  # Make a new message. Every message can be created using a hash,
8
16
  # block, or direct assignment.
9
17
  #
@@ -13,13 +13,26 @@ module Outbox
13
13
  #
14
14
  # Email.default_client :test, option: 'foo'
15
15
  # Email.default_client #=> #<Outbox::Clients::TestClient>
16
- def default_client(client = nil, options = nil)
17
- if client.nil?
18
- @default_client
19
- else
20
- @default_client = get_client(client, options)
16
+ def default_client(client = nil, settings = nil)
17
+ @default_client = get_client(client, settings) if client
18
+ @default_client
19
+ end
20
+
21
+ # Sets the default client.
22
+ def default_client=(client)
23
+ @default_client = get_client(client)
24
+ end
25
+
26
+ # Configure the default client.
27
+ def default_client_settings(settings = nil)
28
+ if default_client.nil?
29
+ raise ArgumentError, 'default_client must be set before setting default_client_settings'
21
30
  end
31
+
32
+ default_client.settings.merge!(settings) if settings
33
+ default_client.settings
22
34
  end
35
+ alias :default_client_settings= :default_client_settings
23
36
 
24
37
  # Registers a client class with an alias.
25
38
  #
@@ -15,13 +15,10 @@ module Outbox
15
15
  # message.from #=> 'bob@example.com'
16
16
  def defaults(defaults = nil)
17
17
  @defaults ||= {}
18
-
19
- if defaults.nil?
20
- @defaults
21
- else
22
- @defaults.merge!(defaults)
23
- end
18
+ @defaults.merge!(defaults) if defaults
19
+ @defaults
24
20
  end
21
+ alias :defaults= :defaults
25
22
 
26
23
  # Returns the defined fields for this message type.
27
24
  #
@@ -26,7 +26,9 @@ module Outbox
26
26
  message_types[name.to_sym] = message_type
27
27
  define_message_type_reader(name, message_type)
28
28
  define_message_type_writer(name)
29
- define_default_message_type_client_accessor(name, message_type)
29
+ define_default_message_type_client_accessors(name, message_type)
30
+ define_default_message_type_client_settings_accessors(name, message_type)
31
+ define_message_type_defaults_accessors(name, message_type)
30
32
  end
31
33
 
32
34
  # Returns a hash of the registred message types, where the key is the name
@@ -63,10 +65,34 @@ module Outbox
63
65
  end
64
66
  end
65
67
 
66
- def define_default_message_type_client_accessor(name, message_type)
68
+ def define_default_message_type_client_accessors(name, message_type)
67
69
  define_singleton_method "default_#{name}_client" do |*args|
68
70
  message_type.default_client(*args)
69
71
  end
72
+
73
+ define_singleton_method "default_#{name}_client=" do |client|
74
+ message_type.default_client = client
75
+ end
76
+ end
77
+
78
+ def define_default_message_type_client_settings_accessors(name, message_type)
79
+ define_singleton_method "default_#{name}_client_settings" do |*args|
80
+ message_type.default_client_settings(*args)
81
+ end
82
+
83
+ define_singleton_method "default_#{name}_client_settings=" do |settings|
84
+ message_type.default_client_settings = settings
85
+ end
86
+ end
87
+
88
+ def define_message_type_defaults_accessors(name, message_type)
89
+ define_singleton_method "#{name}_defaults" do |*args|
90
+ message_type.defaults(*args)
91
+ end
92
+
93
+ define_singleton_method "#{name}_defaults=" do |defaults|
94
+ message_type.defaults = defaults
95
+ end
70
96
  end
71
97
  end
72
98
 
@@ -1,3 +1,3 @@
1
1
  module Outbox
2
- VERSION = '0.1.1'
2
+ VERSION = '0.1.2'
3
3
  end
@@ -1,7 +1,11 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Outbox::Message do
4
+ class MockTelepathyClient < Outbox::Clients::TestClient
5
+ end
6
+
4
7
  class Telepathy < Outbox::Messages::Base
8
+ register_client_alias :mock, MockTelepathyClient
5
9
  default_client :test
6
10
  fields :to, :from, :thought
7
11
 
@@ -27,6 +31,22 @@ describe Outbox::Message do
27
31
 
28
32
  after do
29
33
  Outbox::Message.instance_variable_set :@message_types, @original_message_types
34
+ Telepathy.instance_variable_set :@defaults, {}
35
+ Telepathy.default_client :test
36
+ end
37
+
38
+ describe '.use_test_client' do
39
+ after do
40
+ Outbox::Message.default_email_client :mail
41
+ end
42
+
43
+ it 'sets the default client to TestClient for all message types' do
44
+ Telepathy.default_client :mock
45
+ Outbox::Message.use_test_client
46
+ expect(Telepathy.default_client).to be_a(Outbox::Clients::TestClient)
47
+ expect(MessageInABottle.default_client).to be_a(Outbox::Clients::TestClient)
48
+ expect(Outbox::Messages::Email.default_client).to be_a(Outbox::Clients::TestClient)
49
+ end
30
50
  end
31
51
 
32
52
  describe '.message_types' do
@@ -71,20 +91,53 @@ describe Outbox::Message do
71
91
  end
72
92
  end
73
93
 
74
- describe 'default message type client' do
75
- after do
76
- Telepathy.registered_client_aliases.delete(:foo)
77
- Telepathy.default_client :test
94
+ describe '.default_[message_type]_client' do
95
+ it 'sets the default client for that message type' do
96
+ Outbox::Message.default_telepathy_client :mock, option_1: 1
97
+ client = Outbox::Message.default_telepathy_client
98
+ expect(Telepathy.default_client).to be(client)
99
+ expect(client).to be_a(MockTelepathyClient)
100
+ expect(client.settings[:option_1]).to eq(1)
78
101
  end
102
+ end
79
103
 
104
+ describe '.default_[message_type]_client=' do
80
105
  it 'sets the default client for that message type' do
81
- client_class = Class.new
82
- Telepathy.register_client_alias :foo, client_class
83
- client = double :client
84
- options = { option_1: 1, option_2: 2 }
85
- expect(client_class).to receive(:new).with(options) { client}
86
- Outbox::Message.default_telepathy_client :foo, options
106
+ Outbox::Message.default_telepathy_client = :mock
107
+ client = Outbox::Message.default_telepathy_client
87
108
  expect(Telepathy.default_client).to be(client)
109
+ expect(client).to be_a(MockTelepathyClient)
110
+ end
111
+ end
112
+
113
+ describe '.default_[message_type]_client_settings' do
114
+ it 'sets the default client for that message type' do
115
+ Outbox::Message.default_telepathy_client_settings option_1: 1
116
+ client = Outbox::Message.default_telepathy_client
117
+ expect(client.settings[:option_1]).to eq(1)
118
+ end
119
+ end
120
+
121
+ describe '.default_[message_type]_client_settings=' do
122
+ it 'sets the default client for that message type' do
123
+ Outbox::Message.default_telepathy_client_settings = { option_1: 1 }
124
+ client = Outbox::Message.default_telepathy_client
125
+ expect(client.settings[:option_1]).to eq(1)
126
+ end
127
+ end
128
+
129
+ describe '.[message_type]_defaults' do
130
+ it 'sets the defaults for that message type' do
131
+ Outbox::Message.telepathy_defaults from: 'Me'
132
+ expect(Telepathy.new.from).to eq('Me')
133
+ end
134
+ end
135
+
136
+ describe '.[message_type]_defaults=' do
137
+ it 'sets the defaults for that message type' do
138
+ Outbox::Message.telepathy_defaults = { from: 'Me' }
139
+ expect(Outbox::Message.telepathy_defaults).to eq(Telepathy.defaults)
140
+ expect(Telepathy.new.from).to eq('Me')
88
141
  end
89
142
  end
90
143
 
@@ -40,9 +40,11 @@ describe Outbox::Messages::Base do
40
40
  field :body, writer: false
41
41
  end
42
42
 
43
- describe '.default_client' do
44
- after { Message.default_client :test }
43
+ after do
44
+ Message.default_client :test
45
+ end
45
46
 
47
+ describe '.default_client' do
46
48
  it 'sets the default client' do
47
49
  client = double :client
48
50
  allow(client).to receive(:dup) { client }
@@ -52,9 +54,42 @@ describe Outbox::Messages::Base do
52
54
  end
53
55
  end
54
56
 
55
- describe '.register_client' do
56
- after { Message.default_client :test }
57
+ describe '.default_client=' do
58
+ it 'sets the default client' do
59
+ client = double :client
60
+ allow(client).to receive(:dup) { client }
61
+ Message.default_client = client
62
+ message = Message.new
63
+ expect(message.client).to be(client)
64
+ end
65
+ end
57
66
 
67
+ describe '.default_client_settings' do
68
+ it 'sets the default client settings' do
69
+ Message.default_client_settings option_1: true
70
+ message = Message.new
71
+ expect(message.client.settings[:option_1]).to eq(true)
72
+ end
73
+
74
+ context 'without a default client' do
75
+ it 'sets the default client settings once set' do
76
+ Message.instance_variable_set :@default_client, nil
77
+ expect {
78
+ Message.default_client_settings option_1: true
79
+ }.to raise_error(ArgumentError)
80
+ end
81
+ end
82
+ end
83
+
84
+ describe '.default_client_settings=' do
85
+ it 'sets the default client settings' do
86
+ Message.default_client_settings = { option_1: true }
87
+ message = Message.new
88
+ expect(message.client.settings[:option_1]).to eq(true)
89
+ end
90
+ end
91
+
92
+ describe '.register_client' do
58
93
  it 'registers a client alias' do
59
94
  client_class = Class.new
60
95
  Message.register_client_alias :foo, client_class
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: outbox
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pete Browne