Sha256: dd778deb2fa7473f77b231d9fa1746a8703b664f0a6a553936da11f990538872

Contents?: true

Size: 948 Bytes

Versions: 1

Compression:

Stored size: 948 Bytes

Contents

require 'memoist'

module Catarse
class Configuration < ActiveRecord::Base
  attr_accessible :name, :value
  validates_presence_of :name
  class << self
    extend Memoist

    # This method returns the values of the config simulating a Hash, like:
    #   Configuration[:foo]
    # It can also bring Arrays of keys, like:
    #   Configuration[:foo, :bar]
    # ... so you can pass it to a method using *.
    # It is memoized, so it will be correctly cached.
    def [] *keys
      if keys.size == 1
        get keys.shift
      else
        keys.map{|key| get key }
      end
    end
    def []= key, value
      set key, value
    end
  private

    def get key
      find_by_name(key).value rescue nil
    end
    memoize :get

    def set key, value
      begin
        find_by_name(key).update_attribute :value, value
      rescue
        create!(name: key, value: value)
      end
      flush_cache(:get)
      value
    end

  end
end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
catarse_core-1.0.0.beta app/models/catarse/configuration.rb