Module: MessageStore::StreamName

Defined in:
lib/message_store/stream_name.rb

Constant Summary collapse

Error =
Class.new(RuntimeError)

Class Method Summary collapse

Class Method Details

.category?(stream_name) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/message_store/stream_name.rb', line 32

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

.get_category(stream_name) ⇒ Object



28
29
30
# File 'lib/message_store/stream_name.rb', line 28

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

.get_entity_name(stream_name) ⇒ Object



52
53
54
# File 'lib/message_store/stream_name.rb', line 52

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

.get_id(stream_name) ⇒ Object



23
24
25
26
# File 'lib/message_store/stream_name.rb', line 23

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

.get_type_list(stream_name) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/message_store/stream_name.rb', line 36

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



44
45
46
47
48
49
50
# File 'lib/message_store/stream_name.rb', line 44

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



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/message_store/stream_name.rb', line 5

def self.stream_name(category_name, id=nil, type: nil, types: nil)
  if category_name == nil
    raise Error, "Category name must not be omitted"
  end

  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