Class: Maily::Email

Inherits:
Object
  • Object
show all
Defined in:
lib/maily/email.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, mailer) ⇒ Email

Returns a new instance of Email.



5
6
7
8
9
10
11
12
# File 'lib/maily/email.rb', line 5

def initialize(name, mailer)
  self.name          = name
  self.mailer        = mailer
  self.arguments     = nil
  self.template_path = mailer.name
  self.template_name = name
  self.description   = nil
end

Instance Attribute Details

#argumentsObject

Returns the value of attribute arguments.



3
4
5
# File 'lib/maily/email.rb', line 3

def arguments
  @arguments
end

#descriptionObject

Returns the value of attribute description.



3
4
5
# File 'lib/maily/email.rb', line 3

def description
  @description
end

#mailerObject

Returns the value of attribute mailer.



3
4
5
# File 'lib/maily/email.rb', line 3

def mailer
  @mailer
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/maily/email.rb', line 3

def name
  @name
end

#template_nameObject

Returns the value of attribute template_name.



3
4
5
# File 'lib/maily/email.rb', line 3

def template_name
  @template_name
end

#template_pathObject

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



78
79
80
# File 'lib/maily/email.rb', line 78

def base_path(part)
  Dir["#{Rails.root}/app/views/#{template_path}/#{template_name}.#{part}.*"].first
end

#callObject



68
69
70
71
72
73
74
75
76
# File 'lib/maily/email.rb', line 68

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_klassObject



14
15
16
# File 'lib/maily/email.rb', line 14

def mailer_klass
  mailer.klass
end

#optional_argumentsObject



30
31
32
# File 'lib/maily/email.rb', line 30

def optional_arguments
  parameters.select { |param| param.first == :opt }.map(&:last)
end

#parametersObject



18
19
20
# File 'lib/maily/email.rb', line 18

def parameters
  mailer_klass.instance_method(name).parameters
end

#path(part = nil) ⇒ Object



82
83
84
85
86
87
88
89
90
91
# File 'lib/maily/email.rb', line 82

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



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/maily/email.rb', line 48

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

    args.pop
  end

  self.arguments = args
end

#require_hook?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/maily/email.rb', line 22

def require_hook?
  parameters.any?
end

#required_argumentsObject



26
27
28
# File 'lib/maily/email.rb', line 26

def required_arguments
  parameters.select { |param| param.first == :req }.map(&:last)
end

#template(part = nil) ⇒ Object



93
94
95
# File 'lib/maily/email.rb', line 93

def template(part = nil)
  File.read(path(part))
end

#update_template(new_content, part = nil) ⇒ Object



97
98
99
100
101
# File 'lib/maily/email.rb', line 97

def update_template(new_content, part = nil)
  File.open(path(part), 'w') do |f|
    f.write(new_content)
  end
end

#validate_argumentsObject



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/maily/email.rb', line 34

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