class Posgra::DSL::Converter

Public Class Methods

convert_databases(exported, options = {}) click to toggle source
# File lib/posgra/dsl/converter.rb, line 10
def self.convert_databases(exported, options = {})
  self.new(exported, options).convert_databases
end
convert_grants(exported, options = {}) click to toggle source
# File lib/posgra/dsl/converter.rb, line 6
def self.convert_grants(exported, options = {})
  self.new(exported, options).convert_grants
end
convert_roles(exported, options = {}) click to toggle source
# File lib/posgra/dsl/converter.rb, line 2
def self.convert_roles(exported, options = {})
  self.new(exported, options).convert_roles
end
new(exported, options = {}) click to toggle source
# File lib/posgra/dsl/converter.rb, line 14
def initialize(exported, options = {})
  @exported = exported
  @options = options
end

Public Instance Methods

convert_databases() click to toggle source
# File lib/posgra/dsl/converter.rb, line 34
def convert_databases
  database_grants_by_role = @exported || {}
  output_database_roles(database_grants_by_role).strip
end
convert_grants() click to toggle source
# File lib/posgra/dsl/converter.rb, line 29
def convert_grants
  grants_by_role = @exported || {}
  output_roles(grants_by_role).strip
end
convert_roles() click to toggle source
# File lib/posgra/dsl/converter.rb, line 19
def convert_roles
  users_by_group = @exported[:users_by_group] || {}
  users = @exported.fetch(:users, []) - users_by_group.values.flatten

  [
    output_users(users),
    output_groups(users_by_group),
  ].join("\n").strip
end

Private Instance Methods

output_database(database, grants) click to toggle source
# File lib/posgra/dsl/converter.rb, line 172
  def output_database(database, grants)
    if grants.empty?
      grants = "# no grants"
    else
      grants = output_grants(grants, '    ')
    end

    <<-EOS
  database #{database.inspect} do
    #{grants}
  end
    EOS
  end
output_database_role(role, grants_by_database) click to toggle source
# File lib/posgra/dsl/converter.rb, line 152
  def output_database_role(role, grants_by_database)
    if grants_by_database.empty?
      databases = "# no databases"
    else
      databases = output_databases(grants_by_database)
    end

    <<-EOS
role #{role.inspect} do
  #{databases}
end
    EOS
  end
output_database_roles(database_grants_by_role) click to toggle source
# File lib/posgra/dsl/converter.rb, line 146
def output_database_roles(database_grants_by_role)
  database_grants_by_role.sort_by {|r, _| r }.map {|role, grants_by_database|
    output_database_role(role, grants_by_database)
  }.join("\n")
end
output_databases(grants_by_database) click to toggle source
# File lib/posgra/dsl/converter.rb, line 166
def output_databases(grants_by_database)
  grants_by_database.sort_by {|s, _| s }.map {|database, grants|
    output_database(database, grants).strip
  }.join("\n  ")
end
output_grant(privilege_type, options) click to toggle source
# File lib/posgra/dsl/converter.rb, line 135
def output_grant(privilege_type, options)
  is_grantable = options.fetch('is_grantable')
  out = "grant #{privilege_type.inspect}"

  if is_grantable
    out << ", :grantable => #{is_grantable}"
  end

  out
end
output_grants(grants, indent = " ") click to toggle source
# File lib/posgra/dsl/converter.rb, line 129
def output_grants(grants, indent = "      ")
  grants.sort_by {|g| g.to_s }.map {|privilege_type, options|
    output_grant(privilege_type, options).strip
  }.join("\n#{indent}")
end
output_group(group, users) click to toggle source
# File lib/posgra/dsl/converter.rb, line 53
  def output_group(group, users)
    if users.empty?
      users = "# no users"
    else
      users = users.sort.map {|user|
        "user #{user.inspect}"
      }.join("\n  ")
    end

    <<-EOS
group #{group.inspect} do
  #{users}
end
    EOS
  end
output_groups(users_by_group) click to toggle source
# File lib/posgra/dsl/converter.rb, line 47
def output_groups(users_by_group)
  users_by_group.sort_by {|g, _| g }.map {|group, users|
    output_group(group, users)
  }.join("\n")
end
output_object(object, grants) click to toggle source
# File lib/posgra/dsl/converter.rb, line 115
  def output_object(object, grants)
    if grants.empty?
      grants = "# no grants"
    else
      grants = output_grants(grants)
    end

    <<-EOS
    on #{object.inspect} do
      #{grants}
    end
    EOS
  end
output_objects(grants_by_object) click to toggle source
# File lib/posgra/dsl/converter.rb, line 109
def output_objects(grants_by_object)
  grants_by_object.sort_by {|o, _| o }.map {|object, grants|
    output_object(object, grants).strip
  }.join("\n    ")
end
output_role(role, grants_by_schema) click to toggle source
# File lib/posgra/dsl/converter.rb, line 75
  def output_role(role, grants_by_schema)
    if grants_by_schema.empty?
      schemas = "# no schemas"
    else
      schemas = output_schemas(grants_by_schema)
    end

    <<-EOS
role #{role.inspect} do
  #{schemas}
end
    EOS
  end
output_roles(grants_by_role) click to toggle source
# File lib/posgra/dsl/converter.rb, line 69
def output_roles(grants_by_role)
  grants_by_role.sort_by {|r, _| r }.map {|role, grants_by_schema|
    output_role(role, grants_by_schema)
  }.join("\n")
end
output_schema(schema, grants_by_object) click to toggle source
# File lib/posgra/dsl/converter.rb, line 95
  def output_schema(schema, grants_by_object)
    if grants_by_object.empty?
      objects = "# no objects"
    else
      objects = output_objects(grants_by_object)
    end

    <<-EOS
  schema #{schema.inspect} do
    #{objects}
  end
    EOS
  end
output_schemas(grants_by_schema) click to toggle source
# File lib/posgra/dsl/converter.rb, line 89
def output_schemas(grants_by_schema)
  grants_by_schema.sort_by {|s, _| s }.map {|schema, grants_by_object|
    output_schema(schema, grants_by_object).strip
  }.join("\n  ")
end
output_users(users) click to toggle source
# File lib/posgra/dsl/converter.rb, line 41
def output_users(users)
  users.sort.map {|user|
    "user #{user.inspect}"
  }.join("\n") + "\n"
end