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)


82
83
84
# File 'lib/message_store/stream_name.rb', line 82

def self.category?(stream_name)
  !stream_name.include?(id_separator)
end

.category_type_separatorObject



13
14
15
# File 'lib/message_store/stream_name.rb', line 13

def self.category_type_separator
  ':'
end

.compound_id_separatorObject



9
10
11
# File 'lib/message_store/stream_name.rb', line 9

def self.compound_id_separator
  ID.compound_id_separator
end

.compound_type_separatorObject



17
18
19
# File 'lib/message_store/stream_name.rb', line 17

def self.compound_type_separator
  '+'
end

.get_cardinal_id(stream_name) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/message_store/stream_name.rb', line 70

def self.get_cardinal_id(stream_name)
  id = get_id(stream_name)

  return nil if id.nil?

  ID.get_cardinal_id(id)
end

.get_category(stream_name) ⇒ Object



78
79
80
# File 'lib/message_store/stream_name.rb', line 78

def self.get_category(stream_name)
  split(stream_name)[0]
end

.get_category_type(stream_name) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/message_store/stream_name.rb', line 86

def self.get_category_type(stream_name)
  return nil unless stream_name.include?(category_type_separator)

  category = get_category(stream_name)

  category.split(category_type_separator)[1]
end

.get_category_types(stream_name) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/message_store/stream_name.rb', line 98

def self.get_category_types(stream_name)
  type_list = get_type(stream_name)

  return [] if type_list.nil?

  type_list.split(compound_type_separator)
end

.get_entity_name(stream_name) ⇒ Object



110
111
112
# File 'lib/message_store/stream_name.rb', line 110

def self.get_entity_name(stream_name)
  get_category(stream_name).split(category_type_separator)[0]
end

.get_id(stream_name) ⇒ Object



58
59
60
# File 'lib/message_store/stream_name.rb', line 58

def self.get_id(stream_name)
  split(stream_name)[1]
end

.get_ids(stream_name) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/message_store/stream_name.rb', line 62

def self.get_ids(stream_name)
  ids = get_id(stream_name)

  return [] if ids.nil?

  ID.parse(ids)
end

.get_type(*args) ⇒ Object



94
95
96
# File 'lib/message_store/stream_name.rb', line 94

def self.get_type(*args)
  get_category_type(*args)
end

.get_types(*args) ⇒ Object



106
107
108
# File 'lib/message_store/stream_name.rb', line 106

def self.get_types(*args)
  get_category_types(*args)
end

.id_separatorObject



5
6
7
# File 'lib/message_store/stream_name.rb', line 5

def self.id_separator
  '-'
end

.split(stream_name) ⇒ Object



54
55
56
# File 'lib/message_store/stream_name.rb', line 54

def self.split(stream_name)
  stream_name.split(id_separator, 2)
end

.stream_name(category, stream_id = nil, cardinal_id: nil, id: nil, ids: nil, type: nil, types: nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/message_store/stream_name.rb', line 21

def self.stream_name(category, stream_id=nil, cardinal_id: nil, id: nil, ids: nil, type: nil, types: nil)
  if category == nil
    raise Error, "Category must not be omitted from stream name"
  end

  stream_name = category

  type_list = []
  type_list.concat(Array(type))
  type_list.concat(Array(types))

  type_part = type_list.join(compound_type_separator)

  if not type_part.empty?
    stream_name = "#{stream_name}#{category_type_separator}#{type_part}"
  end

  id_list = []
  id_list << cardinal_id if not cardinal_id.nil?

  id_list.concat(Array(stream_id))
  id_list.concat(Array(id))
  id_list.concat(Array(ids))

  id_part = nil
  if not id_list.empty?
    id_part = ID.compound_id(id_list)
    stream_name = "#{stream_name}#{id_separator}#{id_part}"
  end

  stream_name
end