Class: Configurable

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/configurable.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.[](key) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/models/configurable.rb', line 38

def self.[](key)
  return parse_value key, defaults[key][:default] unless table_exists?

  value, found = [false, false]
  database_finder = -> do
    config = find_by_name(key)
    found = !!config
    value = config.try(:value)
  end


  if ConfigurableEngine::Engine.config.use_cache
    found = Rails.cache.exist?(cache_key(key))
    if found
      value = Rails.cache.fetch(cache_key(key))
    else
      database_finder.call
      if found
        Rails.cache.write cache_key(key), value
      end
    end
  else
    database_finder.call
  end

  if found
    value
  else
    parse_value(key, defaults[key][:default]).tap do |val|
      if ConfigurableEngine::Engine.config.use_cache
        Rails.cache.write cache_key(key), val
      end
    end
  end
end

.[]=(key, value) ⇒ Object



25
26
27
28
29
30
31
32
# File 'app/models/configurable.rb', line 25

def self.[]=(key, value)
  exisiting = find_by_name(key)
  if exisiting
    exisiting.update_attribute(:value, value)
  else
    create {|c| c.name = key.to_s; c.value = value}
  end
end

.cache_key(key) ⇒ Object



34
35
36
# File 'app/models/configurable.rb', line 34

def self.cache_key(key)
  "configurable_engine:#{key}"
end

.defaultsObject



13
14
15
16
17
18
19
# File 'app/models/configurable.rb', line 13

def self.defaults
  @defaults ||= HashWithIndifferentAccess.new(
    YAML.load_file(
      Rails.root.join('config', 'configurable.yml')
    )
  )
end

.keysObject



21
22
23
# File 'app/models/configurable.rb', line 21

def self.keys
  self.defaults.collect { |k,v| k.to_s }.sort
end

.method_missing(name, *args) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
# File 'app/models/configurable.rb', line 120

def self.method_missing(name, *args)
  name_stripped = name.to_s.sub(/[\?=]$/, '')
  if self.keys.include?(name_stripped)
    if name.to_s.end_with?('=')
      self[name_stripped] = args.first
    else
      self[name_stripped]
    end
  else
    super
  end
end

.parse_value(key, value) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/models/configurable.rb', line 78

def self.parse_value key, value
  case defaults[key][:type]
  when 'boolean'
    [true, 1, "1", "t", "true"].include?(value)
  when 'decimal'
    value ||= 0
    BigDecimal(value.to_s)
  when 'integer'
    value.to_i
  when 'list'
    value ||= []
    if value.is_a? Array
      value
    else
      value.split("\n").collect { |v| v =~ /,/ ? v.split(',') : v }
    end
  when 'date'
    if value.is_a? Date
      value
    else
     Date.parse(value) if value.present?
    end
  else
    value
  end
end

.serialized_value(key) ⇒ Object



105
106
107
# File 'app/models/configurable.rb', line 105

def self.serialized_value key
  value_for_serialization key, self[key]
end

.value_for_serialization(key, value) ⇒ Object



109
110
111
112
113
114
115
116
117
118
# File 'app/models/configurable.rb', line 109

def self.value_for_serialization(key, value)
  if defaults[key][:type] == 'list' && value.is_a?(Array)
    if value.all? {|entry| entry.is_a? Array}
      value = value.collect {|entry| entry.join ','}
    end
    value.join("\n")
  else
    value.to_s
  end.gsub("\r\n","\n")
end

Instance Method Details

#valueObject



74
75
76
# File 'app/models/configurable.rb', line 74

def value
  self.class.parse_value name, super
end