Class: Messaging::Adapters::Postgres::Categories

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/messaging/adapters/postgres/categories.rb

Instance Method Summary collapse

Instance Method Details

#[](name) ⇒ nil, Category

Get a category by name

Parameters:

  • name (String) —

    the name of the category

Returns:

  • (nil) —

    if no category exists with the given name

  • (Category)


18
19
20
21
22
23
# File 'lib/messaging/adapters/postgres/categories.rb', line 18

def [](name)
  category = all_categories.select { |c| c == name }
  return unless category

  Category.new(category)
end

#create(name) ⇒ Category

Creates a table partition for the given category

Parameters:

  • name (String) —

    the name of the category

Returns:



29
30
31
32
33
34
35
36
37
# File 'lib/messaging/adapters/postgres/categories.rb', line 29

def create(name)
  table_name = name.parameterize(separator: '_')
  sql = <<~SQL
    CREATE TABLE messaging.#{table_name}
    PARTITION OF messaging.messages FOR VALUES IN ('#{name}');
  SQL
  connection.execute sql
  Category.new(name)
end

#drop(name) ⇒ Object

Drops the table partition (including all messages) for the given category

Parameters:

  • name (String) —

    the name of the category



42
43
44
45
46
47
48
# File 'lib/messaging/adapters/postgres/categories.rb', line 42

def drop(name)
  table_name = name.parameterize(separator: '_')
  sql = <<~SQL
    drop TABLE messaging.#{table_name}
  SQL
  connection.execute sql
end

#each ⇒ Object



7
8
9
10
11
# File 'lib/messaging/adapters/postgres/categories.rb', line 7

def each
  all_categories.each do |name|
    yield Category.new(name)
  end
end