Sha256: 8f3c359427773f522622f54229ad5d5e24d34b6c9d27d9264cebb234eaa95808

Contents?: true

Size: 893 Bytes

Versions: 8

Compression:

Stored size: 893 Bytes

Contents

class Configurable < ActiveRecord::Base
  
  def self.defaults
    HashWithIndifferentAccess.new(
      YAML.load_file(
        Rails.root.join('config', 'configurable.yml')
      )
    )
  end
  
  def self.keys
    self.defaults.collect { |k,v| k.to_s }.sort
  end
  
  def self.[](key)
    value = find_by_name(key).try(:value) || self.defaults[key][:default]
    case self.defaults[key][:type]
    when 'boolean'
      [true, 1, "1", "t", "true"].include?(value)
    when 'decimal'
      BigDecimal.new(value.to_s)
    when 'integer'
      value.to_i
    when 'list'
      return value if value.is_a?(Array)
      value.split("\n").collect{ |v| v.split(',') }
    else
      value
    end
  end
  
  def self.method_missing(name, *args)
    name_stripped = name.to_s.gsub('?', '')
    if self.keys.include?(name_stripped)
      self[name_stripped]
    else
      super
    end
  end
  
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
configurable_engine-0.2.8 app/models/configurable.rb
configurable_engine-0.2.7 app/models/configurable.rb
configurable_engine-0.2.6 app/models/configurable.rb
configurable_engine-0.2.5 app/models/configurable.rb
configurable_engine-0.2.4 app/models/configurable.rb
configurable_engine-0.2.3 app/models/configurable.rb
configurable_engine-0.2.2 app/models/configurable.rb
configurable_engine-0.2.1 app/models/configurable.rb