ssoroka-app_settings 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Steven Soroka
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.
@@ -0,0 +1,47 @@
1
+ = app_settings
2
+
3
+ Simple wrapper for YAML config files for Rails apps and gems
4
+
5
+ == Install
6
+
7
+ sudo gem install ssoroka-app_settings
8
+ rake app_settings:install_config
9
+
10
+ == Usage
11
+
12
+ === Usage with Rails
13
+
14
+ in an initializer, put:
15
+
16
+ $settings = AppSettings.new(File.join(Rails.root, 'config', 'app_settings.yml'))
17
+
18
+ Then anywhere in your app you can access the settings with:
19
+
20
+ $settings['users/tim/user_name']
21
+ $settings['users/tim/password']
22
+
23
+ Or whatever applies for your config file.
24
+
25
+ See example_config/example_for_rails.yml for an example yml file, here's some code examples to match:
26
+
27
+ $settings['google_maps_api_keys/localhost']
28
+
29
+ $settings['domains/legacy']
30
+
31
+ $settings['from_address']
32
+
33
+ $settings.from_address
34
+
35
+ === Usage with Gems or Plugins
36
+
37
+ # if user_config_file_path doesn't exist, default_config_file_path will be used.
38
+ @settings = AppSettings.new(user_config_file_path, default_config_file_path)
39
+ @settings['hoptoad/auth_token']
40
+
41
+ === ERB in config files
42
+
43
+ The config files support ERB: <%= %>
44
+
45
+ == Copyright
46
+
47
+ Copyright (c) 2009 Steven Soroka. See LICENSE for details.
@@ -0,0 +1,48 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "app_settings"
8
+ gem.summary = %Q{Simple wrapper for YAML config files for Rails apps and gems}
9
+ gem.email = "ssoroka78@gmail.com"
10
+ gem.homepage = "http://github.com/ssoroka/app_settings"
11
+ gem.authors = ["Steven Soroka"]
12
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
13
+ end
14
+
15
+ rescue LoadError
16
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
17
+ end
18
+
19
+ require 'spec/rake/spectask'
20
+ Spec::Rake::SpecTask.new(:spec) do |spec|
21
+ spec.libs << 'lib' << 'spec'
22
+ spec.spec_files = FileList['spec/**/*_spec.rb']
23
+ end
24
+
25
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
26
+ spec.libs << 'lib' << 'spec'
27
+ spec.pattern = 'spec/**/*_spec.rb'
28
+ spec.rcov = true
29
+ end
30
+
31
+
32
+ task :default => :spec
33
+
34
+ require 'rake/rdoctask'
35
+ Rake::RDocTask.new do |rdoc|
36
+ if File.exist?('VERSION.yml')
37
+ config = YAML.load(File.read('VERSION.yml'))
38
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
39
+ else
40
+ version = ""
41
+ end
42
+
43
+ rdoc.rdoc_dir = 'rdoc'
44
+ rdoc.title = "app_settings #{version}"
45
+ rdoc.rdoc_files.include('README*')
46
+ rdoc.rdoc_files.include('lib/**/*.rb')
47
+ end
48
+
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 0
3
+ :minor: 2
4
+ :patch: 0
@@ -0,0 +1,50 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{app_settings}
5
+ s.version = "0.2.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Steven Soroka"]
9
+ s.date = %q{2009-07-22}
10
+ s.email = %q{ssoroka78@gmail.com}
11
+ s.extra_rdoc_files = [
12
+ "LICENSE",
13
+ "README.rdoc"
14
+ ]
15
+ s.files = [
16
+ ".document",
17
+ ".gitignore",
18
+ "LICENSE",
19
+ "README.rdoc",
20
+ "Rakefile",
21
+ "VERSION.yml",
22
+ "app_settings.gemspec",
23
+ "example_config/example_for_gem.yml",
24
+ "example_config/example_for_rails.yml",
25
+ "init.rb",
26
+ "lib/app_settings.rb",
27
+ "spec/app_settings_spec.rb",
28
+ "spec/spec_helper.rb",
29
+ "tasks/app_settings.rake"
30
+ ]
31
+ s.homepage = %q{http://github.com/ssoroka/app_settings}
32
+ s.rdoc_options = ["--charset=UTF-8"]
33
+ s.require_paths = ["lib"]
34
+ s.rubygems_version = %q{1.3.3}
35
+ s.summary = %q{Simple wrapper for YAML config files for Rails apps and gems}
36
+ s.test_files = [
37
+ "spec/app_settings_spec.rb",
38
+ "spec/spec_helper.rb"
39
+ ]
40
+
41
+ if s.respond_to? :specification_version then
42
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
43
+ s.specification_version = 3
44
+
45
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
46
+ else
47
+ end
48
+ else
49
+ end
50
+ end
@@ -0,0 +1,8 @@
1
+ # ERB is allowed in this file. eg. test_value: <%= 3 * 12 %> Keep in mind, when
2
+ # using ERB, it's only evaluated ONCE when the application loads!
3
+ twitter:
4
+ user: frank
5
+ password: orange
6
+ sleep: 300
7
+ bitly:
8
+ api: abc123
@@ -0,0 +1,48 @@
1
+ # Settings in defaults: affect all environments. settings in other environments override the
2
+ # defaults setting erb is allowed in this file. eg. test_value: <%= 3 * 12 %> Keep in mind, when
3
+ # using erb, it's only evaluated ONCE when the application loads! (at least, in production).
4
+ defaults: &defaults
5
+ domains:
6
+ www: <%= ENV['TMPDIR'].to_s.match(/passenger/) ? 'AppSettings.local' : '0.0.0.0:3000' %>
7
+ mail: localhost
8
+ legacy: www.AppSettings.com
9
+ images: images.AppSettings.com
10
+
11
+ # settings can reference other settings like so:
12
+ from_address: help@AppSettings.com
13
+ attachment_path: public/images
14
+
15
+ # This is at least until we have a dedicated exception sender. Or always; whatever.
16
+ exception_sender: rails@AppSettings.com
17
+
18
+ items_per_page: 50
19
+
20
+ # git commit number that HEAD currently points to. Only evaluated once on server start
21
+ git_head: <%= `git log --abbrev=8 -1 --pretty=format:"%h"` %>
22
+
23
+ google_maps_api_keys:
24
+ 0.0.0.0: key here
25
+ localhost: key here
26
+
27
+ request_history_size: 4
28
+
29
+ development: &development
30
+ <<: *defaults
31
+
32
+ test:
33
+ <<: *defaults
34
+ domains:
35
+ www: 0.0.0.0:3001
36
+ mail: localhost
37
+ legacy: www.AppSettings.com
38
+ images: images.AppSettings.com
39
+
40
+ production: &production
41
+ <<: *defaults
42
+ google_maps_api_keys: # Each subdomain requires its own key, I do believe
43
+ www.AppSettings.com: key_here
44
+ domains:
45
+ www: www.AppSettings.com
46
+ mail: AppSettings.com
47
+ legacy: www1.AppSettings.com
48
+ images: images.AppSettings.com
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'app_settings'
@@ -0,0 +1,56 @@
1
+ require 'erb'
2
+ require 'yaml'
3
+
4
+ # see README.rdoc
5
+ class AppSettings
6
+ class MissingConfigFileException < Exception
7
+ def initialize(file)
8
+ border = "\n" << ('*' * 60) << "\n"
9
+ super("#{border} Config file #{file} does not exist. Please create it. #{border}")
10
+ end
11
+ end
12
+ attr_accessor :config_file, :stored_settings
13
+
14
+ # use the first config file passed in that exists. Nice for allowing users to override your default config
15
+ # defaults to "#{RAILS_ROOT}/config/app_settings.yml" in rails apps if no config filename is passed in
16
+ def initialize(*config_files)
17
+ @config_file = Array(config_files).detect{|f| File.exist?(f) }
18
+ @config_file ||= "#{RAILS_ROOT}/config/app_settings.yml" if defined?(RAILS_ROOT)
19
+ end
20
+
21
+ # allows $settings.key_name shortcut for simple structures.
22
+ def method_missing(m, *a, &b)
23
+ read_settings(m.to_s)
24
+ end
25
+
26
+ # supports $settings['twitter/api/keys/frank'] style settings. Wont blow up on nil keys
27
+ # if RAILS_ENV is defined, it'll look for the keys in the appropriate environment inside app_settings.yml
28
+ def [](k)
29
+ keys = k.to_s.split('/')
30
+ result = read_settings(keys.shift)
31
+ while result && keys.any?
32
+ result = result[keys.shift]
33
+ end
34
+ result
35
+ end
36
+
37
+ # immediately expires the settings caches
38
+ def reload!
39
+ @stored_settings &&= nil
40
+ end
41
+
42
+ protected
43
+ def read_settings(k)
44
+ unless @stored_settings
45
+ die_if_file_missing
46
+ @stored_settings = YAML::load(ERB.new(File.read(config_file)).result)
47
+ @stored_settings = @stored_settings[RAILS_ENV] if defined?(RAILS_ENV)
48
+ end
49
+
50
+ @stored_settings[k.to_s]
51
+ end
52
+
53
+ def die_if_file_missing
54
+ raise MissingConfigFileException.new(@config_file) if !@config_file || !File.exist?(@config_file)
55
+ end
56
+ end
@@ -0,0 +1,29 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "AppSettings" do
4
+ before(:each) do
5
+ @settings = AppSettings.new(File.join(File.dirname(__FILE__), %w(.. example_config/example_for_rails.yml)))
6
+ end
7
+
8
+ describe '[]' do
9
+ it "should support single keys" do
10
+ @settings['domains/legacy'].should == 'www.AppSettings.com'
11
+ end
12
+
13
+ it "should support nested keys" do
14
+ @settings[:items_per_page].should == 50
15
+ end
16
+
17
+ it "should not die on keys that don't exist" do
18
+ lambda {
19
+ @settings['a/b/c/d/e/f/g'].should be_nil
20
+ }.should_not raise_error
21
+ end
22
+ end
23
+
24
+ describe '.value' do
25
+ it "should work" do
26
+ @settings.domains['legacy'].should == 'www.AppSettings.com'
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,12 @@
1
+ require 'spec'
2
+
3
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
4
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
5
+ RAILS_ENV = 'test'
6
+
7
+ require 'app_settings'
8
+
9
+ Spec::Runner.configure do |config|
10
+ AppSettings::APP_SETTINGS_FILE = File.join(File.dirname(__FILE__), %w(.. example_config app_settings.yml))
11
+
12
+ end
@@ -0,0 +1,13 @@
1
+ namespace :app_settings do
2
+ desc "Install the default config file"
3
+ task :install_config => [:environment] do
4
+ dest_file = File.join(Rails.root, 'config', 'app_settings.yml')
5
+ if File.exist?(dest_file)
6
+ puts "#{dest_file} already exists. Remove it first if you want to overwrite it."
7
+ else
8
+ puts "installing default config file to #{dest_file}"
9
+ FileUtils.mv(File.join(File.dirname(__FILE__), %w(.. example_config example_for_rails.yml)), dest_file)
10
+ end
11
+ end
12
+ end
13
+
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ssoroka-app_settings
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Steven Soroka
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-07-22 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: ssoroka78@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - .document
27
+ - .gitignore
28
+ - LICENSE
29
+ - README.rdoc
30
+ - Rakefile
31
+ - VERSION.yml
32
+ - app_settings.gemspec
33
+ - example_config/example_for_gem.yml
34
+ - example_config/example_for_rails.yml
35
+ - init.rb
36
+ - lib/app_settings.rb
37
+ - spec/app_settings_spec.rb
38
+ - spec/spec_helper.rb
39
+ - tasks/app_settings.rake
40
+ has_rdoc: false
41
+ homepage: http://github.com/ssoroka/app_settings
42
+ post_install_message:
43
+ rdoc_options:
44
+ - --charset=UTF-8
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ version:
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ version:
59
+ requirements: []
60
+
61
+ rubyforge_project:
62
+ rubygems_version: 1.2.0
63
+ signing_key:
64
+ specification_version: 3
65
+ summary: Simple wrapper for YAML config files for Rails apps and gems
66
+ test_files:
67
+ - spec/app_settings_spec.rb
68
+ - spec/spec_helper.rb