class Bukelatta::DSL::Context

Public Class Methods

eval(dsl, path, options = {}) click to toggle source
# File lib/bukelatta/dsl/context.rb, line 4
def self.eval(dsl, path, options = {})
  self.new(path, options) {
    eval(dsl, binding, path)
  }
end
new(path, options = {}, &block) click to toggle source
# File lib/bukelatta/dsl/context.rb, line 15
def initialize(path, options = {}, &block)
  @path = path
  @options = options
  @result = {}

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

  instance_eval(&block)
end

Public Instance Methods

result() click to toggle source
# File lib/bukelatta/dsl/context.rb, line 10
def result
  expand_leaf_array!
  @result.sort_array!
end
template(name, &block) click to toggle source
# File lib/bukelatta/dsl/context.rb, line 29
def template(name, &block)
  @context.templates[name.to_s] = block
end

Private Instance Methods

bucket(name) { || ... } click to toggle source
# File lib/bukelatta/dsl/context.rb, line 47
def bucket(name)
  name = name.to_s

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

  @result[name] = yield
end
expand_leaf_array(obj) click to toggle source
# File lib/bukelatta/dsl/context.rb, line 61
def expand_leaf_array(obj)
  case obj
  when Array
    if obj[0].instance_of?(Array) || obj[0].instance_of?(Hash)
      return obj.map do |o|
        expand_leaf_array(o)
      end
    end
    # Leaf
    if obj.length == 1
      return obj[0]
    end
    return obj
  when Hash
    h = {}
    obj.each do |k, v|
      h[k] = expand_leaf_array(v)
    end
    return h
  end
  return obj
end
expand_leaf_array!() click to toggle source
# File lib/bukelatta/dsl/context.rb, line 57
def expand_leaf_array!
  @result = expand_leaf_array(@result)
end
require(file) click to toggle source
# File lib/bukelatta/dsl/context.rb, line 35
def require(file)
  policyfile = (file =~ %r|\A/|) ? file : File.expand_path(File.join(File.dirname(@path), file))

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