Class: Excursion::Datastores::ActiveRecord

Inherits:
Datastore
  • Object
show all
Defined in:
lib/excursion/datastores/active_record.rb

Instance Method Summary collapse

Methods inherited from Datastore

#all_apps, #app, #application_class

Instance Method Details

#allObject



39
40
41
42
43
# File 'lib/excursion/datastores/active_record.rb', line 39

def all
  hash = HashWithIndifferentAccess.new
  @model.all.each { |m| hash[m.key.to_sym] = m.value }
  hash
end

#delete(key) ⇒ Object Also known as: unset



31
32
33
34
35
36
# File 'lib/excursion/datastores/active_record.rb', line 31

def delete(key)
  deleted = model_find(key)
  return nil if deleted.nil?
  deleted.destroy
  deleted.value
end

#model_find(key) ⇒ Object



8
9
10
11
# File 'lib/excursion/datastores/active_record.rb', line 8

def model_find(key)
  return @model.find_by(key: key) if Excursion.rails4?
  return @model.find_by_key(key) if Excursion.rails3?
end

#read(key) ⇒ Object Also known as: get



13
14
15
16
17
# File 'lib/excursion/datastores/active_record.rb', line 13

def read(key)
  model_find(key).value
rescue
  nil
end

#write(key, value) ⇒ Object Also known as: set



20
21
22
23
24
25
26
27
28
# File 'lib/excursion/datastores/active_record.rb', line 20

def write(key, value)
  written = model_find(key)
  if written.nil?
    written = @model.create key: key, value: value
  else
    written.update value: value
  end
  written.value
end