Class: CTioga2::Data::Backends::BackendFactory

Inherits:
Object
  • Object
show all
Includes:
Log
Defined in:
lib/ctioga2/data/backends/factory.rb

Overview

This class holds an instance of all the different Backend available, and features a ‘current backend’.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Log

context, debug, error, fatal, #format_exception, #identify, info, init_logger, log_to, logger, set_level, #spawn, warn

Constructor Details

#initialize(default) ⇒ BackendFactory

Creates a new BackendFactory



41
42
43
44
45
46
47
48
49
50
# File 'lib/ctioga2/data/backends/factory.rb', line 41

def initialize(default)
  @backends = {}
  @backend_descriptions = Backend.list_backends
  for backend in @backend_descriptions.keys
    reset_backend(backend)
    # Add commands
    @backend_descriptions[backend].create_backend_commands
  end
  @current = @backends[default]
end

Instance Attribute Details

#backendsObject

A hash name (as in Description#name) -> Backend



35
36
37
# File 'lib/ctioga2/data/backends/factory.rb', line 35

def backends
  @backends
end

#currentObject

The current Backend



38
39
40
# File 'lib/ctioga2/data/backends/factory.rb', line 38

def current
  @current
end

Instance Method Details

#reset_backend(backend) ⇒ Object

Resets the given backend to its default values.



53
54
55
# File 'lib/ctioga2/data/backends/factory.rb', line 53

def reset_backend(backend)
  @backends[backend] = @backend_descriptions[backend].instantiate
end

#set_backend_parameter_value(backend, param, value) ⇒ Object

Sets the (raw) value of the parameter of the given backend



79
80
81
82
83
# File 'lib/ctioga2/data/backends/factory.rb', line 79

def set_backend_parameter_value(backend, param, value)
  b = @backends[backend]
  # This is way too complicated !
  b.description.param_hash[param].set_value(b, value)
end

#set_current_backend(backend) ⇒ Object

Selects the current backend



58
59
60
# File 'lib/ctioga2/data/backends/factory.rb', line 58

def set_current_backend(backend)
  @current = @backends[backend]
end

#specified_backend(options = {}) ⇒ Object

Returns the backend named in the ‘as’ key of options, or the current backend if there isn’t



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ctioga2/data/backends/factory.rb', line 64

def specified_backend(options = {})
  if options.key? 'as'
    k = options['as']
    if @backends.key? k
      return @backends[k]
    else
      error { "No such backend: #{k}, ignoring" }
    end
  else
    return @current
  end
end