Class: GCoder::Persistence::DataStore

Inherits:
Object
  • Object
show all
Defined in:
lib/gcoder/persistence.rb

Direct Known Subclasses

Resolver

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ DataStore

Returns a new instance of DataStore.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/gcoder/persistence.rb', line 7

def initialize(options = {})
  @config = Config.merge(options)
  unless @config[:tt_host]
    raise ArgumentError, ':tt_host must be specified when it is not ' \
    'present in the global configuration.'
  end
  @tyrant = Rufus::Tokyo::Tyrant.new(@config[:tt_host], @config[:tt_port])
rescue RuntimeError => boom
  if boom.message.include?('couldn\'t connect to tyrant')
    errmsg = 'Unable to connect to the Tokyo Tyrant server at ' \
    "#{@config[:tt_host]} [#{@config[:tt_port]}]"
    if @config[:no_raise_on_connection_fail]
      @tyrant = nil
      STDERR.puts("[GCODER] #{errmsg}")
    else
      raise Errors::TTUnableToConnectError, errmsg
    end
  else
    raise boom
  end
end

Instance Method Details

#[](key) ⇒ Object



38
39
40
# File 'lib/gcoder/persistence.rb', line 38

def [](key)
  storage_get(key)
end

#[]=(key, value) ⇒ Object



42
43
44
45
46
47
# File 'lib/gcoder/persistence.rb', line 42

def []=(key, value)
  unless key.is_a?(String) || key.is_a?(Symbol)
    raise ArgumentError, "key must be String or Symbol, not: #{key.class}"
  end
  storage_put(key, value)
end

#fetch(key, &block) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/gcoder/persistence.rb', line 29

def fetch(key, &block)
  unless block_given?
    raise ArgumentError, 'no block was given but one was expected'
  end
  value = storage_get(key)
  return value if value
  storage_put(key, block.call(key.to_s))
end