choices_settings 0.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3a6de46fadb0ee79f77c7b253f581c4edc118a33168081f639106c952dacda05
4
+ data.tar.gz: c89291d37abc73bfcda15fdf2636900701cd0a226861a95a77f76a43f5e427b6
5
+ SHA512:
6
+ metadata.gz: 7dad7ea354ba9f0891422a281c2496df38ace7056229b12f11839660fef6dbd6fdf2d47086c70fc01fb816c8990e6809a7583b3e3e44e7c128c94e9ef86c4e23
7
+ data.tar.gz: 88f79228e9803902606b98b855a84811cef9536ee17151f9e30da70a22b54377025c0eaa40dd52884738ba7f54c69e6866af6942068bf43f40c8d30aeefa6d20
data/MIT-LICENSE ADDED
@@ -0,0 +1,18 @@
1
+ Copyright (c) 2013 Mislav Marohnić
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ this software and associated documentation files (the "Software"), to deal in
5
+ the Software without restriction, including without limitation the rights to
6
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
7
+ the Software, and to permit persons to whom the Software is furnished to do so,
8
+ subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
15
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,67 @@
1
+ ChoicesSettings for Rails _(Previously [Choices](https://github.com/mislav/choices))_
2
+ =================
3
+
4
+ _Forked from [mislav/choices](https://github.com/mislav/choices)_
5
+
6
+ Easy-peasy external settings for your Rails app.
7
+
8
+ ~~~ rb
9
+ # Gemfile
10
+ gem 'choices_settings'
11
+ ~~~
12
+
13
+ In your app initializer block:
14
+
15
+ ~~~ rb
16
+ config.from_file 'settings.yml'
17
+ ~~~
18
+
19
+ This will read configuration from "config/settings.yml" and, additionally,
20
+ "settings.local.yml" if it exists. You should check the main file into version
21
+ control, but not the ".local" file which is to be used for per-machine
22
+ configuration: tweaks in development or private keys in production, for example.
23
+
24
+ ~~~
25
+ # .gitignore
26
+ config/settings.local.yml
27
+ ~~~
28
+
29
+ Configuration files can contain ERB; this is useful for reading in dynamic
30
+ config such as from environment variables:
31
+
32
+ ~~~ yaml
33
+ # settings.yml
34
+ defaults: &defaults
35
+ secret_token: <%= ENV['COOKIE_SECRET'] %>
36
+ heroku: <%= !!ENV['HEROKU_TYPE'] %>
37
+ mongodb:
38
+ uri: <%= ENV['MONGOHQ_URL'] %>
39
+
40
+ development:
41
+ <<: *defaults
42
+
43
+ test: &testing
44
+ <<: *defaults
45
+ secret_token: <%= "banana" * 5 %>
46
+ mongodb:
47
+ database: myapp_test
48
+
49
+ cucumber:
50
+ <<: *testing
51
+ ~~~
52
+
53
+ The ".local" file can contain overrides for your development environment:
54
+
55
+ ~~~ yaml
56
+ # settings.local.yml
57
+ development:
58
+ mongodb:
59
+ database: myapp_dev
60
+ ~~~
61
+
62
+ Finally, the config keys can be read in your app as such:
63
+
64
+ ~~~ rb
65
+ Rails.configuration.heroku #=> false
66
+ Rails.configuration.mongodb.database #=> "myapp_dev"
67
+ ~~~
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler'
2
+ require 'rake/testtask'
3
+
4
+ Bundler::GemHelper.install_tasks
5
+
6
+ Rake::TestTask.new do |t|
7
+ end
8
+
9
+ desc 'Run test suite'
10
+ task :default => :test
@@ -0,0 +1,67 @@
1
+ require 'hashie/mash'
2
+ require 'choices_settings'
3
+
4
+ module ChoicesSettings::Rails
5
+ def self.included(base)
6
+ base.class_eval do
7
+ def initialize_with_choices(*args, &block)
8
+ initialize_without_choices(*args, &block)
9
+ @choices_settings = Hashie::Mash.new
10
+ end
11
+
12
+ alias_method :initialize_without_choices, :initialize
13
+ alias_method :initialize, :initialize_with_choices
14
+ end
15
+ end
16
+
17
+ def from_file(name)
18
+ root = respond_to?(:root) ? self.root : Rails.root
19
+ file = "#{root}/config/#{name}"
20
+
21
+ settings = ChoicesSettings.load_settings(file, Rails.respond_to?(:env) ? Rails.env : RAILS_ENV)
22
+ @choices_settings.update settings
23
+
24
+ settings.each do |key, value|
25
+ old_value = respond_to?(key) ? send(key) : nil
26
+
27
+ if 'Rails::OrderedOptions' == old_value.class.name
28
+ # convert from Array to a real Hash
29
+ old_value = old_value.each_with_object({}) do |(k, v), h|
30
+ h[k] = v
31
+ end
32
+ end
33
+
34
+ if value.is_a?(Hash) and old_value.is_a?(Hash)
35
+ # don't overwrite existing Hash values; deep update them
36
+ value = Hashie::Mash.new(old_value).update value
37
+ end
38
+
39
+ send("#{key}=", value)
40
+ end
41
+ end
42
+ end
43
+
44
+ if defined? Rails::Engine::Configuration
45
+ Rails::Engine::Configuration.include ChoicesSettings::Rails
46
+ elsif defined? Rails::Configuration
47
+ Rails::Configuration.class_eval do
48
+ include ChoicesSettings::Rails
49
+ include(Module.new do
50
+ def respond_to?(method)
51
+ super or method.to_s =~ /=$/ or (method.to_s =~ /\?$/ and @choices_settings.key?(Regexp.last_match.pre_match))
52
+ end
53
+
54
+ private
55
+
56
+ def method_missing(method, *args, &block)
57
+ if method.to_s =~ /=$/ or (method.to_s =~ /\?$/ and @choices_settings.key?(Regexp.last_match.pre_match))
58
+ @choices_settings.send(method, *args)
59
+ elsif @choices_settings.key?(method)
60
+ @choices_settings[method]
61
+ else
62
+ super
63
+ end
64
+ end
65
+ end)
66
+ end
67
+ end
@@ -0,0 +1,47 @@
1
+ require 'hashie/mash'
2
+ require 'erb'
3
+ require 'yaml'
4
+
5
+ module ChoicesSettings
6
+ extend self
7
+
8
+ def load_settings(filename, env)
9
+ mash = Hashie::Mash.new(load_settings_hash(filename))
10
+
11
+ with_local_settings(filename, '.local') do |local|
12
+ mash.update local
13
+ end
14
+
15
+ mash.fetch(env) do
16
+ raise IndexError, %(Missing key for "#{env}" in `#{filename}')
17
+ end
18
+ end
19
+
20
+ def load_settings_hash(filename)
21
+ yaml_content = ERB.new(IO.read(filename)).result
22
+ yaml_load(yaml_content)
23
+ end
24
+
25
+ def with_local_settings(filename, suffix)
26
+ local_filename = filename.sub(/(\.\w+)?$/, "#{suffix}\\1")
27
+ return unless File.exist? local_filename
28
+
29
+ hash = load_settings_hash(local_filename)
30
+ yield hash if hash
31
+ end
32
+
33
+ def yaml_load(content)
34
+ if defined?(YAML::ENGINE) && defined?(Syck)
35
+ # avoid using broken Psych in 1.9.2
36
+ old_yamler = YAML::ENGINE.yamler
37
+ YAML::ENGINE.yamler = 'syck'
38
+ end
39
+ begin
40
+ YAML.load(content, aliases: true)
41
+ ensure
42
+ YAML::ENGINE.yamler = old_yamler if defined?(YAML::ENGINE) && defined?(Syck)
43
+ end
44
+ end
45
+ end
46
+
47
+ require 'choices_settings/rails' if defined? Rails
@@ -0,0 +1,5 @@
1
+ development:
2
+ name: 'Development'
3
+
4
+ production:
5
+ name: 'Production local'
@@ -0,0 +1,6 @@
1
+ defaults: &defaults
2
+ name: 'Defaults'
3
+
4
+ production:
5
+ <<: *defaults
6
+
@@ -0,0 +1,13 @@
1
+ defaults: &defaults
2
+ name: 'Defaults'
3
+
4
+ development:
5
+ name: 'Development'
6
+
7
+ production:
8
+ <<: *defaults
9
+ name: 'Production'
10
+
11
+ test:
12
+ name: 'Test'
13
+
@@ -0,0 +1,32 @@
1
+ gem 'minitest'
2
+ require 'minitest/autorun'
3
+ require 'choices_settings'
4
+ require 'pathname'
5
+
6
+ describe ChoicesSettings do
7
+ before do
8
+ path = Pathname.new(__FILE__)
9
+ @without_local = path + '../settings/without_local.yml'
10
+ @with_local = path + '../settings/with_local.yml'
11
+ end
12
+
13
+ describe 'when loading a settings file' do
14
+ it 'should return a mash for the specified environment' do
15
+ ChoicesSettings.load_settings(@without_local, 'defaults').name.must_equal 'Defaults'
16
+ ChoicesSettings.load_settings(@without_local, 'production').name.must_equal 'Production'
17
+ end
18
+
19
+ it 'should load the local settings' do
20
+ ChoicesSettings.load_settings(@with_local, 'defaults').name.must_equal 'Defaults'
21
+ ChoicesSettings.load_settings(@with_local, 'development').name.must_equal 'Development'
22
+ ChoicesSettings.load_settings(@with_local, 'production').name.must_equal 'Production local'
23
+ end
24
+
25
+ it 'should raise an exception if the environment does not exist' do
26
+ error = lambda {
27
+ ChoicesSettings.load_settings(@with_local, 'nonexistent')
28
+ }.must_raise(IndexError)
29
+ error.message.must_equal %(Missing key for "nonexistent" in `#{@with_local}')
30
+ end
31
+ end
32
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: choices_settings
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.2
5
+ platform: ruby
6
+ authors:
7
+ - Mislav Marohnić
8
+ - MonClubSportif
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2025-05-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: hashie
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: 5.0.0
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: 5.0.0
28
+ - !ruby/object:Gem::Dependency
29
+ name: minitest
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: 5.0.6
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: 5.0.6
42
+ description: Forked from mislav/choices (https://github.com/mislav/choices)
43
+ email: info@monclubsportif.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - MIT-LICENSE
49
+ - README.md
50
+ - Rakefile
51
+ - lib/choices_settings.rb
52
+ - lib/choices_settings/rails.rb
53
+ - test/settings/with_local.local.yml
54
+ - test/settings/with_local.yml
55
+ - test/settings/without_local.yml
56
+ - test/test_load_settings.rb
57
+ homepage: https://github.com/monclubsportif/choices_settings
58
+ licenses:
59
+ - MIT
60
+ metadata: {}
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '2.0'
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubygems_version: 3.5.23
77
+ signing_key:
78
+ specification_version: 4
79
+ summary: Easy settings for your app
80
+ test_files:
81
+ - test/test_load_settings.rb