Skip to content

Instantly share code, notes, and snippets.

@zenlor
Created June 13, 2011 16:53
Show Gist options
  • Save zenlor/1023171 to your computer and use it in GitHub Desktop.
Save zenlor/1023171 to your computer and use it in GitHub Desktop.
simple config-keeper for Padrino and MongoMapper
{
"site":{
"locales":["it"],
"acl":{
"admin":["links","photos","posts","pages","taxonomy"]
}
}
}
module Cfg
class Backend
attr_accessor :config, :cache
def initialize
@cache = Time.new
@config = JSON.parse(File.read(Padrino.root("config", "config.json")))['site']
fetch
end
def get key
if Time.new - @cache > 1.hour
refresh
end
@config[key.to_s]
end
def refresh
fetch
end
protected
def fetch
conf = Hash.new
MongoMapper.database['configurations'].find({}).find_all.to_a.each do |c|
conf[c["key"]] = c["value"]
end
@config = @config.merge conf
@cache = Time.new
end
end
class << self
def config
Thread.current[:cfg_fetcher] ||= Cfg::Backend.new
end
def config= v
Thread.current[:cfg_fetcher] = v
end
def roles
t = self[:roles]
return (t.nil?)? [:admin] : t
end
def [] key
self.config.get key
end
def acl role
self[role]
end
def refresh!
self.config.refresh
end
def locale
(self[:locales].include? I18n.locale)? I18n.locale : self[:locales].first
end
def default_locale
Cfg[:locales].first
end
end
end
Cfg.refresh!
@zenlor
Copy link
Author

zenlor commented Jun 13, 2011

note to make this: JSON.parse(File.read(Padrino.root("config", "config.json"))) working Yajl ("yajl/json-gem") is needed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment