Skip to content

Instantly share code, notes, and snippets.

@sapslaj
Created January 25, 2014 02:54
Show Gist options
  • Save sapslaj/8611089 to your computer and use it in GitHub Desktop.
Save sapslaj/8611089 to your computer and use it in GitHub Desktop.
Shitty JSON configuration manager. Based (sort of) on configatron
Settings.defaults = {:email=>'[email protected]', :database=>{:url=>'postgres://localhost/foo'}}
Settings.load(organization_name: 'My Company', application_name: 'My Application')
configatron.email # => "[email protected]"
configatron.database.url = 'postgres://localhost/foo_development'
Settings.save # => "{"email":"[email protected]", "database":{"url":"postgres://localhost/foo_development"}}"
require 'configatron'
require 'json'
# Source: https://gist.github.com/morhekil/998709
module DeepSymbolizable
def __deep_symbolize(&block)
method = self.class.to_s.downcase.to_sym
syms = DeepSymbolizable::Symbolizers
syms.respond_to?(method) ? syms.send(method, self, &block) : self
end
module Symbolizers
extend self
# the primary method - symbolizes keys of the given hash,
# preprocessing them with a block if one was given, and recursively
# going into all nested enumerables
def hash(hash, &block)
hash.inject({}) do |result, (key, value)|
# Recursively deep-symbolize subhashes
value = _recurse_(value, &block)
# Pre-process the key with a block if it was given
key = yield key if block_given?
# Symbolize the key string if it responds to to_sym
sym_key = key.to_sym rescue key
# write it back into the result and return the updated hash
result[sym_key] = value
result
end
end
# walking over arrays and symbolizing all nested elements
def array(ary, &block)
ary.map { |v| _recurse_(v, &block) }
end
# handling recursion - any Enumerable elements (except String)
# is being extended with the module, and then symbolized
def _recurse_(value, &block)
if value.is_a?(Enumerable) && !value.is_a?(String)
# support for a use case without extended core Hash
value.extend DeepSymbolizable unless value.class.include?(DeepSymbolizable)
value = value.__deep_symbolize(&block)
end
value
end
end
end
class String
def underscore
self.gsub(/::/, '_').
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
gsub(/([a-z\d])([A-Z])/,'\1_\2').
gsub(/[\s]/, '\1_\2').
gsub(/[\/|\\]/,'\1_\2').
tr("-", "_").
downcase
end
end
class Hash
include DeepSymbolizable
def deep_symbolize!
self.__deep_symbolize do |key|
if key.respond_to? :underscore
key.underscore
else
key
end
end
end
end
module Settings
extend self
def defaults=(*options)
@defaults = options[0]
end
def load(*options)
@options = options[0]
if RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/
#puts ENV['APPDATA'].to_s
#puts @options[:organization_name].to_s
#puts @options[:application_name].to_s
@settings_file_name = "#{ENV['APPDATA'].to_s}\\#{@options[:organization_name].to_s}\\#{@options[:application_name].to_s}.json"
else
@settings_file_name = "/etc/#{@options[:organization_name]}/#{@options[:application_name]}.json"
end
unless Dir.exist? File.dirname(@settings_file_name)
Dir.mkdir File.dirname(@settings_file_name)
end
unless File.exists? @settings_file_name
File.open(@settings_file_name, 'w+') do |file|
file.write(JSON.generate(@defaults))
end
end
begin
configatron.configure_from_hash(JSON.load(File.read(@settings_file_name)).deep_symbolize!)
rescue JSON::ParserError
configatron.configure_from_hash(@defaults)
end
end
def save
File.open(@settings_file_name, 'w+') do |file|
file.write(JSON.generate(configatron.to_hash))
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment