Class: MotherBrain::Gear::Service

Inherits:
Base
  • Object
show all
Defined in:
lib/mb/gears/service.rb,
lib/mb/gears/service/action.rb,
lib/mb/gears/service/action_runner.rb

Defined Under Namespace

Classes: Action, ActionRunner, CleanRoom

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

register_gear, #run

Constructor Details

#initialize(component, name, &block) ⇒ Service

Returns a new instance of Service.

Parameters:



33
34
35
36
37
38
39
40
41
# File 'lib/mb/gears/service.rb', line 33

def initialize(component, name, &block)
  @name      = name.to_s
  @component = component
  @actions   = Set.new

  if block_given?
    dsl_eval(&block)
  end
end

Instance Attribute Details

#actionsSet<Action> (readonly)

Returns:



23
24
25
# File 'lib/mb/gears/service.rb', line 23

def actions
  @actions
end

#componentMB::Component (readonly)

Returns:



25
26
27
# File 'lib/mb/gears/service.rb', line 25

def component
  @component
end

#nameString (readonly)

Returns:

  • (String)


21
22
23
# File 'lib/mb/gears/service.rb', line 21

def name
  @name
end

#service_attributeObject (readonly)

Returns the value of attribute service_attribute.



29
30
31
# File 'lib/mb/gears/service.rb', line 29

def service_attribute
  @service_attribute
end

#service_groupObject (readonly)

Returns the value of attribute service_group.



27
28
29
# File 'lib/mb/gears/service.rb', line 27

def service_group
  @service_group
end

#service_recipeObject (readonly)

Returns the value of attribute service_recipe.



28
29
30
# File 'lib/mb/gears/service.rb', line 28

def service_recipe
  @service_recipe
end

Class Method Details

.find(gears, name) ⇒ MB::Gear::Service

Finds a gear identified by name in the list of gears supplied.

Parameters:

  • gears (Array<MB::Gear::Service>)

    the list of gears to search in

  • name (#to_s)

    the name of the gear to search for

Returns:

  • (MB::Gear::Service)


15
16
17
# File 'lib/mb/gears/service.rb', line 15

def find(gears, name)
  gears.find { |obj| obj.name == name.to_s }
end

Instance Method Details

#action(name) ⇒ Gear::Action

Find and return the given action

Parameters:

  • name (String)

Returns:

  • (Gear::Action)

Raises:

  • (ActionNotFound)

    if there is no action of the given name defined



67
68
69
# File 'lib/mb/gears/service.rb', line 67

def action(name)
  actions.find { |action| action.name == name }
end

#action!(name) ⇒ Gear::Action

Find and return the given action

Parameters:

  • name (String)

Returns:

  • (Gear::Action)

Raises:

  • (ActionNotFound)

    if there is no action of the given name defined



50
51
52
53
54
55
56
57
58
# File 'lib/mb/gears/service.rb', line 50

def action!(name)
  action = action(name)

  unless action
    raise ActionNotFound, "#{self.class.keyword} '#{_attributes_[:name]}' does not have the action '#{name}'"
  end

  action
end

#add_action(new_action) ⇒ Set<Action>

Add a new action to this Service

Parameters:

Returns:



83
84
85
86
87
88
89
# File 'lib/mb/gears/service.rb', line 83

def add_action(new_action)
  if action(new_action.name)
    raise DuplicateAction, "Action '#{new_action.name}' already defined on service '#{_attributes_[:name]}'"
  end

  actions << new_action
end

#dynamic_service?Boolean

Indicates whether this service conforms to the dyanamic service pattern

Returns:

  • (Boolean)

    TrueClass, FalseClass



117
118
119
# File 'lib/mb/gears/service.rb', line 117

def dynamic_service?
  missing_fields_for_dynamic_service.empty?
end

#set_service_attribute(attribute) ⇒ Object



99
100
101
# File 'lib/mb/gears/service.rb', line 99

def set_service_attribute(attribute)
  @service_attribute = attribute
end

#set_service_group(group) ⇒ Object



91
92
93
# File 'lib/mb/gears/service.rb', line 91

def set_service_group(group)
  @service_group = group
end

#set_service_recipe(recipe) ⇒ Object



95
96
97
# File 'lib/mb/gears/service.rb', line 95

def set_service_recipe(recipe)
  @service_recipe = recipe
end

#stop_actionGear::Action

Find and return the stop action

Returns:

  • (Gear::Action)


74
75
76
# File 'lib/mb/gears/service.rb', line 74

def stop_action
  action(:stop)
end

#to_dynamic_serviceMB::Gears::DynamicService

Convert a Service to a DynamicService

Returns:

  • (MB::Gears::DynamicService)


106
107
108
109
110
111
112
# File 'lib/mb/gears/service.rb', line 106

def to_dynamic_service
  log {
    "Service '#{self.name}' does not appear to by a dynamic service. It does not define the following fields which are required for dynamic services: #{self.missing_fields_for_dynamic_service.join(",")}"
  } unless dynamic_service?

  MotherBrain::Gear::DynamicService.new(component.name, name)
end