Sha256: 9032f9e88bf627a49ecc4d38e408be09b927960cc1022799bb7477fcf8df2841

Contents?: true

Size: 1.4 KB

Versions: 8

Compression:

Stored size: 1.4 KB

Contents

class Barkdog::DSL::Context
  include Barkdog::TemplateHelper

  def self.eval(dsl, path, options = {})
    self.new(path, options) {
      eval(dsl, binding, path)
    }
  end

  attr_reader :result

  def initialize(path, options = {}, &block)
    @path = path
    @options = options
    @result = {}

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

    instance_eval(&block)
  end

  private

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

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

    if File.exist?(barkfile)
      instance_eval(File.read(barkfile), barkfile)
    elsif File.exist?(barkfile + '.rb')
      instance_eval(File.read(barkfile + '.rb'), barkfile + '.rb')
    else
      Kernel.require(file)
    end
  end

  def monitor(name, fixed_options = {}, &block)
    name = name.to_s

    if @result[name]
      raise "Monitor `#{name}` is already defined"
    end

    fixed_options.keys.each do |key|
      unless Barkdog::FIXED_OPTION_KEYS.include?(key.to_s)
        raise "Monitor `#{name}`: `#{key}` is invalid fixed option key"
      end
    end

    fixed_options = Hash[fixed_options.map {|k, v| [k.to_s, v] }]
    attrs = Barkdog::DSL::Context::Monitor.new(@context, name, &block).result
    @result[name] = fixed_options.merge(attrs)
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
barkdog-0.1.9 lib/barkdog/dsl/context.rb
barkdog-0.1.8 lib/barkdog/dsl/context.rb
barkdog-0.1.7 lib/barkdog/dsl/context.rb
barkdog-0.1.6 lib/barkdog/dsl/context.rb
barkdog-0.1.5 lib/barkdog/dsl/context.rb
barkdog-0.1.4 lib/barkdog/dsl/context.rb
barkdog-0.1.3 lib/barkdog/dsl/context.rb
barkdog-0.1.3.beta lib/barkdog/dsl/context.rb