class Posgra::DSL::Roles

Public Class Methods

eval(dsl, path, options = {}) click to toggle source
# File lib/posgra/dsl/roles.rb, line 6
def self.eval(dsl, path, options = {})
  self.new(path, options) do
    eval(dsl, binding, path)
  end
end
new(path, options = {}, &block) click to toggle source
# File lib/posgra/dsl/roles.rb, line 37
def initialize(path, options = {}, &block)
  @path = path
  @options = options
  @result = {
    :users => [],
    :users_by_group => {},
  }

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

  instance_eval(&block)
end

Public Instance Methods

result() click to toggle source
# File lib/posgra/dsl/roles.rb, line 12
def result
  @result[:users].uniq!

  group_users = @result[:users_by_group].flat_map do |group, users|
    if users.empty?
      [group, nil]
    else
      users.map {|u| [group, u] }
    end
  end

  new_users_by_group = {}

  group_users.each do |group, user|
    next unless [group, user].any? {|i| not i.nil? and matched?(i, @options[:include_role], @options[:exclude_role]) }
    new_users_by_group[group] ||= []
    new_users_by_group[group] << user if user
  end

  new_users_by_group.values.each(&:uniq!)
  @result[:users_by_group] = new_users_by_group

  @result
end

Private Instance Methods

group(name, &block) click to toggle source
# File lib/posgra/dsl/roles.rb, line 80
def group(name, &block)
  name = name.to_s
  @result[:users_by_group][name] = Posgra::DSL::Roles::Group.new(@context, name, @options, &block).result
end
require(file) click to toggle source
# File lib/posgra/dsl/roles.rb, line 60
def require(file)
  pgrantfile = (file =~ %r|\A/|) ? file : File.expand_path(File.join(File.dirname(@path), file))

  if File.exist?(pgrantfile)
    instance_eval(File.read(pgrantfile), pgrantfile)
  elsif File.exist?(pgrantfile + '.rb')
    instance_eval(File.read(pgrantfile + '.rb'), pgrantfile + '.rb')
  else
    Kernel.require(file)
  end
end
template(name, &block) click to toggle source
# File lib/posgra/dsl/roles.rb, line 56
def template(name, &block)
  @context.templates[name.to_s] = block
end
user(name, &block) click to toggle source
# File lib/posgra/dsl/roles.rb, line 72
def user(name, &block)
  name = name.to_s

  if matched?(name, @options[:include_role], @options[:exclude_role])
    @result[:users] << name
  end
end