Sha256: 50df1725198faffa5b7a13600a19166e90aaa898a7a919262feb6f0245491f54

Contents?: true

Size: 1.43 KB

Versions: 17

Compression:

Stored size: 1.43 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.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

  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].sort.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.3.2 lib/gratan/dsl/converter.rb
gratan-0.3.1 lib/gratan/dsl/converter.rb
gratan-0.3.1.beta4 lib/gratan/dsl/converter.rb
gratan-0.3.1.beta3 lib/gratan/dsl/converter.rb
gratan-0.3.1.beta2 lib/gratan/dsl/converter.rb
gratan-0.3.1.beta lib/gratan/dsl/converter.rb
gratan-0.3.0 lib/gratan/dsl/converter.rb
gratan-0.3.0.beta lib/gratan/dsl/converter.rb
gratan-0.2.9 lib/gratan/dsl/converter.rb
gratan-0.2.9.beta3 lib/gratan/dsl/converter.rb
gratan-0.2.9.beta2 lib/gratan/dsl/converter.rb
gratan-0.2.9.beta lib/gratan/dsl/converter.rb
gratan-0.2.8 lib/gratan/dsl/converter.rb
gratan-0.2.8.beta3 lib/gratan/dsl/converter.rb
gratan-0.2.8.beta2 lib/gratan/dsl/converter.rb
gratan-0.2.8.beta lib/gratan/dsl/converter.rb
gratan-0.2.7 lib/gratan/dsl/converter.rb