Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
source :rubygems
gemspec
gem "activesupport"
gemspec
16 changes: 16 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
23 changes: 12 additions & 11 deletions lib/settingslogic.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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('.')
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
8 changes: 5 additions & 3 deletions settingslogic.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ Gem::Specification.new do |s|
s.name = "settingslogic"
s.version = "2.0.9"
s.platform = Gem::Platform::RUBY
s.authors = ["Ben Johnson"]
s.email = ["[email protected]"]
s.authors = ["Ben Johnson", "Derek Rockwell", "Chris Nelson"]
s.email = ["[email protected]", "[email protected]", "[email protected]"]
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'

s.files = `git ls-files`.split("\n")
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
end