Class: Raterr::StoreContainer

Inherits:
Object
  • Object
show all
Defined in:
lib/raterr/store_container.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store:, identifier:) ⇒ StoreContainer

Returns a new instance of StoreContainer.



6
7
8
9
# File 'lib/raterr/store_container.rb', line 6

def initialize(store:, identifier:)
  @store = store
  @identifier = identifier
end

Instance Attribute Details

#identifierObject (readonly)

Returns the value of attribute identifier.



4
5
6
# File 'lib/raterr/store_container.rb', line 4

def identifier
  @identifier
end

#storeObject (readonly)

Returns the value of attribute store.



4
5
6
# File 'lib/raterr/store_container.rb', line 4

def store
  @store
end

#store_typeObject (readonly)

Returns the value of attribute store_type.



4
5
6
# File 'lib/raterr/store_container.rb', line 4

def store_type
  @store_type
end

Instance Method Details

#resolve(method, attrs = nil) ⇒ Object



11
12
13
14
# File 'lib/raterr/store_container.rb', line 11

def resolve(method, attrs = nil)
  store_type = store.class.to_s.downcase.to_sym
  resolvers[method][store_type].call(attrs)
end

#resolversObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/raterr/store_container.rb', line 16

def resolvers
  {
    get: {
      hash: -> (attributes) { store.fetch(identifier) { { 'attempts' => 1, 'start_time' => Time.now } } },
      redis: -> (attributes) { JSON.parse(store.get(identifier) || { 'attempts' => 1, 'start_time' => Time.now }.to_json) }
    },
    set: {
      hash: -> (attributes) { store[identifier] = attributes },
      redis: -> (attributes) { store.set(identifier, attributes.to_json) }
    },
    delete: {
      hash: -> (identifier) { store.delete(identifier) },
      redis: -> (identifier) { store.del(identifier) }
    }
  }
end