Module: EventSource::Postgres::StreamName

Defined in:
lib/event_source/postgres/stream_name.rb

Class Method Summary collapse

Class Method Details

.category?(stream_name) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/event_source/postgres/stream_name.rb', line 27

def self.category?(stream_name)
  !stream_name.include?('-')
end

.get_category(stream_name) ⇒ Object



23
24
25
# File 'lib/event_source/postgres/stream_name.rb', line 23

def self.get_category(stream_name)
  stream_name.split('-').first
end

.get_entity_name(stream_name) ⇒ Object



47
48
49
# File 'lib/event_source/postgres/stream_name.rb', line 47

def self.get_entity_name(stream_name)
  get_category(stream_name).split(':').first
end

.get_id(stream_name) ⇒ Object



18
19
20
21
# File 'lib/event_source/postgres/stream_name.rb', line 18

def self.get_id(stream_name)
  id = stream_name.partition('-').last
  id.empty? ? nil : id
end

.get_type_list(stream_name) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/event_source/postgres/stream_name.rb', line 31

def self.get_type_list(stream_name)
  type = stream_name.split(':').last.split('-').first

  return nil if stream_name.start_with?(type)

  type
end

.get_types(stream_name) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/event_source/postgres/stream_name.rb', line 39

def self.get_types(stream_name)
  type_list = get_type_list(stream_name)

  return [] if type_list.nil?

  type_list.split('+')
end

.stream_name(category_name, id = nil, type: nil, types: nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/event_source/postgres/stream_name.rb', line 4

def self.stream_name(category_name, id=nil, type: nil, types: nil)
  types = Array(types)
  types.unshift(type) unless type.nil?

  type_list = nil
  type_list = types.join('+') unless types.empty?

  stream_name = category_name
  stream_name = "#{stream_name}:#{type_list}" unless type_list.nil?
  stream_name = "#{stream_name}-#{id}" unless id.nil?

  stream_name
end