Sha256: 60f4f78151caefeba7d9df88c50a6013cd691bbe39bf67279ab9cf9149b4afc5

Contents?: true

Size: 1.5 KB

Versions: 2

Compression:

Stored size: 1.5 KB

Contents

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

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

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

  def templates
    templates_order
  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_order << template
    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

2 entries across 2 versions & 1 rubygems

Version Path
courier-0.2.1 lib/courier/config.rb
courier-0.2.0 lib/courier/config.rb