Skip to content

Instantly share code, notes, and snippets.

@lukesutton
Created January 21, 2009 01:13
Show Gist options
  • Select an option

  • Save lukesutton/49788 to your computer and use it in GitHub Desktop.

Select an option

Save lukesutton/49788 to your computer and use it in GitHub Desktop.
Gluttonberg::Config.locales do
locale :australia_english do
location :au, "Australia"
dialect :en, "English"
default true
end
locale :spain_spanish do
location :es, "Spain", "Espana"
dialect :es, "Spanish", "Espanol"
end
locale :spain_catalan do
location :es, "Spain", "Espana"
dialect :cs, "Catalan", "Catala"
end
end
module Gluttonberg
module Config
def self.update(&blk)
self.class_eval(&blk)
end
def self.location(name, &blk)
Location.create(name, &blk)
end
class Location
@@default_location = nil
@@locations = {}
@@location_lookup = Hash.new {|h,k| h[k] = {}}
def self.create(name, &blk)
location = Location.new(name, &blk)
@@locations[name] = location
# This collection is used to do a look up of locations based on location
# and dialect codes.
@@location_lookup[location[:code]][location[:dialect_code]] = location
end
def self.all(location = nil, dialect = nil)
@@locations
end
def self.get(location, dialect)
@@location_lookup[location][dialect]
end
def self.default=(location)
@@default_location = location
end
def self.default
@@default_location
end
def initialize(name, &blk)
@default = false
@options = {}
@options[:name] = name
instance_eval(&blk)
end
def [](key)
@options[key]
end
def code(new_code)
@options[:code] = new_code
end
def label(label, native_label = nil)
@options[:label] = label
@options[:native_label] = native_label if native_label
end
def dialect(code, label, native_label = nil)
@options[:dialect_code], @options[:dialect_label] = code, label
@options[:dialect_native_label] = native_label if native_label
end
def default(bool)
@default = bool
self.class.default = self if bool
end
def default?
@default
end
end
end
end
module Gluttonberg
describe Config do
before :all do
Gluttonberg::Config.update do
location :australia_english do
code :au
label "Australia"
dialect :en, "English"
default true
end
location :spain_spanish do
code :es
label "Spain", "Espana"
dialect :es, "Spanish", "Espanol"
end
location :spain_catalan do
code :es
label "Spain", "Espana"
dialect :ca, "Catalan", "Catala"
end
end
end
it "should create locations" do
Config::Location.all.empty?.should be_false
end
it "should create three unique locations" do
Config::Location.all.length.should == 3
end
it "should set a default" do
Config::Location.default.should_not be_nil
Config::Location.default[:name].should == :australia_english
end
it "should get the correct location" do
Config::Location.get(:au, :en)[:name].should == :australia_english
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment