Class: Sc4ry::Backends::Redis

Inherits:
Object
  • Object
show all
Defined in:
lib/sc4ry/backends/redis.rb

Overview

Redis backend definition

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Sc4ry::Backends::Redis

Constructor

Parameters:

  • config (Hash)

    Config map



10
11
12
13
14
15
# File 'lib/sc4ry/backends/redis.rb', line 10

def initialize(config)
  @auth = config.slice(:auth)[:auth]
  @config = config.slice(:host, :port, :db)
  @be = ::Redis.new @config
  @be.auth(@auth) if @auth
end

Instance Method Details

#del(key:) ⇒ Boolean

delete a specific record

Parameters:

  • key (Symbol)

    the name of the record

Returns:

  • (Boolean)

    status of the operation



45
46
47
# File 'lib/sc4ry/backends/redis.rb', line 45

def del(key:)
  @be.del key
end

#exist?(key:) ⇒ Boolean

verifiy a specific record existence

Parameters:

  • key (Symbol)

    the name of the record

Returns:

  • (Boolean)

    presence of the record



58
59
60
# File 'lib/sc4ry/backends/redis.rb', line 58

def exist?(key:)
  !@be.get(key).nil?
end

#flushBoolean

flush all records in backend

Returns:

  • (Boolean)

    status of the operation



51
52
53
# File 'lib/sc4ry/backends/redis.rb', line 51

def flush
  @be.flushdb
end

#get(key:) ⇒ String

return value of queried record

Parameters:

  • key (Symbol)

    the name of the record

Returns:

  • (String)

    content value of record



26
27
28
29
30
# File 'lib/sc4ry/backends/redis.rb', line 26

def get(key:)
  res = YAML.load(@be.get(key))
  res[:exceptions].map! { |item| Object.const_get(item) if item.instance_of?(String) }
  res
end

#listArray

return the list of find records in backend for a specific pattern

Returns:

  • (Array)

    list of record (for all hostname if hostname is specified)



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

def list
  @be.keys('*').map(&:to_sym)
end

#put(key:, value:) ⇒ String

defined and store value for specified key

Parameters:

  • key (Symbol)

    :key the name of the record

  • value (Symbol)

    :value the content value of the record

Returns:

  • (String)

    content value of record



36
37
38
39
40
# File 'lib/sc4ry/backends/redis.rb', line 36

def put(key:, value:)
  data = value.dup
  data[:exceptions].map! { |item| item.name.to_s if item.instance_of?(Class) }
  @be.set key, data.to_yaml
end