diff --git a/Gemfile b/Gemfile index df87cd4..c6e0e7d 100644 --- a/Gemfile +++ b/Gemfile @@ -1,2 +1,3 @@ source :rubygems -gemspec \ No newline at end of file +gem "activesupport" +gemspec diff --git a/Gemfile.lock b/Gemfile.lock index 6446d66..07c212b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,11 +2,21 @@ PATH remote: . specs: settingslogic (2.0.9) + activesupport GEM remote: http://rubygems.org/ specs: + activesupport (7.0.4.2) + concurrent-ruby (~> 1.0, >= 1.0.2) + i18n (>= 1.6, < 2) + minitest (>= 5.1) + tzinfo (~> 2.0) + concurrent-ruby (1.2.2) diff-lcs (1.1.3) + i18n (1.12.0) + concurrent-ruby (~> 1.0) + minitest (5.18.0) rake (10.0.3) rspec (2.12.0) rspec-core (~> 2.12.0) @@ -16,11 +26,17 @@ GEM rspec-expectations (2.12.1) diff-lcs (~> 1.1.3) rspec-mocks (2.12.1) + tzinfo (2.0.6) + concurrent-ruby (~> 1.0) PLATFORMS ruby DEPENDENCIES + activesupport rake rspec settingslogic! + +BUNDLED WITH + 2.3.12 diff --git a/lib/settingslogic.rb b/lib/settingslogic.rb index 83055f7..036e41f 100644 --- a/lib/settingslogic.rb +++ b/lib/settingslogic.rb @@ -1,6 +1,7 @@ require "yaml" require "erb" require 'open-uri' +require 'active_support' # A simple settings solution using a YAML file. See README for more information. class Settingslogic < Hash @@ -10,7 +11,7 @@ class << self def name # :nodoc: self.superclass != Hash && instance.key?("name") ? instance.name : super end - + # Enables Settings.get('nested.key.name') for dynamic access def get(key) parts = key.split('.') @@ -108,8 +109,8 @@ def initialize(hash_or_file = self.class.source, section = nil) self.replace hash_or_file else file_contents = open(hash_or_file).read - hash = file_contents.empty? ? {} : YAML.load(ERB.new(file_contents).result).to_hash - default_hash = hash[self.class.default_namespace] || {} + hash = file_contents.empty? ? {} : YAML.safe_load(ERB.new(file_contents).result, aliases: true).to_hash + default_hash = hash[self.class.default_namespace] || {} if self.class.namespace hash = hash[self.class.namespace] or return missing_key("Missing setting '#{self.class.namespace}' in #{hash_or_file}") end @@ -176,22 +177,22 @@ def #{key} end EndEval end - + def symbolize_keys - + inject({}) do |memo, tuple| - + k = (tuple.first.to_sym rescue tuple.first) || tuple.first - + v = k.is_a?(Symbol) ? send(k) : tuple.last # make sure the value is accessed the same way Settings.foo.bar works - + memo[k] = v && v.respond_to?(:symbolize_keys) ? v.symbolize_keys : v #recurse for nested hashes - + memo end - + end - + def missing_key(msg) return nil if self.class.suppress_errors diff --git a/settingslogic.gemspec b/settingslogic.gemspec index 4b87f4b..4d55581 100644 --- a/settingslogic.gemspec +++ b/settingslogic.gemspec @@ -5,12 +5,14 @@ Gem::Specification.new do |s| s.name = "settingslogic" s.version = "2.0.9" s.platform = Gem::Platform::RUBY - s.authors = ["Ben Johnson"] - s.email = ["bjohnson@binarylogic.com"] + s.authors = ["Ben Johnson", "Derek Rockwell", "Chris Nelson"] + s.email = ["bjohnson@binarylogic.com", "derek.rockwell@zipnosis.com", "chris.nelson@zipnosis.com"] s.homepage = "http://github.com/binarylogic/settingslogic" s.summary = %q{A simple and straightforward settings solution that uses an ERB enabled YAML file and a singleton design pattern.} s.description = %q{A simple and straightforward settings solution that uses an ERB enabled YAML file and a singleton design pattern.} + s.add_dependency 'activesupport' + s.add_development_dependency 'rake' s.add_development_dependency 'rspec' @@ -18,4 +20,4 @@ Gem::Specification.new do |s| s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } s.require_paths = ["lib"] -end \ No newline at end of file +end