class Gratan::DSL::Converter

Public Class Methods

convert(exported, options = {}) click to toggle source
# File lib/gratan/dsl/converter.rb, line 2
def self.convert(exported, options = {})
  self.new(exported, options).convert
end
new(exported, options = {}) click to toggle source
# File lib/gratan/dsl/converter.rb, line 6
def initialize(exported, options = {})
  @exported = exported
  @options = options
end

Public Instance Methods

convert() click to toggle source
# File lib/gratan/dsl/converter.rb, line 11
def convert
  @exported.map {|user_host, attrs|
    output_user(user_host, attrs)
  }.join("\n")
end

Private Instance Methods

output_grant(grant) click to toggle source
# File lib/gratan/dsl/converter.rb, line 63
  def output_grant(grant)
    grant[:privs].sort.map {|priv|
      <<-EOS
    grant #{priv.inspect}
      EOS
    }.join.strip
  end
output_object_options(grant) click to toggle source
# File lib/gratan/dsl/converter.rb, line 52
def output_object_options(grant)
  with_option = grant.delete(:with)

  if with_option
    options = strip_hash_brace({:with => with_option}.inspect)
    ", #{options} "
  else
    ' '
  end
end
output_objects(objects) click to toggle source
# File lib/gratan/dsl/converter.rb, line 40
  def output_objects(objects)
    objects.sort_by {|k, v| k }.map {|object, grant|
      options = output_object_options(grant)

      <<-EOS
  on #{object.inspect}#{options}do
    #{output_grant(grant)}
  end
      EOS
    }.join("\n").strip
  end
output_user(user_host, attrs) click to toggle source
# File lib/gratan/dsl/converter.rb, line 19
  def output_user(user_host, attrs)
    user, host = user_host
    objects, options = attrs.values_at(:objects, :options)
    options = output_user_options(options)

    <<-EOS
user #{user.inspect}, #{host.inspect}#{options}do
  #{output_objects(objects)}
end
    EOS
  end
output_user_options(options) click to toggle source
# File lib/gratan/dsl/converter.rb, line 31
def output_user_options(options)
  if options.empty?
    ' '
  else
    options = strip_hash_brace(options.inspect)
    ", #{options} "
  end
end
strip_hash_brace(hash_str) click to toggle source
# File lib/gratan/dsl/converter.rb, line 71
def strip_hash_brace(hash_str)
  hash_str.sub(/\A\{/, '').sub(/\}\z/, '')
end