Class: FmRest::TokenStore::ActiveRecord
- Defined in:
- lib/fmrest/token_store/active_record.rb
Overview
Heavily inspired by Moneta's ActiveRecord store:
https://github.com/minad/moneta/blob/master/lib/moneta/adapters/activerecord.rb
Constant Summary collapse
- DEFAULT_TABLE_NAME =
"fmrest_session_tokens".freeze
Class Attribute Summary collapse
-
.connection_lock ⇒ Object
readonly
Returns the value of attribute connection_lock.
Instance Attribute Summary collapse
-
#connection_pool ⇒ Object
readonly
Returns the value of attribute connection_pool.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
Attributes inherited from Base
Instance Method Summary collapse
- #delete(key) ⇒ Object
-
#initialize(options = {}) ⇒ ActiveRecord
constructor
A new instance of ActiveRecord.
- #load(key) ⇒ Object
- #store(key, value) ⇒ Object
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( = {}) super @connection_pool = ::ActiveRecord::Base.connection_pool create_table @model = Class.new(::ActiveRecord::Base) @model.table_name = table_name end |
Class Attribute Details
.connection_lock ⇒ Object (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_pool ⇒ Object (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 |
#model ⇒ Object (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 |