scoped_choices 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
File without changes
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ The choice is yours
2
+ ===================
3
+
4
+ Easy-peasy external settings for your Rails app.
5
+
6
+ # Gemfile
7
+ gem 'scoped_choices'
8
+
9
+ In your app initializer block:
10
+
11
+ config.from_file 'settings.yml'
12
+
13
+ #to scope out your settings
14
+ # Rails.configuration.my_engine...
15
+ config.from_file_with_scope 'settings.yml', "my_engine"
16
+
17
+ 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.
18
+
19
+ # .gitignore
20
+ config/settings.local.yml
21
+
22
+ Configuration files can contain ERB; this is useful for reading in Heroku configuration. For example:
23
+
24
+ # settings.yml
25
+ defaults: &defaults
26
+ secret_token: <%= ENV['COOKIE_SECRET'] %>
27
+ heroku: <%= !!ENV['HEROKU_TYPE'] %>
28
+ mongodb:
29
+ uri: <%= ENV['MONGOHQ_URL'] %>
30
+
31
+ development:
32
+ <<: *defaults
33
+
34
+ test: &testing
35
+ <<: *defaults
36
+ secret_token: <%= "banana" * 5 %>
37
+ mongodb:
38
+ database: myapp_test
39
+
40
+ cucumber:
41
+ <<: *testing
42
+
43
+ The ".local" file can contain overrides for your development environment:
44
+
45
+ # settings.local.yml
46
+ development:
47
+ mongodb:
48
+ database: myapp_dev
49
+
50
+ Finally, the config keys can be read in your app as such:
51
+
52
+ Rails.configuration.heroku #=> false
53
+ Rails.configuration.mongodb.database #=> "myapp_dev"
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,55 @@
1
+ require 'scoped_choices'
2
+
3
+ module ScopedChoices::Rails
4
+ def self.included(base)
5
+ base.class_eval do
6
+ def initialize_with_scoped_choices(*args, &block)
7
+ initialize_without_scoped_choices(*args, &block)
8
+ @scoped_choices = Hashie::Mash.new
9
+ end
10
+
11
+ alias :initialize_without_scoped_choices :initialize
12
+ alias :initialize :initialize_with_scoped_choices
13
+ end
14
+ end
15
+
16
+ def from_file_with_scope(name, scope)
17
+ root = self.respond_to?(:root) ? self.root : Rails.root
18
+ file = root + 'config' + name
19
+
20
+ settings = ScopedChoices.load_settings(file, Rails.respond_to?(:env) ? Rails.env : RAILS_ENV)
21
+ scoped_settings = Hashie::Mash.new
22
+ scoped_settings.send("#{scope}=", settings)
23
+ settings = scoped_settings
24
+ @scoped_choices.update settings
25
+
26
+ settings.each do |key, value|
27
+ self.send("#{key}=", value)
28
+ end
29
+ end
30
+ end
31
+
32
+ if defined? Rails::Application::Configuration
33
+ Rails::Application::Configuration.send(:include, ScopedChoices::Rails)
34
+ elsif defined? Rails::Configuration
35
+ Rails::Configuration.class_eval do
36
+ include ScopedChoices::Rails
37
+ include Module.new {
38
+ def respond_to?(method)
39
+ super or method.to_s =~ /=$/ or (method.to_s =~ /\?$/ and @scoped_choices.key?($`))
40
+ end
41
+
42
+ private
43
+
44
+ def method_missing(method, *args, &block)
45
+ if method.to_s =~ /=$/ or (method.to_s =~ /\?$/ and @scoped_choices.key?($`))
46
+ @scoped_choices.send(method, *args)
47
+ elsif @scoped_choices.key?(method)
48
+ @scoped_choices[method]
49
+ else
50
+ super
51
+ end
52
+ end
53
+ }
54
+ end
55
+ end
@@ -0,0 +1,46 @@
1
+ require 'hashie/mash'
2
+ require 'erb'
3
+
4
+ module ScopedChoices
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
+
30
+ def yaml_load(content)
31
+ if defined? YAML::ENGINE
32
+ # avoid using broken Psych in 1.9.2
33
+ old_yamler = YAML::ENGINE.yamler
34
+ YAML::ENGINE.yamler = 'syck'
35
+ end
36
+ begin
37
+ YAML::load(content)
38
+ ensure
39
+ YAML::ENGINE.yamler = old_yamler if defined? YAML::ENGINE
40
+ end
41
+ end
42
+ end
43
+
44
+ if defined? Rails
45
+ require 'scoped_choices/rails'
46
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: scoped_choices
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Mislav Marohnić
9
+ - Samer Masry
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2012-03-02 00:00:00.000000000Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hashie
17
+ requirement: &70248575343200 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: 0.4.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *70248575343200
26
+ description: Based off Mislav's choices gem allows for scoping your configuration
27
+ email: samer@onekingslane.com
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - Rakefile
33
+ - lib/scoped_choices/rails.rb
34
+ - lib/scoped_choices.rb
35
+ - README
36
+ - README.md
37
+ homepage: http://github.com/okl/scoped_choices
38
+ licenses: []
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubyforge_project:
57
+ rubygems_version: 1.8.17
58
+ signing_key:
59
+ specification_version: 3
60
+ summary: Easy settings for your app
61
+ test_files: []