Class: GlobalConfiguration::Configuration

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/global_configuration/configuration.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.[](key) ⇒ Object



61
62
63
# File 'lib/global_configuration/configuration.rb', line 61

def self.[](key)
  read(key)
end

.[]=(key, value) ⇒ Object



65
66
67
# File 'lib/global_configuration/configuration.rb', line 65

def self.[]=(key, value)
  write!(key, value)
end

.delete(key) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/global_configuration/configuration.rb', line 49

def self.delete(key)
  result = find(key.to_s).try(:destroy) rescue false
  if result
    Rails.cache.delete(cache_key(key))
  end
  result
end

.delete!(key) ⇒ Object



57
58
59
# File 'lib/global_configuration/configuration.rb', line 57

def self.delete!(key)
  delete(key) or raise(ArgumentError, 'key must not be blank')
end

.read(key) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/global_configuration/configuration.rb', line 39

def self.read(key)
  key = key.to_s

  if !key.blank?
    Rails.cache.fetch(cache_key(key)) do
      find(key).data rescue nil 
    end
  end
end

.write(key, value) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/global_configuration/configuration.rb', line 18

def self.write(key, value)
  key = key.to_s

  if !key.blank?
    if value.present?
      configuration = find_or_initialize_by(key: key)
      configuration.data = value
      if configuration.save
        Rails.cache.write(cache_key(key), value)
        true
      end
    else
      delete(key)
    end
  end
end

.write!(key, value) ⇒ Object



35
36
37
# File 'lib/global_configuration/configuration.rb', line 35

def self.write!(key, value)
  write(key, value) or raise(ArgumentError, 'key must not be blank')
end

Instance Method Details

#dataObject



7
8
9
# File 'lib/global_configuration/configuration.rb', line 7

def data
  data_string || data_integer || data_float || data_boolean
end

#data=(value) ⇒ Object



11
12
13
14
15
16
# File 'lib/global_configuration/configuration.rb', line 11

def data=(value)
  self.data_integer = (value if value.is_a?(Integer))
  self.data_float = (value if value.is_a?(Float))
  self.data_boolean = (value if (value.is_a?(TrueClass) || value.is_a?(FalseClass)))
  self.data_string = (value if value.is_a?(String))
end