Skip to content

Nested namespaces #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
22 changes: 22 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,28 @@ If your default settings seem to be overwriting your environment-specific settin
require 'yaml'
YAML::ENGINE.yamler= 'syck'

==== Namespaced settings

Maybe you need to use another semantics for your namespace besides the current
environment. You can put a nested namespace in your settings and you are good
to go.

# /some/file/here.yml
professional:
preferences:
order: desc
client:
prefs:
order: asc

class ProfessionalPreferences < Settingslogic
source "/some/file/here.yml"
namespace "professional.preferences"
end

>> ProfessionalPreferences.order
=> "desc"

=== 3. Access your settings

>> Rails.env
Expand Down
25 changes: 19 additions & 6 deletions lib/settingslogic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ def name # :nodoc:
end

# Enables Settings.get('nested.key.name') for dynamic access
def get(key)
def get(key, hash = nil)
parts = key.split('.')
curs = self
curs = hash || self
while p = parts.shift
curs = curs.send(p)
curs = curs[p] or (yield p if block_given?)
end
curs
end
Expand Down Expand Up @@ -101,15 +101,28 @@ def initialize(hash_or_file = self.class.source, section = nil)
else
file_contents = open(hash_or_file).read
hash = file_contents.empty? ? {} : YAML.load(ERB.new(file_contents).result).to_hash
if self.class.namespace
hash = hash[self.class.namespace] or return missing_key("Missing setting '#{self.class.namespace}' in #{hash_or_file}")
end
hash = namespaced_hash(hash, hash_or_file) if self.class.namespace
self.replace hash
end
@section = section || self.class.source # so end of error says "in application.yml"
create_accessors!
end

def missing_setting_action(namespace)
if namespace.include? "."
proc { missing_key "Missing setting '#{setting}' for '#{namespace}' in #{hash_or_file}" }
else
proc { missing_key "Missing setting '#{namespace}' in #{hash_or_file}" }
end
end

def namespaced_hash(hash, hash_or_file)
namespace = self.class.namespace
self.class.get namespace, hash, &missing_setting_action(namespace)
end

private :missing_setting_action, :namespaced_hash

# Called for dynamically-defined keys, and also the first key deferenced at the top-level, if load! is not used.
# Otherwise, create_accessors! (called by new) will have created actual methods for each key.
def method_missing(name, *args, &block)
Expand Down
12 changes: 11 additions & 1 deletion spec/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,14 @@ class Settings < Settingslogic
end

class SettingsInst < Settingslogic
end
end

class SettingsNestedNamespace < Settingslogic
source "#{File.dirname(__FILE__)}/settings.yml"
namespace "language.smalltalk"
end

class SettingsInvalidNestedNamespace < Settingslogic
source "#{File.dirname(__FILE__)}/settings.yml"
namespace "inexistent.namespace.omg"
end
13 changes: 13 additions & 0 deletions spec/settingslogic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,19 @@ class NoSource < Settingslogic; end
SettingsEmpty.keys.should eql([])
end

context "using a nested namespace (`language.smalltalk`)" do
it "returns the right internal nested value" do
expect(SettingsNestedNamespace.paradigm).to eq "object oriented"
end

context "when namespace doesn't exists" do
it "raises the missing key error" do
expect { SettingsInvalidNestedNamespace.paradigm }.to \
raise_error Settingslogic::MissingSetting
end
end
end

# Put this test last or else call to .instance will load @instance,
# masking bugs.
it "should be a hash" do
Expand Down