Class: Econfig::ActiveRecord

Inherits:
Object
  • Object
show all
Defined in:
lib/econfig/active_record.rb

Defined Under Namespace

Classes: Option

Constant Summary collapse

MISSING_TABLE_WARNING =
<<-WARNING.gsub(/^ */, "")
  =======================================================================
  Econfig table not found. Please add the configuration table by running:

  rails generate econfig:migration
  rake db:migrate
  =======================================================================
WARNING

Instance Method Summary collapse

Instance Method Details

#get(key) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/econfig/active_record.rb', line 28

def get(key)
  if option = Option.find_by_key(key)
    option.value
  else
    yield if block_given?
  end
rescue ::ActiveRecord::StatementInvalid
  warn MISSING_TABLE_WARNING
end

#keysObject



21
22
23
24
25
26
# File 'lib/econfig/active_record.rb', line 21

def keys
  Set.new(Option.pluck(:key))
rescue ::ActiveRecord::StatementInvalid
  warn MISSING_TABLE_WARNING
  Set.new([])
end

#set(key, value) ⇒ Object



38
39
40
41
42
43
# File 'lib/econfig/active_record.rb', line 38

def set(key, value)
  option = Option.where(:key => key).first_or_initialize
  option.update_attributes!(:value => value)
rescue ::ActiveRecord::StatementInvalid
  warn MISSING_TABLE_WARNING
end