Sha256: b4c3c376f26ecb5a3b5fd778d5577157e60a4ed272ca82ee5a3a51a5c82bd0cf

Contents?: true

Size: 1.32 KB

Versions: 4

Compression:

Stored size: 1.32 KB

Contents

class Gratan::DSL::Context
  include Gratan::DSL::Validator
  include Gratan::Logger::Helper

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

  attr_reader :result

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

  private

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

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

  def user(name, host, options = {}, &block)
    name = name.to_s
    host = host.to_s
    options ||= {}

    __validate("User `#{name}@#{host}` is already defined") do
      not @result.has_key?([name, host])
    end

    if @options[:enable_expired] and (expired = options.delete(:expired))
      expired = Time.parse(expired)

      if Time.new >= expired
        log(:warn, "User `#{name}@#{host}` has expired", :yellow)
        return
      end
    end

    @result[[name, host]] = {
      :objects => Gratan::DSL::Context::User.new(name, host, &block).result,
      :options => options,
    }
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
gratan-0.1.3 lib/gratan/dsl/context.rb
gratan-0.1.2 lib/gratan/dsl/context.rb
gratan-0.1.1 lib/gratan/dsl/context.rb
gratan-0.1.0 lib/gratan/dsl/context.rb