mc-settings 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "rspec"
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.5.1"
12
+ gem "rcov", ">= 0"
13
+ gem "ruby-debug"
14
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,36 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ columnize (0.3.2)
5
+ diff-lcs (1.1.2)
6
+ git (1.2.5)
7
+ jeweler (1.5.1)
8
+ bundler (~> 1.0.0)
9
+ git (>= 1.2.5)
10
+ rake
11
+ linecache (0.43)
12
+ rake (0.8.7)
13
+ rcov (0.9.9)
14
+ rspec (2.3.0)
15
+ rspec-core (~> 2.3.0)
16
+ rspec-expectations (~> 2.3.0)
17
+ rspec-mocks (~> 2.3.0)
18
+ rspec-core (2.3.0)
19
+ rspec-expectations (2.3.0)
20
+ diff-lcs (~> 1.1.2)
21
+ rspec-mocks (2.3.0)
22
+ ruby-debug (0.10.4)
23
+ columnize (>= 0.1)
24
+ ruby-debug-base (~> 0.10.4.0)
25
+ ruby-debug-base (0.10.4)
26
+ linecache (>= 0.3)
27
+
28
+ PLATFORMS
29
+ ruby
30
+
31
+ DEPENDENCIES
32
+ bundler (~> 1.0.0)
33
+ jeweler (~> 1.5.1)
34
+ rcov
35
+ rspec
36
+ ruby-debug
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Edwin Cruz
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,53 @@
1
+ = Settings
2
+
3
+ Application Settings Manager
4
+
5
+ == Usage in Rails
6
+ Settings conventions
7
+ key_name:
8
+ default: value
9
+ key_name: value
10
+ multiples_values:
11
+ default: default
12
+ another_value: another_default
13
+
14
+
15
+ config/settings/default.yml
16
+ config/settings/environments/development.yml
17
+ config/settings/environments/production.yml
18
+ config/settings/local/custom.yml
19
+ config/settings/local/verysecret.yml
20
+
21
+ Setting.load(:files => ["default.yml", "environments/#{Rails.env}.yml"],
22
+ :path => "#{Rails.root}/config/settings",
23
+ :local => true)
24
+
25
+ Setting.key_name
26
+ Setting.available_settings['key_name']
27
+ Setting.available_settings['key_name']['another_value']
28
+ Setting['key_name']
29
+ Setting[:key_name]
30
+ Setting['multiples_values']['another_value']
31
+
32
+
33
+ This means:
34
+ * Read default.yml, and load all setings in Setting object
35
+ * Override specific keys depending on rails environment
36
+ * Apply all #{path}/local/*.yml files to Settings, overriding common keys
37
+
38
+
39
+ == Contributing to mc-settings
40
+
41
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
42
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
43
+ * Fork the project
44
+ * Start a feature/bugfix branch
45
+ * Commit and push until you are happy with your contribution
46
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
47
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
48
+
49
+ == Copyright
50
+
51
+ Copyright (c) 2010 Edwin Cruz. See LICENSE.txt for
52
+ further details.
53
+
data/Rakefile ADDED
@@ -0,0 +1,50 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "mc-settings"
16
+ gem.homepage = "http://github.com/modcloth/mc-settings"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{Manage settings per environment}
19
+ gem.description = %Q{implement custom keys indenendently of environment}
20
+ gem.email = "rubydev@modcloth.com"
21
+ gem.authors = ["Edwin Cruz", "Colin Shield"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
25
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rspec/core'
30
+ require 'rspec/core/rake_task'
31
+ require 'ruby-debug'
32
+ RSpec::Core::RakeTask.new(:spec) do |spec|
33
+ spec.pattern = FileList['spec/**/*_spec.rb']
34
+ end
35
+
36
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
37
+ spec.pattern = 'spec/**/*_spec.rb'
38
+ end
39
+
40
+ task :default => :spec
41
+
42
+ require 'rake/rdoctask'
43
+ Rake::RDocTask.new do |rdoc|
44
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
45
+
46
+ rdoc.rdoc_dir = 'rdoc'
47
+ rdoc.title = "mc-settings #{version}"
48
+ rdoc.rdoc_files.include('README*')
49
+ rdoc.rdoc_files.include('lib/**/*.rb')
50
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1 @@
1
+ require 'setting'
data/lib/setting.rb ADDED
@@ -0,0 +1,84 @@
1
+ require 'singleton'
2
+ class Setting
3
+ class SettingNotFound < RuntimeError; end
4
+ class SettingFileError < RuntimeError; end
5
+
6
+ include Singleton
7
+
8
+ def self.reload params = {}
9
+ @available_settings = {}
10
+ self.load params
11
+ end
12
+
13
+ def self.available_settings
14
+ self.instance ? @available_settings : {}
15
+ end
16
+
17
+ # get a setting value by [] notation
18
+ def self.[](name)
19
+ self.check_value(name.to_s)
20
+ self.value_for(name.to_s)
21
+ end
22
+
23
+ def self.method_missing(method, *args, &block)
24
+ # see if this method is defined above us in the hierarchy
25
+ super(method, *args)
26
+ rescue
27
+ name = method.to_s
28
+ if name[-1, 1] == "?"
29
+ name.chomp!('?')
30
+ self[name]['default'].to_i > 0
31
+ else
32
+ self[name]
33
+ end
34
+ end
35
+
36
+ def initialize
37
+ @available_settings ||= {}
38
+ end
39
+
40
+ class << self
41
+ def has_key?(key)
42
+ @available_settings.has_key?(key)
43
+ end
44
+
45
+ def value_for(value)
46
+ v = @available_settings[value]
47
+ if v.is_a?(Hash) && v.size > 1
48
+ v
49
+ elsif v.is_a?(Hash) && v.has_key?("default")
50
+ v['default'].nil? ? "" : v['default']
51
+ else
52
+ v
53
+ end
54
+ end
55
+ end
56
+
57
+ def self.load(params)
58
+ files = []
59
+ path = params[:path]
60
+ params[:files].each do |file|
61
+ files << File.join(path, file)
62
+ end
63
+ if params[:local]
64
+ files << Dir.glob(File.join(path, 'local', '*.yml'))
65
+ end
66
+ @available_settings ||= {}
67
+ files.flatten.each do |file|
68
+ begin
69
+ @available_settings.merge!(YAML::load(File.open(file)) || {}) if File.exists?(file)
70
+ rescue Exception => e
71
+ raise SettingNotFound.new("Error parsing file #{file}, with: #{e.message}")
72
+ end
73
+ end
74
+ @available_settings
75
+ end
76
+
77
+ private
78
+
79
+ def self.check_value(name)
80
+ raise RuntimeError.new("settings are not yet initialized") unless self.instance
81
+ raise SettingNotFound.new("#{name} not found") unless self.has_key?(name)
82
+ end
83
+
84
+ end
@@ -0,0 +1,50 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ describe Setting do
3
+ subject { Setting }
4
+
5
+ context "Test environment" do
6
+ before :each do
7
+ stub_setting_files
8
+ Setting.reload(
9
+ :files => ["default.yml", "environments/test.yml"],
10
+ :path => "config/settings",
11
+ :local => true)
12
+ end
13
+ it 'should return test specific values' do
14
+ Setting.available_settings['one'].should == "test"
15
+ Setting.one.should == "test"
16
+ Setting['one'].should == "test"
17
+ end
18
+
19
+ it "should handle custom values overriding everything else" do
20
+ Setting.seven.should == "seven from custom"
21
+ end
22
+
23
+ it "handles multiple values" do
24
+ Setting['six'].should == {"default"=>"default value", "extra"=>"extra"}
25
+ Setting.available_settings['six']['default'].should == "default value"
26
+ Setting.available_settings['six']['extra'].should == "extra"
27
+ Setting.seven.should == "seven from custom"
28
+ end
29
+
30
+ it "should support symbols as keys" do
31
+ Setting[:six].should == {"default"=>"default value", "extra"=>"extra"}
32
+ end
33
+ it "handles default key" do
34
+ Setting.default_setting.should == 1
35
+ Setting['seven'].should == "seven from custom"
36
+ end
37
+
38
+ it "should handle empty strings" do
39
+ Setting.empty.should == ""
40
+ end
41
+
42
+ it "should responds to ? mark" do
43
+ Setting.autologin?.should == true
44
+ end
45
+
46
+ it "should returns false correctly" do
47
+ Setting.flag_false.should be(false)
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,13 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'mc-settings'
5
+ require 'ruby-debug'
6
+
7
+ # Requires supporting files with custom matchers and macros, etc,
8
+ # in ./support/ and its subdirectories.
9
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
10
+
11
+ RSpec.configure do |config|
12
+
13
+ end
@@ -0,0 +1,46 @@
1
+ def stub_setting_files
2
+ defaults = <<-CONTENT
3
+ one: default
4
+ two:
5
+ three: 3
6
+ four: "4"
7
+ five: "default string"
8
+ default_setting: 1
9
+ six:
10
+ default: "default value"
11
+ extra: "extra"
12
+ seven:
13
+ default: "seven"
14
+ empty:
15
+ default:
16
+ autologin:
17
+ format: int
18
+ default: 7
19
+ flag_false:
20
+ default: false
21
+ CONTENT
22
+ test = <<-CONTENT
23
+ one: test
24
+ two:
25
+ three: 5
26
+ four: "6"
27
+ five: "test string"
28
+ CONTENT
29
+
30
+ empty = <<-CONTENT
31
+ CONTENT
32
+
33
+ custom = <<-CONTENT
34
+ seven:
35
+ default: "seven from custom"
36
+ CONTENT
37
+
38
+ File.stub!(:exists?).and_return(true)
39
+ File.stub!(:exists?).with("config/settings/environments/development.yml").and_return(false)
40
+ File.stub!(:open).with("config/settings/default.yml").and_return(defaults)
41
+ File.stub!(:open).with("config/settings/environments/test.yml").and_return(test)
42
+ File.stub!(:open).with("config/settings/local/custom.yml").and_return(custom)
43
+ File.stub!(:open).with("config/settings/local/empty.yml").and_return(empty)
44
+
45
+ Dir.stub!(:glob).and_return(["config/settings/local/empty.yml", "config/settings/local/custom.yml"])
46
+ end
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mc-settings
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Edwin Cruz
14
+ - Colin Shield
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2010-12-17 00:00:00 -08:00
20
+ default_executable:
21
+ dependencies:
22
+ - !ruby/object:Gem::Dependency
23
+ name: rspec
24
+ prerelease: false
25
+ requirement: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ">="
29
+ - !ruby/object:Gem::Version
30
+ hash: 3
31
+ segments:
32
+ - 0
33
+ version: "0"
34
+ type: :development
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: bundler
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ hash: 23
45
+ segments:
46
+ - 1
47
+ - 0
48
+ - 0
49
+ version: 1.0.0
50
+ type: :development
51
+ version_requirements: *id002
52
+ - !ruby/object:Gem::Dependency
53
+ name: jeweler
54
+ prerelease: false
55
+ requirement: &id003 !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ~>
59
+ - !ruby/object:Gem::Version
60
+ hash: 1
61
+ segments:
62
+ - 1
63
+ - 5
64
+ - 1
65
+ version: 1.5.1
66
+ type: :development
67
+ version_requirements: *id003
68
+ - !ruby/object:Gem::Dependency
69
+ name: rcov
70
+ prerelease: false
71
+ requirement: &id004 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ hash: 3
77
+ segments:
78
+ - 0
79
+ version: "0"
80
+ type: :development
81
+ version_requirements: *id004
82
+ - !ruby/object:Gem::Dependency
83
+ name: ruby-debug
84
+ prerelease: false
85
+ requirement: &id005 !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ hash: 3
91
+ segments:
92
+ - 0
93
+ version: "0"
94
+ type: :development
95
+ version_requirements: *id005
96
+ description: implement custom keys indenendently of environment
97
+ email: rubydev@modcloth.com
98
+ executables: []
99
+
100
+ extensions: []
101
+
102
+ extra_rdoc_files:
103
+ - LICENSE.txt
104
+ - README.rdoc
105
+ files:
106
+ - .document
107
+ - Gemfile
108
+ - Gemfile.lock
109
+ - LICENSE.txt
110
+ - README.rdoc
111
+ - Rakefile
112
+ - VERSION
113
+ - lib/mc-settings.rb
114
+ - lib/setting.rb
115
+ - spec/mc_settings_spec.rb
116
+ - spec/spec_helper.rb
117
+ - spec/support/settings_helper.rb
118
+ has_rdoc: true
119
+ homepage: http://github.com/modcloth/mc-settings
120
+ licenses:
121
+ - MIT
122
+ post_install_message:
123
+ rdoc_options: []
124
+
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ none: false
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ hash: 3
133
+ segments:
134
+ - 0
135
+ version: "0"
136
+ required_rubygems_version: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ hash: 3
142
+ segments:
143
+ - 0
144
+ version: "0"
145
+ requirements: []
146
+
147
+ rubyforge_project:
148
+ rubygems_version: 1.3.7
149
+ signing_key:
150
+ specification_version: 3
151
+ summary: Manage settings per environment
152
+ test_files:
153
+ - spec/mc_settings_spec.rb
154
+ - spec/spec_helper.rb
155
+ - spec/support/settings_helper.rb