Sha256: 05289c8d1ea366d16239a9c8ce4adf70aa61ae5e9df14ba8c894cd909774da66

Contents?: true

Size: 1.41 KB

Versions: 8

Compression:

Stored size: 1.41 KB

Contents

require 'hashie'
require 'simnos/template_helper'
require 'simnos/dsl/sns'

module Simnos
  class DSL
    include Simnos::TemplateHelper

    class << self
      def define(source, filepath, options)
        self.new(filepath, options) do
          eval(source, binding, filepath)
        end
      end
    end

    attr_reader :result

    def initialize(filepath, options, &block)
      @filepath = filepath
      @result = OpenStruct.new(snss: Hashie::Mash.new)

      @context = Hashie::Mash.new(
        filepath: filepath,
        templates: {},
        options: options,
      )

      instance_eval(&block)
    end

    def require(file)
      albfile = (file =~ %r|\A/|) ? file : File.expand_path(File.join(File.dirname(@filepath), file))

      if File.exist?(albfile)
        instance_eval(File.read(albfile), albfile)
      elsif File.exist?("#{albfile}.rb")
        instance_eval(File.read("#{albfile}.rb"), "#{albfile}.rb")
      else
        Kernel.require(file)
      end
    end

    def template(name, &block)
      @context.templates[name.to_s] = block
    end

    def sns(region = nil, &block)
      current_region = @context[:region] = region || ENV['AWS_DEFAULT_REGION'] || ENV.fetch('AWS_REGION')
      current_region_sns = @result.snss[current_region]
      topics = current_region_sns ? current_region_sns.topics : []
      @result.snss[current_region] = SNS.new(@context, topics, &block).result
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
simnos-0.1.3.beta1 lib/simnos/dsl.rb
simnos-0.1.2 lib/simnos/dsl.rb
simnos-0.1.1 lib/simnos/dsl.rb
simnos-0.1.1.beta2 lib/simnos/dsl.rb
simnos-0.1.1.beta1 lib/simnos/dsl.rb
simnos-0.1.0 lib/simnos/dsl.rb
simnos-0.1.0.beta4 lib/simnos/dsl.rb
simnos-0.1.0.beta3 lib/simnos/dsl.rb