Sha256: b114f39f8ea5b2bf8a09ac2699ceba2e8259e1acbbf77917cb0e0d87cd12d776

Contents?: true

Size: 1.28 KB

Versions: 3

Compression:

Stored size: 1.28 KB

Contents

class Configurable < ActiveRecord::Base

  validates_presence_of :name
  validates_uniqueness_of :name

  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)
    exisiting = find_by_name(key)
    if exisiting
      exisiting.update_attribute(:value, value)
    else
      create(:name => key.to_s, :value => value)
    end
  end

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

    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.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

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
configurable_engine-0.3.0 app/models/configurable.rb
configurable_engine-0.2.10 app/models/configurable.rb
configurable_engine-0.2.9 app/models/configurable.rb