choices 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.md +49 -0
  2. data/Rakefile +2 -0
  3. data/lib/choices.rb +40 -0
  4. metadata +84 -0
@@ -0,0 +1,49 @@
1
+ The choice is yours
2
+ ===================
3
+
4
+ Easy-peasy external settings for your Rails app.
5
+
6
+ # Gemfile
7
+ gem 'choices'
8
+
9
+ In your app initializer block:
10
+
11
+ config.from_file 'settings.yml'
12
+
13
+ This will read configuration from "config/settings.yml" and, additionally, "settings.local.yml" if it exists. You should check the main file into version control, but not the ".local" file which is to be used for per-machine configuration: tweaks in development or private keys in production, for example.
14
+
15
+ # .gitignore
16
+ config/settings.local.yml
17
+
18
+ Configuration files can contain ERB; this is useful for reading in Heroku configuration. For example:
19
+
20
+ # settings.yml
21
+ defaults: &defaults
22
+ secret_token: <%= ENV['COOKIE_SECRET'] %>
23
+ heroku: <%= !!ENV['HEROKU_TYPE'] %>
24
+ mongodb:
25
+ uri: <%= ENV['MONGOHQ_URL'] %>
26
+
27
+ development:
28
+ <<: *defaults
29
+
30
+ test: &testing
31
+ <<: *defaults
32
+ secret_token: <%= "banana" * 5 %>
33
+ mongodb:
34
+ database: myapp_test
35
+
36
+ cucumber:
37
+ <<: *testing
38
+
39
+ The ".local" file can contain overrides for your development environment:
40
+
41
+ # settings.local.yml
42
+ development:
43
+ mongodb:
44
+ database: myapp_dev
45
+
46
+ Finally, the config keys can be read in your app as such:
47
+
48
+ Rails.configuration.heroku #=> false
49
+ Rails.configuration.mongodb.database #=> "myapp_dev"
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,40 @@
1
+ require 'hashie/mash'
2
+ require 'erb'
3
+
4
+ module Choices
5
+ extend self
6
+
7
+ def load_settings(filename, env)
8
+ mash = Hashie::Mash.new(load_settings_hash(filename, env))
9
+
10
+ with_local_settings(filename, env, '.local') do |local|
11
+ mash.update local
12
+ end
13
+
14
+ return mash
15
+ end
16
+
17
+ def load_settings_hash(filename, env)
18
+ yaml_content = ERB.new(IO.read(filename)).result
19
+ YAML::load(yaml_content)[env]
20
+ end
21
+
22
+ def with_local_settings(filename, env, suffix)
23
+ local_filename = filename.sub(/(\.\w+)?$/, "#{suffix}\\1")
24
+ if File.exists? local_filename
25
+ hash = load_settings_hash(local_filename, env)
26
+ yield hash if hash
27
+ end
28
+ end
29
+ end
30
+
31
+ if defined? Rails::Application::Configuration
32
+ Rails::Application::Configuration.class_eval do
33
+ def from_file(name)
34
+ file = self.root + 'config' + name
35
+ Choices.load_settings(file, Rails.env.to_s).each do |key, value|
36
+ self.send("#{key}=", value)
37
+ end
38
+ end
39
+ end
40
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: choices
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - "Mislav Marohni\xC4\x87"
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-10-23 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: hashie
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 15
30
+ segments:
31
+ - 0
32
+ - 4
33
+ - 0
34
+ version: 0.4.0
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ description:
38
+ email: mislav.marohnic@gmail.com
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files: []
44
+
45
+ files:
46
+ - Rakefile
47
+ - lib/choices.rb
48
+ - README.md
49
+ has_rdoc: false
50
+ homepage: http://github.com/mislav/choices
51
+ licenses: []
52
+
53
+ post_install_message:
54
+ rdoc_options: []
55
+
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ hash: 3
64
+ segments:
65
+ - 0
66
+ version: "0"
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ hash: 3
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ requirements: []
77
+
78
+ rubyforge_project:
79
+ rubygems_version: 1.3.7
80
+ signing_key:
81
+ specification_version: 3
82
+ summary: Easy settings for your app
83
+ test_files: []
84
+