Class: Sc4ry::Store

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Singleton
Defined in:
lib/sc4ry/store.rb

Overview

Note:

must be accessed by Circuits.store

Sc4ry::Store class Store Class Provider/manager singleton Forwarder on Backends::Memory or Backends::Redis

Constant Summary collapse

@@current =
:memory
@@backends =
{ memory: { class: Sc4ry::Backends::Memory, config: {} },
redis: { class: Sc4ry::Backends::Redis, config: { host: 'localhost', port: 6379, db: 1 } } }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStore

constructor pointing on :memory backend



24
25
26
# File 'lib/sc4ry/store.rb', line 24

def initialize
  change_backend name: @@current
end

Instance Attribute Details

#beObject (readonly)

accessor on current backend (default :memory)



19
20
21
# File 'lib/sc4ry/store.rb', line 19

def be
  @be
end

Instance Method Details

#change_backend(name:) ⇒ Symbol

Note:

if changing form :memory to :redis => all values and result are lost and circuits will be lost

Note:

if changing to :redis, get all the define circuits with values and status (ideal)

Note:

for distributed worker/instance/runner/services

change the current backend

Parameters:

  • name (Symbol)

    the name of the target backend

Returns:

  • (Symbol)

    the name of the new current backend

Raises:

  • Sc4ry::Exceptions::Sc4ryBackendError if backend is not found



66
67
68
69
70
71
72
# File 'lib/sc4ry/store.rb', line 66

def change_backend(name:)
  raise Sc4ry::Exceptions::Sc4ryBackendError, "backend #{name} not found" unless @@backends.include? name

  @@current =  name
  @be = @@backends[@@current][:class].new(@@backends[@@current][:config])
  name
end

#config_backend(name:, config:) ⇒ Object

change the specified backend config

Parameters:

  • name (Symbol)

    the name of the target backend

  • config (Hash)

    the config of the specified backend

Raises:

  • Sc4ry::Exceptions::Sc4ryBackendError if backend is not found, or name == :memory



104
105
106
107
108
109
# File 'lib/sc4ry/store.rb', line 104

def config_backend(name:, config:)
  raise Sc4ry::Exceptions::Sc4ryBackendError, "backend #{name} not found" unless @@backends.include? name
  raise Sc4ry::Exceptions::Sc4ryBackendError, 'backend :memory not need config' if name == :memory

  @@backends[name][:config] = config
end

#currentObject

return the current backend

Examples:

usage

include Sc4ry
puts Circuits.store.current

Returns:



33
34
35
# File 'lib/sc4ry/store.rb', line 33

def current
  @@current
end

#delete_backend(name:) ⇒ Boolean

delete the specified backend reference

Parameters:

  • name (Symbol)

    the name of the target backend

Returns:

  • (Boolean)

Raises:

  • Sc4ry::Exceptions::Sc4ryBackendError if backend is not found, or name == :memory or :redis



91
92
93
94
95
96
97
98
# File 'lib/sc4ry/store.rb', line 91

def delete_backend(name:)
  forbidden_mes = 'Delete forbidden for backend in [:redis,:memory]'
  notfound_mes = "backend #{name} not found"
  raise Sc4ry::Exceptions::Sc4ryBackendError, forbidden_mes if %i[memory redis].include? name
  raise Sc4ry::Exceptions::Sc4ryBackendError, notfound_mes  unless @@backends.include? name

  @@backends.delete(name)
end

#get_config(backend:) ⇒ Hash

return the config of a specific backend

Examples:

usage

include Sc4ry
puts Circuits.store.get_config backend: :redis

Parameters:

  • backend (Symbol)

    the name the backend

Returns:

  • (Hash)

    the config of the backend

Raises:

  • Sc4ry::Exceptions::Sc4ryBackendError if backend is not found



44
45
46
47
48
# File 'lib/sc4ry/store.rb', line 44

def get_config(backend:)
  raise Sc4ry::Exceptions::Sc4ryBackendError, "backend #{backend} not found" unless @@backends.include? backend

  @@backends[backend][:config]
end

#list_backendArray

list backend available

Examples:

usage

include Sc4ry
puts Circuits.store.list_backend

Returns:

  • (Array)

    of Symbol the list of backend name



55
56
57
# File 'lib/sc4ry/store.rb', line 55

def list_backend
  @@backends.keys
end

#register_backend(name:, backend_class:, config: {}) ⇒ Symbol

register a new backend

Parameters:

  • name (Symbol)

    the name of the backend

  • config (Hash) (defaults to: {})

    the config for this backend

  • backend_class (Class)

    the class name of the new backend

Returns:

  • (Symbol)

    the name of the backend

Raises:

  • Sc4ry::Exceptions::Sc4ryBackendError if backend already exist



80
81
82
83
84
85
# File 'lib/sc4ry/store.rb', line 80

def register_backend(name:, backend_class:, config: {})
  raise Sc4ry::Exceptions::Sc4ryBackendError, "backend #{name} already exist" if @@backends.include? name

  @@backends[name] = { config: config, class: backend_class }
  name
end