Sha256: 5d705ac7b83141be454e3015e1d468d9e9f5a206aa1bb6ca85b0f2bdd4314332

Contents?: true

Size: 1.44 KB

Versions: 4

Compression:

Stored size: 1.44 KB

Contents

require "eots/engine"

ActiveSupport::Inflector.inflections(:en) do |inflect|
  inflect.acronym "EOTS"
end

module EOTS

  # standard fields you'd see in any email
  STANDARD_FIELDS = %i(from to reply_to cc bcc subject)

  # need a better short name for this... stuff we want to let the dev pass in
  # the email_kind declaration, either as an option or in the block, other than
  # fields....
  NON_FIELD_ITEMS = %i(header footer)

  def self.email_kind(name, options={}, &block)
    kind = create_email_kind(name, options)
    @@kind_stack.push kind
    yield if block_given?
    @@kind_stack.pop
  end

  def self.field(name, label, options={})
    @@kind_stack.last.add_field(name, label, options)
  end

  def self.find_kind(kind_name)
    kind = @@kind_hash[kind_name]
    unless kind
      raise(EOTS::EmailKind:: NotFoundError,
            "Unknown email kind '#{kind_name}'")
    end
    kind
  end

  (STANDARD_FIELDS + NON_FIELD_ITEMS).each do |field|
    define_singleton_method field do |value|
      @@kind_stack.last.send "#{field}=".to_sym, value
    end
  end

  private

  # declare/init innards this way so we can reset them in tests
  def self.reset
    @@kind_stack = []
    @@kind_hash = {}
  end
  self.reset

  def self.create_email_kind(name, options)
    raise(EmailKind::AlreadyDefinedError, "Email kind #{name} is already defined") if @@kind_hash[name]
    @@kind_hash[name] = EOTS::EmailKind.new(name, @@kind_stack.last, options)
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
eots-0.0.4 lib/eots.rb
eots-0.0.3 lib/eots.rb
eots-0.0.2 lib/eots.rb
eots-0.0.1 lib/eots.rb