Sha256: baea9683e76625074e5fa305243c6ccedb57b355f54307fba4c246dafc953b40

Contents?: true

Size: 1.39 KB

Versions: 4

Compression:

Stored size: 1.39 KB

Contents

# -*- coding: utf-8 -*-

class Courier::Config
  attr_accessor :services_hash, :templates_hash, :services_order

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

  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

  def get_service name
    name=name.to_sym
    services_hash[name] or raise "No such service '#{name}'"
  end

  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_hash[template.name] = template
  end

  def get_template key
    templates_hash[key.to_sym] or raise "No such template '#{key}'"
  end

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
courier-0.1.5 lib/courier/config.rb
courier-0.1.4 lib/courier/config.rb
courier-0.1.3 lib/courier/config.rb
courier-0.1.2 lib/courier/config.rb