hanami-mailer 1.3.3 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,628 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "hanami/mailer/rendering/template_name"
4
- require "hanami/mailer/rendering/templates_finder"
5
-
6
- module Hanami
7
- module Mailer
8
- # Class level DSL
9
- #
10
- # @since 0.1.0
11
- module Dsl
12
- # @since 0.3.0
13
- # @api private
14
- def self.extended(base)
15
- base.class_eval do
16
- @from = nil
17
- @to = nil
18
- @cc = nil
19
- @bcc = nil
20
- @reply_to = nil
21
- @return_path = nil
22
- @subject = nil
23
- end
24
- end
25
-
26
- # Set the template name IF it differs from the convention.
27
- #
28
- # For a given mailer named <tt>Signup::Welcome</tt> it will look for
29
- # <tt>signup/welcome.*.*</tt> templates under the root directory.
30
- #
31
- # If for some reason, we need to specify a different template name, we can
32
- # use this method.
33
- #
34
- # This is part of a DSL, for this reason when this method is called with
35
- # an argument, it will set the corresponding class variable. When
36
- # called without, it will return the already set value, or the default.
37
- #
38
- # @overload template(value)
39
- # Sets the given value
40
- # @param value [String, #to_s] relative template path, under root
41
- # @return [NilClass]
42
- #
43
- # @overload template
44
- # Gets the template name
45
- # @return [String]
46
- #
47
- # @since 0.1.0
48
- #
49
- # @see Hanami::Mailers::Configuration.root
50
- #
51
- # @example Custom template name
52
- # require 'hanami/mailer'
53
- #
54
- # class MyMailer
55
- # include Hanami::Mailer
56
- # template 'mailer'
57
- # end
58
- def template(value = nil)
59
- if value.nil?
60
- @template ||= ::Hanami::Mailer::Rendering::TemplateName.new(name, configuration.namespace).to_s
61
- else
62
- @template = value
63
- end
64
- end
65
-
66
- # Returns a set of associated templates or only one for the given format
67
- #
68
- # This is part of a DSL, for this reason when this method is called with
69
- # an argument, it will set the corresponding class variable. When
70
- # called without, it will return the already set value, or the default.
71
- #
72
- # @overload templates(format)
73
- # Returns the template associated with the given format
74
- # @param value [Symbol] the format
75
- # @return [Hash]
76
- #
77
- # @overload templates
78
- # Returns all the associated templates
79
- # Gets the template name
80
- # @return [Hash] a set of templates
81
- #
82
- # @since 0.1.0
83
- # @api private
84
- def templates(format = nil)
85
- if format.nil?
86
- @templates = ::Hanami::Mailer::Rendering::TemplatesFinder.new(self).find
87
- else
88
- @templates.fetch(format, nil)
89
- end
90
- end
91
-
92
- # Sets the MAIL FROM address for mail messages.
93
- # This lets you specify a "bounce address" different from the sender
94
- # address specified with `from`.
95
- #
96
- # It accepts a hardcoded value as a string, or a symbol that represents
97
- # an instance method for more complex logic.
98
- #
99
- # This value is optional.
100
- #
101
- # When a value is given, specify the MAIL FROM address of the email
102
- # Otherwise, it returns the MAIL FROM address of the email
103
- #
104
- # This is part of a DSL, for this reason when this method is called with
105
- # an argument, it will set the corresponding class variable. When
106
- # called without, it will return the already set value, or the default.
107
- #
108
- # @overload return_path(value)
109
- # Sets the MAIL FROM address
110
- # @param value [String, Symbol] the hardcoded value or method name
111
- # @return [NilClass]
112
- #
113
- # @overload return_path
114
- # Returns the MAIL FROM address
115
- # @return [String, Symbol] the MAIL FROM address
116
- #
117
- # @since 1.3.2
118
- #
119
- # @example Hardcoded value (String)
120
- # require 'hanami/mailer'
121
- #
122
- # class WelcomeMailer
123
- # include Hanami::Mailer
124
- #
125
- # return_path "bounce@example.com"
126
- # end
127
- #
128
- # @example Method (Symbol)
129
- # require 'hanami/mailer'
130
- #
131
- # class WelcomeMailer
132
- # include Hanami::Mailer
133
- # return_path :bounce_address
134
- #
135
- # private
136
- #
137
- # def bounce_address
138
- # "bounce@example.com"
139
- # end
140
- # end
141
- def return_path(value = nil)
142
- if value.nil?
143
- @return_path
144
- else
145
- @return_path = value
146
- end
147
- end
148
-
149
- # Sets the sender for mail messages
150
- #
151
- # It accepts a hardcoded value as a string, or a symbol that represents
152
- # an instance method for more complex logic.
153
- #
154
- # This value MUST be set, otherwise an exception is raised at the delivery
155
- # time.
156
- #
157
- # When a value is given, specify the sender of the email
158
- # Otherwise, it returns the sender of the email
159
- #
160
- # This is part of a DSL, for this reason when this method is called with
161
- # an argument, it will set the corresponding class variable. When
162
- # called without, it will return the already set value, or the default.
163
- #
164
- # @overload from(value)
165
- # Sets the sender
166
- # @param value [String, Symbol] the hardcoded value or method name
167
- # @return [NilClass]
168
- #
169
- # @overload from
170
- # Returns the sender
171
- # @return [String, Symbol] the sender
172
- #
173
- # @since 0.1.0
174
- #
175
- # @example Hardcoded value (String)
176
- # require 'hanami/mailer'
177
- #
178
- # class WelcomeMailer
179
- # include Hanami::Mailer
180
- #
181
- # from "noreply@example.com"
182
- # end
183
- #
184
- # @example Method (Symbol)
185
- # require 'hanami/mailer'
186
- #
187
- # class WelcomeMailer
188
- # include Hanami::Mailer
189
- # from :sender
190
- #
191
- # private
192
- #
193
- # def sender
194
- # "noreply@example.com"
195
- # end
196
- # end
197
- def from(value = nil)
198
- if value.nil?
199
- @from
200
- else
201
- @from = value
202
- end
203
- end
204
-
205
- # Sets the cc (carbon copy) for mail messages
206
- #
207
- # It accepts a hardcoded value as a string or array of strings.
208
- # For dynamic values, you can specify a symbol that represents an instance
209
- # method.
210
- #
211
- # This value is optional.
212
- #
213
- # When a value is given, it specifies the cc for the email.
214
- # When a value is not given, it returns the cc of the email.
215
- #
216
- # This is part of a DSL, for this reason when this method is called with
217
- # an argument, it will set the corresponding class variable. When
218
- # called without, it will return the already set value, or the default.
219
- #
220
- # @overload cc(value)
221
- # Sets the cc
222
- # @param value [String, Array, Symbol] the hardcoded value or method name
223
- # @return [NilClass]
224
- #
225
- # @overload cc
226
- # Returns the cc
227
- # @return [String, Array, Symbol] the recipient
228
- #
229
- # @since 0.3.0
230
- #
231
- # @example Hardcoded value (String)
232
- # require 'hanami/mailer'
233
- #
234
- # class WelcomeMailer
235
- # include Hanami::Mailer
236
- #
237
- # to "user@example.com"
238
- # cc "other.user@example.com"
239
- # end
240
- #
241
- # @example Hardcoded value (Array)
242
- # require 'hanami/mailer'
243
- #
244
- # class WelcomeMailer
245
- # include Hanami::Mailer
246
- #
247
- # to ["user-1@example.com", "user-2@example.com"]
248
- # cc ["other.user-1@example.com", "other.user-2@example.com"]
249
- # end
250
- #
251
- # @example Method (Symbol)
252
- # require 'hanami/mailer'
253
- #
254
- # class WelcomeMailer
255
- # include Hanami::Mailer
256
- # to "user@example.com"
257
- # cc :email_address
258
- #
259
- # private
260
- #
261
- # def email_address
262
- # user.email
263
- # end
264
- # end
265
- #
266
- # other_user = User.new(name: 'L')
267
- # WelcomeMailer.deliver(user: other_user)
268
- #
269
- # @example Method that returns a collection of recipients
270
- # require 'hanami/mailer'
271
- #
272
- # class WelcomeMailer
273
- # include Hanami::Mailer
274
- # to "user@example.com"
275
- # cc :recipients
276
- #
277
- # private
278
- #
279
- # def recipients
280
- # users.map(&:email)
281
- # end
282
- # end
283
- #
284
- # other_users = [User.new(name: 'L'), User.new(name: 'MG')]
285
- # WelcomeMailer.deliver(users: other_users)
286
- def cc(value = nil)
287
- if value.nil?
288
- @cc
289
- else
290
- @cc = value
291
- end
292
- end
293
-
294
- # Sets the bcc (blind carbon copy) for mail messages
295
- #
296
- # It accepts a hardcoded value as a string or array of strings.
297
- # For dynamic values, you can specify a symbol that represents an instance
298
- # method.
299
- #
300
- # This value is optional.
301
- #
302
- # When a value is given, it specifies the bcc for the email.
303
- # When a value is not given, it returns the bcc of the email.
304
- #
305
- # This is part of a DSL, for this reason when this method is called with
306
- # an argument, it will set the corresponding class variable. When
307
- # called without, it will return the already set value, or the default.
308
- #
309
- # @overload bcc(value)
310
- # Sets the bcc
311
- # @param value [String, Array, Symbol] the hardcoded value or method name
312
- # @return [NilClass]
313
- #
314
- # @overload bcc
315
- # Returns the bcc
316
- # @return [String, Array, Symbol] the recipient
317
- #
318
- # @since 0.3.0
319
- #
320
- # @example Hardcoded value (String)
321
- # require 'hanami/mailer'
322
- #
323
- # class WelcomeMailer
324
- # include Hanami::Mailer
325
- #
326
- # to "user@example.com"
327
- # bcc "other.user@example.com"
328
- # end
329
- #
330
- # @example Hardcoded value (Array)
331
- # require 'hanami/mailer'
332
- #
333
- # class WelcomeMailer
334
- # include Hanami::Mailer
335
- #
336
- # to ["user-1@example.com", "user-2@example.com"]
337
- # bcc ["other.user-1@example.com", "other.user-2@example.com"]
338
- # end
339
- #
340
- # @example Method (Symbol)
341
- # require 'hanami/mailer'
342
- #
343
- # class WelcomeMailer
344
- # include Hanami::Mailer
345
- # to "user@example.com"
346
- # bcc :email_address
347
- #
348
- # private
349
- #
350
- # def email_address
351
- # user.email
352
- # end
353
- # end
354
- #
355
- # other_user = User.new(name: 'L')
356
- # WelcomeMailer.deliver(user: other_user)
357
- #
358
- # @example Method that returns a collection of recipients
359
- # require 'hanami/mailer'
360
- #
361
- # class WelcomeMailer
362
- # include Hanami::Mailer
363
- # to "user@example.com"
364
- # bcc :recipients
365
- #
366
- # private
367
- #
368
- # def recipients
369
- # users.map(&:email)
370
- # end
371
- # end
372
- #
373
- # other_users = [User.new(name: 'L'), User.new(name: 'MG')]
374
- # WelcomeMailer.deliver(users: other_users)
375
- def bcc(value = nil)
376
- if value.nil?
377
- @bcc
378
- else
379
- @bcc = value
380
- end
381
- end
382
-
383
- # Sets the reply_to for mail messages
384
- #
385
- # It accepts a hardcoded value as a string or array of strings.
386
- # For dynamic values, you can specify a symbol that represents an instance
387
- # method.
388
- #
389
- # This value is optional.
390
- #
391
- # When a value is given, it specifies the reply_to for the email.
392
- # When a value is not given, it returns the reply_to of the email.
393
- #
394
- # This is part of a DSL, for this reason when this method is called with
395
- # an argument, it will set the corresponding class variable. When
396
- # called without, it will return the already set value, or the default.
397
- #
398
- # @overload reply_to(value)
399
- # Sets the reply_to
400
- # @param value [String, Array, Symbol] the hardcoded value or method name
401
- # @return [NilClass]
402
- #
403
- # @overload reply_to
404
- # Returns the reply_to
405
- # @return [String, Array, Symbol] the recipient
406
- #
407
- # @since 1.3.0
408
- #
409
- # @example Hardcoded value (String)
410
- # require 'hanami/mailer'
411
- #
412
- # class WelcomeMailer
413
- # include Hanami::Mailer
414
- #
415
- # to "user@example.com"
416
- # reply_to "other.user@example.com"
417
- # end
418
- #
419
- # @example Hardcoded value (Array)
420
- # require 'hanami/mailer'
421
- #
422
- # class WelcomeMailer
423
- # include Hanami::Mailer
424
- #
425
- # to ["user-1@example.com", "user-2@example.com"]
426
- # reply_to ["other.user-1@example.com", "other.user-2@example.com"]
427
- # end
428
- #
429
- # @example Method (Symbol)
430
- # require 'hanami/mailer'
431
- #
432
- # class WelcomeMailer
433
- # include Hanami::Mailer
434
- # to "user@example.com"
435
- # reply_to :email_address
436
- #
437
- # private
438
- #
439
- # def email_address
440
- # user.email
441
- # end
442
- # end
443
- #
444
- # other_user = User.new(name: 'L')
445
- # WelcomeMailer.deliver(user: other_user)
446
- #
447
- # @example Method that returns a collection of recipients
448
- # require 'hanami/mailer'
449
- #
450
- # class WelcomeMailer
451
- # include Hanami::Mailer
452
- # to "user@example.com"
453
- # reply_to :recipients
454
- #
455
- # private
456
- #
457
- # def recipients
458
- # users.map(&:email)
459
- # end
460
- # end
461
- #
462
- # other_users = [User.new(name: 'L'), User.new(name: 'MG')]
463
- # WelcomeMailer.deliver(users: other_users)
464
- def reply_to(value = nil)
465
- if value.nil?
466
- @reply_to
467
- else
468
- @reply_to = value
469
- end
470
- end
471
-
472
- # Sets the recipient for mail messages
473
- #
474
- # It accepts a hardcoded value as a string or array of strings.
475
- # For dynamic values, you can specify a symbol that represents an instance
476
- # method.
477
- #
478
- # This value MUST be set, otherwise an exception is raised at the delivery
479
- # time.
480
- #
481
- # When a value is given, specify the recipient of the email
482
- # Otherwise, it returns the recipient of the email
483
- #
484
- # This is part of a DSL, for this reason when this method is called with
485
- # an argument, it will set the corresponding class variable. When
486
- # called without, it will return the already set value, or the default.
487
- #
488
- # @overload to(value)
489
- # Sets the recipient
490
- # @param value [String, Array, Symbol] the hardcoded value or method name
491
- # @return [NilClass]
492
- #
493
- # @overload to
494
- # Returns the recipient
495
- # @return [String, Array, Symbol] the recipient
496
- #
497
- # @since 0.1.0
498
- #
499
- # @example Hardcoded value (String)
500
- # require 'hanami/mailer'
501
- #
502
- # class WelcomeMailer
503
- # include Hanami::Mailer
504
- #
505
- # to "user@example.com"
506
- # end
507
- #
508
- # @example Hardcoded value (Array)
509
- # require 'hanami/mailer'
510
- #
511
- # class WelcomeMailer
512
- # include Hanami::Mailer
513
- #
514
- # to ["user-1@example.com", "user-2@example.com"]
515
- # end
516
- #
517
- # @example Method (Symbol)
518
- # require 'hanami/mailer'
519
- #
520
- # class WelcomeMailer
521
- # include Hanami::Mailer
522
- # to :email_address
523
- #
524
- # private
525
- #
526
- # def email_address
527
- # user.email
528
- # end
529
- # end
530
- #
531
- # user = User.new(name: 'L')
532
- # WelcomeMailer.deliver(user: user)
533
- #
534
- # @example Method that returns a collection of recipients
535
- # require 'hanami/mailer'
536
- #
537
- # class WelcomeMailer
538
- # include Hanami::Mailer
539
- # to :recipients
540
- #
541
- # private
542
- #
543
- # def recipients
544
- # users.map(&:email)
545
- # end
546
- # end
547
- #
548
- # users = [User.new(name: 'L'), User.new(name: 'MG')]
549
- # WelcomeMailer.deliver(users: users)
550
- def to(value = nil)
551
- if value.nil?
552
- @to
553
- else
554
- @to = value
555
- end
556
- end
557
-
558
- # Sets the subject for mail messages
559
- #
560
- # It accepts a hardcoded value as a string, or a symbol that represents
561
- # an instance method for more complex logic.
562
- #
563
- # This value MUST be set, otherwise an exception is raised at the delivery
564
- # time.
565
- #
566
- # This is part of a DSL, for this reason when this method is called with
567
- # an argument, it will set the corresponding class variable. When
568
- # called without, it will return the already set value, or the default.
569
- #
570
- # @overload subject(value)
571
- # Sets the subject
572
- # @param value [String, Symbol] the hardcoded value or method name
573
- # @return [NilClass]
574
- #
575
- # @overload subject
576
- # Returns the subject
577
- # @return [String, Symbol] the subject
578
- #
579
- # @since 0.1.0
580
- #
581
- # @example Hardcoded value (String)
582
- # require 'hanami/mailer'
583
- #
584
- # class WelcomeMailer
585
- # include Hanami::Mailer
586
- #
587
- # subject "Welcome"
588
- # end
589
- #
590
- # @example Method (Symbol)
591
- # require 'hanami/mailer'
592
- #
593
- # class WelcomeMailer
594
- # include Hanami::Mailer
595
- # subject :greeting
596
- #
597
- # private
598
- #
599
- # def greeting
600
- # "Hello, #{ user.name }"
601
- # end
602
- # end
603
- #
604
- # user = User.new(name: 'L')
605
- # WelcomeMailer.deliver(user: user)
606
- def subject(value = nil)
607
- if value.nil?
608
- @subject
609
- else
610
- @subject = value
611
- end
612
- end
613
-
614
- protected
615
-
616
- # Loading mechanism hook.
617
- #
618
- # @api private
619
- # @since 0.1.0
620
- #
621
- # @see Hanami::Mailer.load!
622
- def load!
623
- templates.freeze
624
- configuration.freeze
625
- end
626
- end
627
- end
628
- end
@@ -1,55 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "hanami/utils/string"
4
-
5
- module Hanami
6
- module Mailer
7
- module Rendering
8
- # @since 0.1.0
9
- # @api private
10
- #
11
- # TODO this is identical to Hanami::View, consider to move into Hanami::Utils
12
- class TemplateName
13
- # @since 0.1.0
14
- # @api private
15
- NAMESPACE_SEPARATOR = "::"
16
-
17
- # @since 0.1.0
18
- # @api private
19
- def initialize(name, namespace)
20
- @name = name
21
- compile!(namespace)
22
- end
23
-
24
- # @since 0.1.0
25
- # @api private
26
- def to_s
27
- @name
28
- end
29
-
30
- private
31
-
32
- # @since 0.1.0
33
- # @api private
34
- def compile!(namespace)
35
- tokens(namespace) { |token| replace!(token) }
36
- @name = Utils::String.underscore(@name)
37
- end
38
-
39
- # @since 0.1.0
40
- # @api private
41
- def tokens(namespace)
42
- namespace.to_s.split(NAMESPACE_SEPARATOR).each do |token|
43
- yield token
44
- end
45
- end
46
-
47
- # @since 0.1.0
48
- # @api private
49
- def replace!(token)
50
- @name = @name.gsub(/\A#{token}#{NAMESPACE_SEPARATOR}/, "")
51
- end
52
- end
53
- end
54
- end
55
- end