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_path ⇒ Object
Returns the value of attribute template_path.
Instance Method Summary collapse
- #base_path(part) ⇒ Object
- #call ⇒ Object
-
#initialize(name, mailer) ⇒ Email
constructor
A new instance of Email.
- #mailer_klass ⇒ Object
- #mailer_klass_name ⇒ Object
- #optional_arguments ⇒ 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 |
# File 'lib/maily/email.rb', line 5 def initialize(name, mailer) self.name = name.to_s self.mailer = mailer self.arguments = nil self.template_path = mailer 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_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 |
Instance Method Details
#base_path(part) ⇒ Object
73 74 75 |
# File 'lib/maily/email.rb', line 73 def base_path(part) "#{Rails.root}/app/views/#{template_path}/#{name}.#{part}.erb" end |
#call ⇒ Object
63 64 65 66 67 68 69 70 71 |
# File 'lib/maily/email.rb', line 63 def call *args = arguments && arguments.map { |arg| arg.respond_to?(:call) ? arg.call : arg } if args == [nil] mailer_klass.public_send(name) else mailer_klass.public_send(name, *args) end end |
#mailer_klass ⇒ Object
59 60 61 |
# File 'lib/maily/email.rb', line 59 def mailer_klass mailer_klass_name.constantize end |
#mailer_klass_name ⇒ Object
55 56 57 |
# File 'lib/maily/email.rb', line 55 def mailer_klass_name mailer.camelize end |
#optional_arguments ⇒ Object
25 26 27 |
# File 'lib/maily/email.rb', line 25 def optional_arguments parameters.select { |param| param.first == :opt }.map(&:last) end |
#parameters ⇒ Object
13 14 15 |
# File 'lib/maily/email.rb', line 13 def parameters mailer_klass.instance_method(name).parameters end |
#path(part = nil) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/maily/email.rb', line 77 def path(part = nil) if part base_path(part) else if File.exist?(path('html')) base_path('html') else base_path('text') end end end |
#register_hook(*args) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/maily/email.rb', line 43 def register_hook(*args) args = args.flatten if args.last.is_a?(Hash) self.description = args.last.delete(:description) self.template_path = args.last.delete(:template_path) args.pop end self.arguments = args end |
#require_hook? ⇒ Boolean
17 18 19 |
# File 'lib/maily/email.rb', line 17 def require_hook? parameters.any? end |
#required_arguments ⇒ Object
21 22 23 |
# File 'lib/maily/email.rb', line 21 def required_arguments parameters.select { |param| param.first == :req }.map(&:last) end |
#template(part = nil) ⇒ Object
89 90 91 |
# File 'lib/maily/email.rb', line 89 def template(part = nil) File.read(path(part)) end |
#update_template(new_content, part = nil) ⇒ Object
93 94 95 96 97 |
# File 'lib/maily/email.rb', line 93 def update_template(new_content, part = nil) File.open(path(part), 'w') do |f| f.write(new_content) end end |
#validate_arguments ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/maily/email.rb', line 29 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 |