Class: Maily::Email
- Inherits:
-
Object
- Object
- Maily::Email
- Defined in:
- lib/maily/email.rb
Instance Attribute Summary collapse
-
#arguments ⇒ Object
Returns the value of attribute arguments.
-
#description ⇒ Object
Returns the value of attribute description.
-
#mailer ⇒ Object
Returns the value of attribute mailer.
-
#name ⇒ Object
Returns the value of attribute name.
-
#template_name ⇒ Object
Returns the value of attribute template_name.
-
#template_path ⇒ Object
Returns the value of attribute template_path.
-
#with_params ⇒ Object
Returns the value of attribute with_params.
Instance Method Summary collapse
- #base_path(part) ⇒ Object
- #call ⇒ Object
-
#initialize(name, mailer) ⇒ Email
constructor
A new instance of Email.
- #mailer_klass ⇒ Object
- #optional_arguments ⇒ Object
- #parameterized_mailer_klass ⇒ Object
- #parameters ⇒ Object
- #path(part = nil) ⇒ Object
- #register_hook(*args) ⇒ Object
- #require_hook? ⇒ Boolean
- #required_arguments ⇒ Object
- #template(part = nil) ⇒ Object
- #update_template(new_content, part = nil) ⇒ Object
- #validate_arguments ⇒ Object
Constructor Details
#initialize(name, mailer) ⇒ Email
Returns a new instance of Email.
5 6 7 8 9 10 11 12 13 |
# File 'lib/maily/email.rb', line 5 def initialize(name, mailer) self.name = name self.mailer = mailer self.arguments = nil self.with_params = nil self.template_path = mailer.name self.template_name = name self.description = nil end |
Instance Attribute Details
#arguments ⇒ Object
Returns the value of attribute arguments.
3 4 5 |
# File 'lib/maily/email.rb', line 3 def arguments @arguments end |
#description ⇒ Object
Returns the value of attribute description.
3 4 5 |
# File 'lib/maily/email.rb', line 3 def description @description end |
#mailer ⇒ Object
Returns the value of attribute mailer.
3 4 5 |
# File 'lib/maily/email.rb', line 3 def mailer @mailer end |
#name ⇒ Object
Returns the value of attribute name.
3 4 5 |
# File 'lib/maily/email.rb', line 3 def name @name end |
#template_name ⇒ Object
Returns the value of attribute template_name.
3 4 5 |
# File 'lib/maily/email.rb', line 3 def template_name @template_name end |
#template_path ⇒ Object
Returns the value of attribute template_path.
3 4 5 |
# File 'lib/maily/email.rb', line 3 def template_path @template_path end |
#with_params ⇒ Object
Returns the value of attribute with_params.
3 4 5 |
# File 'lib/maily/email.rb', line 3 def with_params @with_params end |
Instance Method Details
#base_path(part) ⇒ Object
90 91 92 |
# File 'lib/maily/email.rb', line 90 def base_path(part) Dir["#{Rails.root}/app/views/#{template_path}/#{template_name}.#{part}.*"].first end |
#call ⇒ Object
80 81 82 83 84 85 86 87 88 |
# File 'lib/maily/email.rb', line 80 def call *args = arguments && arguments.map { |arg| arg.respond_to?(:call) ? arg.call : arg } if args == [nil] parameterized_mailer_klass.public_send(name) else parameterized_mailer_klass.public_send(name, *args) end end |
#mailer_klass ⇒ Object
15 16 17 |
# File 'lib/maily/email.rb', line 15 def mailer_klass mailer.klass end |
#optional_arguments ⇒ Object
40 41 42 |
# File 'lib/maily/email.rb', line 40 def optional_arguments parameters.select { |param| param.first == :opt }.map(&:last) end |
#parameterized_mailer_klass ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/maily/email.rb', line 19 def parameterized_mailer_klass if Rails.version >= '5.1' params = with_params && with_params.transform_values { |param| param.respond_to?(:call) ? param.call : param } mailer_klass.with(params) else mailer_klass end end |
#parameters ⇒ Object
28 29 30 |
# File 'lib/maily/email.rb', line 28 def parameters mailer_klass.instance_method(name).parameters end |
#path(part = nil) ⇒ Object
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/maily/email.rb', line 94 def path(part = nil) return base_path(part) if part html_part = base_path('html') if html_part && File.exist?(html_part) html_part else base_path('text') end end |
#register_hook(*args) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/maily/email.rb', line 58 def register_hook(*args) args = args.flatten if args.last.is_a?(Hash) self.description = args.last.delete(:description) if tpl_path = args.last.delete(:template_path) self.template_path = tpl_path end if tpl_name = args.last.delete(:template_name) self.template_name = tpl_name end self.with_params = args.last.delete(:with_params) args.pop end self.arguments = args end |
#require_hook? ⇒ Boolean
32 33 34 |
# File 'lib/maily/email.rb', line 32 def require_hook? parameters.any? end |
#required_arguments ⇒ Object
36 37 38 |
# File 'lib/maily/email.rb', line 36 def required_arguments parameters.select { |param| param.first == :req }.map(&:last) end |
#template(part = nil) ⇒ Object
105 106 107 |
# File 'lib/maily/email.rb', line 105 def template(part = nil) File.read(path(part)) end |
#update_template(new_content, part = nil) ⇒ Object
109 110 111 112 113 |
# File 'lib/maily/email.rb', line 109 def update_template(new_content, part = nil) File.open(path(part), 'w') do |f| f.write(new_content) end end |
#validate_arguments ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/maily/email.rb', line 44 def validate_arguments from = required_arguments.size to = from + optional_arguments.size passed_by_hook = arguments && arguments.size || 0 if passed_by_hook < from [false, "#{name} email requires at least #{from} arguments, passed #{passed_by_hook}"] elsif passed_by_hook > to [false, "#{name} email requires at the most #{to} arguments, passed #{passed_by_hook}"] else [true, nil] end end |