Class: FmRest::TokenStore::ActiveRecord

Inherits:
Base
  • Object
show all
Defined in:
lib/fmrest/token_store/active_record.rb

Overview

Constant Summary collapse

DEFAULT_TABLE_NAME =
"fmrest_session_tokens".freeze

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes inherited from Base

#options

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ActiveRecord

Returns a new instance of ActiveRecord.



24
25
26
27
28
29
30
31
32
33
# File 'lib/fmrest/token_store/active_record.rb', line 24

def initialize(options = {})
  super

  @connection_pool = ::ActiveRecord::Base.connection_pool

  create_table

  @model = Class.new(::ActiveRecord::Base)
  @model.table_name = table_name
end

Class Attribute Details

.connection_lockObject (readonly)

Returns the value of attribute connection_lock.



17
18
19
# File 'lib/fmrest/token_store/active_record.rb', line 17

def connection_lock
  @connection_lock
end

Instance Attribute Details

#connection_poolObject (readonly)

Returns the value of attribute connection_pool.



20
21
22
# File 'lib/fmrest/token_store/active_record.rb', line 20

def connection_pool
  @connection_pool
end

#modelObject (readonly)

Returns the value of attribute model.



20
21
22
# File 'lib/fmrest/token_store/active_record.rb', line 20

def model
  @model
end

Instance Method Details

#delete(key) ⇒ Object



35
36
37
# File 'lib/fmrest/token_store/active_record.rb', line 35

def delete(key)
  model.where(scope: key).delete_all
end

#load(key) ⇒ Object



39
40
41
# File 'lib/fmrest/token_store/active_record.rb', line 39

def load(key)
  model.where(scope: key).pluck(:token).first
end

#store(key, value) ⇒ Object



43
44
45
46
47
48
# File 'lib/fmrest/token_store/active_record.rb', line 43

def store(key, value)
  record = model.find_or_initialize_by(scope: key)
  record.token = value
  record.save!
  value
end