Class: Configurable
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Configurable
- Defined in:
- app/models/configurable.rb
Class Method Summary collapse
- .[](key) ⇒ Object
- .[]=(key, value) ⇒ Object
- .cache_key(key) ⇒ Object
- .defaults ⇒ Object
- .keys ⇒ Object
- .method_missing(name, *args) ⇒ Object
- .parse_value(key, value) ⇒ Object
- .serialized_value(key) ⇒ Object
- .value_for_serialization(key, value) ⇒ Object
Instance Method Summary collapse
Class Method Details
.[](key) ⇒ Object
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 |
# File 'app/models/configurable.rb', line 39 def self.[](key) return parse_value key, defaults[key][:default] unless table_exists? value = false found = false database_finder = lambda do config = find_by_name(key) found = !config.nil? 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 Rails.cache.write cache_key(key), value if found end else database_finder.call end if found value else parse_value(key, defaults[key][:default]).tap do |val| Rails.cache.write cache_key(key), val if ConfigurableEngine::Engine.config.use_cache end end end |
.[]=(key, value) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 |
# File 'app/models/configurable.rb', line 23 def self.[]=(key, value) exisiting = find_by_name(key) if exisiting exisiting.update_attribute(:value, value) else create do |c| c.name = key.to_s c.value = Configurable.value_for_serialization key, value end end end |
.cache_key(key) ⇒ Object
35 36 37 |
# File 'app/models/configurable.rb', line 35 def self.cache_key(key) "configurable_engine:#{key}" end |
.defaults ⇒ Object
11 12 13 14 15 16 17 |
# File 'app/models/configurable.rb', line 11 def self.defaults @defaults ||= HashWithIndifferentAccess.new( YAML.load_file( Rails.root.join('config', 'configurable.yml') ) ) end |
.keys ⇒ Object
19 20 21 |
# File 'app/models/configurable.rb', line 19 def self.keys defaults.collect { |k, _v| k.to_s }.sort end |
.method_missing(name, *args) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'app/models/configurable.rb', line 115 def self.method_missing(name, *args) name_stripped = name.to_s.sub(/[\?=]$/, '') if 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
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'app/models/configurable.rb', line 75 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 elsif value.present? Date.parse(value) end else value end end |
.serialized_value(key) ⇒ Object
102 103 104 |
# File 'app/models/configurable.rb', line 102 def self.serialized_value(key) value_for_serialization key, self[key] end |
.value_for_serialization(key, value) ⇒ Object
106 107 108 109 110 111 112 113 |
# File 'app/models/configurable.rb', line 106 def self.value_for_serialization(key, value) if defaults[key][:type] == 'list' && value.is_a?(Array) value = value.collect { |entry| entry.join ',' } if value.all? { |entry| entry.is_a? Array } value.join("\n") else value.to_s end.gsub("\r\n", "\n") end |
Instance Method Details
#value ⇒ Object
71 72 73 |
# File 'app/models/configurable.rb', line 71 def value self.class.parse_value name, super end |