Class: Courier::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/courier/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



6
7
8
9
10
11
# File 'lib/courier/config.rb', line 6

def initialize
  self.services_order=[]
  self.services_hash={}
  self.templates_order=[]
  self.templates_hash={}
end

Instance Attribute Details

#services_hashObject

Returns the value of attribute services_hash.



4
5
6
# File 'lib/courier/config.rb', line 4

def services_hash
  @services_hash
end

#services_orderObject

Returns the value of attribute services_order.



4
5
6
# File 'lib/courier/config.rb', line 4

def services_order
  @services_order
end

#templates_hashObject

Returns the value of attribute templates_hash.



4
5
6
# File 'lib/courier/config.rb', line 4

def templates_hash
  @templates_hash
end

#templates_orderObject

Returns the value of attribute templates_order.



4
5
6
# File 'lib/courier/config.rb', line 4

def templates_order
  @templates_order
end

Instance Method Details

#class_of_service(name) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/courier/config.rb', line 48

def class_of_service(name)
  if name.is_a? Symbol
    "Courier::Service::#{name.to_s.classify}".constantize
  else
    name
  end
end

#get_service(name) ⇒ Object



27
28
29
30
# File 'lib/courier/config.rb', line 27

def get_service name
  name=name.to_sym
  services_hash[name] or raise "No such service '#{name}'. Specify it by Courier.init in ./config/initializers/courier.rb"
end

#get_template(key) ⇒ Object



44
45
46
# File 'lib/courier/config.rb', line 44

def get_template key
  templates_hash[key.to_sym] or raise "No such template '#{key}'. Specify it by Courier.init in ./config/initializers/courier.rb"
end

#services(*services) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/courier/config.rb', line 17

def services *services
  return services_order if services.empty?
  raise 'Список сервисов уже определен' unless services_order.empty?

  self.services_order = services.map { |s|
    service = class_of_service(s).new
    services_hash[service.name] = service
  }
end

#template(name, *sets) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/courier/config.rb', line 32

def template name, *sets
  raise "Values (#{sets.count}) and services counts (#{services_order.count}) not much" unless services_order.count==sets.count
  template = Courier::Template::Base.new(:name=>name)
  raise "Such template is already defined #{name}" if templates_hash.has_key? template.name
  sets.each_with_index do |val, index|
    service = services_order[index] or "Too many values (#{index}), no such services"
    template.set(service, val)
  end
  templates_order << template
  templates_hash[template.name] = template
end

#templatesObject



13
14
15
# File 'lib/courier/config.rb', line 13

def templates
  templates_order
end