Class: CommandTower::Messaging::NotificationTypes::Registry

Inherits:
Object
  • Object
show all
Defined in:
app/services/command_tower/messaging/notification_types/registry.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry.



15
16
17
18
19
# File 'app/services/command_tower/messaging/notification_types/registry.rb', line 15

def initialize
  @declarations = {}
  @sealed = false
  @mutex = Mutex.new
end

Class Method Details

.instanceObject



7
8
9
# File 'app/services/command_tower/messaging/notification_types/registry.rb', line 7

def self.instance
  @instance ||= new
end

.reset!Object



11
12
13
# File 'app/services/command_tower/messaging/notification_types/registry.rb', line 11

def self.reset!
  @instance = new
end

Instance Method Details

#catalogObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/services/command_tower/messaging/notification_types/registry.rb', line 67

def catalog
  groups = @declarations.values.group_by(&:category_key)
  categories = groups.map do |category_key, declarations|
    sample = declarations.first
    ordered = declarations.sort_by { |declaration| type_sort_key(declaration) }
    CatalogCategory.build(
      key: category_key,
      label: sample.category_label,
      order: sample.category_order,
      declarations: ordered,
    )
  end

  categories.sort_by! { |category| [category.order, category.key] }
  categories.freeze
end

#enumerateObject



63
64
65
# File 'app/services/command_tower/messaging/notification_types/registry.rb', line 63

def enumerate
  @declarations.values.freeze
end

#lookup(key) ⇒ Object

Raises:



52
53
54
55
56
57
# File 'app/services/command_tower/messaging/notification_types/registry.rb', line 52

def lookup(key)
  declaration = @declarations[key.to_s]
  raise NotFoundError, "notification type not registered: #{key}" if declaration.nil?

  declaration
end

#register(declaration) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/services/command_tower/messaging/notification_types/registry.rb', line 21

def register(declaration)
  @mutex.synchronize do
    raise SealedRegistryError, "notification type registry is sealed" if @sealed

    DeclarationValidator.validate!(declaration)
    DeclarationValidator.validate_category_consistency!(declaration, peers: @declarations.values)

    key = declaration.key
    if @declarations.key?(key)
      raise DuplicateTypeError, "notification type already registered: #{key}"
    end

    @declarations[key] = declaration
    declaration
  end
end

#registered?(key) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'app/services/command_tower/messaging/notification_types/registry.rb', line 59

def registered?(key)
  @declarations.key?(key.to_s)
end

#sealObject



38
39
40
41
42
43
44
45
46
# File 'app/services/command_tower/messaging/notification_types/registry.rb', line 38

def seal
  @mutex.synchronize do
    validate_sealed_catalog!

    @sealed = true
    @declarations = @declarations.dup.freeze
    true
  end
end

#sealed?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'app/services/command_tower/messaging/notification_types/registry.rb', line 48

def sealed?
  @sealed
end