Class: Nisetegami::Mapping

Inherits:
Object
  • Object
show all
Defined in:
lib/nisetegami/mapping.rb

Constant Summary collapse

SEPARATOR =
"#"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMapping

Returns a new instance of Mapping.



7
8
9
10
# File 'lib/nisetegami/mapping.rb', line 7

def initialize
  @mailers = Set.new
  @mapping = {}
end

Instance Attribute Details

#mailersObject (readonly)

Returns the value of attribute mailers.



3
4
5
# File 'lib/nisetegami/mapping.rb', line 3

def mailers
  @mailers
end

Instance Method Details

#actions(mailer) ⇒ Object



44
45
46
47
# File 'lib/nisetegami/mapping.rb', line 44

def actions(mailer)
  klass = mailer.constantize
  klass.ancestors.include?(ActionMailer::Base) && klass.action_methods
end

#lookup(mailer, action) ⇒ Object



21
22
23
# File 'lib/nisetegami/mapping.rb', line 21

def lookup(mailer, action)
  @mapping["#{mailer}#{SEPARATOR}#{action}"] || {}
end

#populate!Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/nisetegami/mapping.rb', line 25

def populate!
  Nisetegami::Template.find_each { |template| template.destroy unless action_exists?(template[:mailer], template.action) }

  @mapping.each do |route, locals|
    mailer, action = route.split(SEPARATOR, 2)
    next unless can_populate?(mailer, action)
    variables = expand_locals(*locals).map{ |v| "{{ #{v} }}" }.join(", ")

    Nisetegami::Template.create!(
      subject:   "Subject",
      body_text: "You can use the following variables: #{variables}. Format: text.",
      body_html: "You can use the following variables: #{variables}. Format: html.",
      mailer:  mailer,
      action:  action,
      enabled: false
    )
  end
end

#register(mailer, action, *locals) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/nisetegami/mapping.rb', line 12

def register(mailer, action, *locals)
  mailer = mailer.to_s.classify
  unless action_exists?(mailer, action)
    raise Exceptions::UnknownActionError.new(mailer, action)
  end
  @mapping["#{mailer}#{SEPARATOR}#{action}"] = prepare_locals(*locals)
  @mailers << mailer
end