Class: Rapid::Services::Set

Inherits:
Service
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/ury_rapid/services/set.rb

Overview

A set of Rapid services

A service set holds a set of configured Rapid services, as well as information about which services are enabled at launch-time.

Direct Known Subclasses

Root

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*_) ⇒ Set

Initialises a Set

Examples:

Create a new Set.

set = Set.new(view)

18
19
20
21
22
23
# File 'lib/ury_rapid/services/set.rb', line 18

def initialize(*_)
  super

  @services         = {}
  @enabled_services = ::Set[]
end

Instance Attribute Details

#constructor_arguments=(value) ⇒ Object (writeonly)

Sets the attribute constructor_arguments


144
145
146
# File 'lib/ury_rapid/services/set.rb', line 144

def constructor_arguments=(value)
  @constructor_arguments = value
end

#model_builder=(value) ⇒ Object (writeonly)

Sets the attribute model_builder


145
146
147
# File 'lib/ury_rapid/services/set.rb', line 145

def model_builder=(value)
  @model_builder = value
end

Instance Method Details

#configure(name, implementation_class, &block) ⇒ void

This method returns an undefined value.

Adds a service and its configuration to this set

Examples:

Configure a service

set.configure(:a_service_name, Service::Class::Here) do
  # Service DSL goes here
end

60
61
62
# File 'lib/ury_rapid/services/set.rb', line 60

def configure(name, implementation_class, &block)
  @services[name] = [implementation_class, block]
end

#disabledArray

Lists the disabled services

Examples:

List the disabled services

# Assuming :a, :b, and :c are disabled
set.disable
#=> [:a, :b, :c]

140
141
142
# File 'lib/ury_rapid/services/set.rb', line 140

def disabled
  @services.each_key.reject { |n| @enabled_services.include?(n) }.to_a
end

#enable(name) ⇒ void

This method returns an undefined value.

Enables a configured service at load-time

Examples:

Enable a service

set.enable(:a_service_name)

74
75
76
77
78
79
80
81
# File 'lib/ury_rapid/services/set.rb', line 74

def enable(name)
  fail(
    Rapid::Common::Exceptions::BadConfig,
    "Tried to enable non-configured service #{name}'."
  ) unless @services.key?(name)

  @enabled_services << name
end

#enable_allvoid

This method returns an undefined value.

Enables all previously configured services

Examples:

Enable a service

set.enable(:a_service_name)

90
91
92
# File 'lib/ury_rapid/services/set.rb', line 90

def enable_all
  @services.each_key(&method(:enable))
end

#group(name, &block) ⇒ void

This method returns an undefined value.

Creates a sub-group of this service set

Examples:

Create a sub-group named :my_group with a service configured

set.group(:my_group) do
  set.configure(:a_service_name, Service::Class::Here) do
    # Service DSL goes here
  end
end

39
40
41
42
43
# File 'lib/ury_rapid/services/set.rb', line 39

def group(name, &block)
  configure(name, Set) do
    instance_eval(&block)
  end
end

#runvoid

This method returns an undefined value.

Runs the services set.

Running the service set entails starting all of the service set's enabled services.

Examples:

Runs a service set.

set.run

157
158
159
# File 'lib/ury_rapid/services/set.rb', line 157

def run
  start_enabled
end

#start(name) ⇒ void

This method returns an undefined value.

Starts a specific service

This should be performed in the EventMachine run loop, as certain services will spin up EventMachine tasks here.

Examples:

Start the service :foo

set.start(:foo)

107
108
109
110
111
112
113
114
# File 'lib/ury_rapid/services/set.rb', line 107

def start(name)
  service_class, service_config = @services.fetch(name)
  environment.insert_components('/') { tree(name, :sub_root) }
  service_view = environment.with_local_root(environment.find("/#{name}"))
  mod = service_class.new(service_view)
  mod.instance_eval(&service_config)
  mod.run
end

#start_enabledvoid

This method returns an undefined value.

Starts all enabled services

Examples:

Start all enabled services

set.start_enabled

123
124
125
# File 'lib/ury_rapid/services/set.rb', line 123

def start_enabled
  @enabled_services.each(&method(:start))
end