Sha256: 445f60d04a3c0826f674becc0194f205f9024bb1c60815ba209adc875d249751

Contents?: true

Size: 1.4 KB

Versions: 17

Compression:

Stored size: 1.4 KB

Contents

class Gratan::DSL::Converter
  def self.convert(exported, options = {})
    self.new(exported, options).convert
  end

  def initialize(exported, options = {})
    @exported = exported
    @options = options
  end

  def convert
    @exported.map {|user_host, attrs|
      output_user(user_host, attrs)
    }.join("\n")
  end

  private

  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

  def output_user_options(options)
    if options.empty?
      ' '
    else
      options = strip_hash_brace(options.inspect)
      ", #{options} "
    end
  end

  def output_objects(objects)
    objects.map {|object, grant|
      options = output_object_options(grant)

      <<-EOS
  on #{object.inspect}#{options}do
    #{output_grant(grant)}
  end
      EOS
    }.join("\n").strip
  end

  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

  def output_grant(grant)
    grant[:privs].map {|priv|
      <<-EOS
    grant #{priv.inspect}
      EOS
    }.join.strip
  end

  def strip_hash_brace(hash_str)
    hash_str.sub(/\A\{/, '').sub(/\}\z/, '')
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
gratan-0.2.6 lib/gratan/dsl/converter.rb
gratan-0.2.5 lib/gratan/dsl/converter.rb
gratan-0.2.4 lib/gratan/dsl/converter.rb
gratan-0.2.3 lib/gratan/dsl/converter.rb
gratan-0.2.2 lib/gratan/dsl/converter.rb
gratan-0.2.1 lib/gratan/dsl/converter.rb
gratan-0.2.0 lib/gratan/dsl/converter.rb
gratan-0.1.9 lib/gratan/dsl/converter.rb
gratan-0.1.8 lib/gratan/dsl/converter.rb
gratan-0.1.7 lib/gratan/dsl/converter.rb
gratan-0.1.6 lib/gratan/dsl/converter.rb
gratan-0.1.5 lib/gratan/dsl/converter.rb
gratan-0.1.4 lib/gratan/dsl/converter.rb
gratan-0.1.3 lib/gratan/dsl/converter.rb
gratan-0.1.2 lib/gratan/dsl/converter.rb
gratan-0.1.1 lib/gratan/dsl/converter.rb
gratan-0.1.0 lib/gratan/dsl/converter.rb