Class: ForemanDeployments::Registry

Inherits:
Object
  • Object
show all
Defined in:
app/lib/foreman_deployments/registry.rb

Defined Under Namespace

Classes: TypeException

Constant Summary collapse

ALLOWED_TYPES =
{
  'task' => 'ForemanDeployments::Tasks::BaseDefinition',
  'input' => 'ForemanDeployments::Inputs::BaseInputDefinition'
}

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry.



10
11
12
# File 'app/lib/foreman_deployments/registry.rb', line 10

def initialize
  clear!
end

Instance Method Details

#available(type) ⇒ Object



18
19
20
# File 'app/lib/foreman_deployments/registry.rb', line 18

def available(type)
  @available[type].clone
end

#available_inputsObject



45
46
47
# File 'app/lib/foreman_deployments/registry.rb', line 45

def available_inputs
  available('input')
end

#available_tasksObject



37
38
39
# File 'app/lib/foreman_deployments/registry.rb', line 37

def available_tasks
  available('task')
end

#clear!Object



14
15
16
# File 'app/lib/foreman_deployments/registry.rb', line 14

def clear!
  @available = Hash[ALLOWED_TYPES.keys.map { |k| [k, {}] }]
end

#register(type, registered_class) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/lib/foreman_deployments/registry.rb', line 22

def register(type, registered_class)
  type = type.to_s
  unless type_valid?(type)
    fail(TypeException, "Type needs to be one of: #{ALLOWED_TYPES.keys.join(', ')}")
  end
  unless class_valid?(type, registered_class)
    fail(TypeException, "Registered class need to be descendant of #{ALLOWED_TYPES[type]}")
  end
  task_name = registered_class.tag_name.to_s
  unless name_valid?(task_name)
    fail(TypeException, "Invalid name #{task_name}")
  end
  @available[type][task_name.to_s] = registered_class.name
end

#register_input(registered_class) ⇒ Object



49
50
51
# File 'app/lib/foreman_deployments/registry.rb', line 49

def register_input(registered_class)
  register('input', registered_class)
end

#register_task(registered_class) ⇒ Object



41
42
43
# File 'app/lib/foreman_deployments/registry.rb', line 41

def register_task(registered_class)
  register('task', registered_class)
end