nifty_settings 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f966a5e5af3ead20201cfc215e5154cf0969f42e
4
+ data.tar.gz: baa6d5a012e6b4b3b100e9d6a2662a106e97579a
5
+ SHA512:
6
+ metadata.gz: 97cd32b0a622cf22dd173b5335a82743b96114943674d129cac4734918fa44fb83c2288b5e101d4f20d967a53861fe5f7d0a87c888e0d922bf413b2d6f1eee2a
7
+ data.tar.gz: 0ee70c1cc0c164d2905aef9aec9eec3d221016af5dda8cd56f9dbd3e55900c56778dc3943c3da0ce061742584ffffb2147f6906e9d2bb0230f605a6255d841f2
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 kraut computing UG (haftungsbeschränkt)
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1 @@
1
+ # Settings
@@ -0,0 +1,9 @@
1
+ require 'nifty_settings/version'
2
+
3
+ module NiftySettings
4
+ end
5
+
6
+ require 'nifty_settings/settings'
7
+ require 'nifty_settings/railtie' if defined?(Rails)
8
+
9
+ ::Settings = NiftySettings::Settings unless defined?(::Settings)
@@ -0,0 +1,7 @@
1
+ module NiftySettings
2
+ class Railtie < Rails::Railtie
3
+ config.to_prepare do
4
+ NiftySettings::Settings.reset!
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,191 @@
1
+ require 'pathname'
2
+ require 'yaml'
3
+ require 'erb'
4
+
5
+ module NiftySettings
6
+ class Settings
7
+ def initialize(hash = {})
8
+ @hash = Hash.new { |h,k| h[k] = self.class.new }
9
+ hash.each_pair { |k, v| self[k] = v }
10
+ end
11
+
12
+ def to_hash
13
+ unpack_attr @hash
14
+ end
15
+
16
+ def []=(k, v)
17
+ @hash[k.to_sym] = normalize_attr(v)
18
+ end
19
+
20
+ def [](k)
21
+ @hash[k.to_sym]
22
+ end
23
+
24
+ def has?(key)
25
+ key = key.to_sym
26
+ @hash.has_key?(key) && @hash[key].present?
27
+ end
28
+
29
+ def fetch(key, default = nil)
30
+ has?(key) ? self[key] : default
31
+ end
32
+
33
+ def blank?
34
+ @hash.blank?
35
+ end
36
+
37
+ def present?
38
+ !blank?
39
+ end
40
+
41
+ def method_missing(name, *args, &blk)
42
+ name = name.to_s
43
+ key, modifier = name[0..-2], name[-1, 1]
44
+ case modifier
45
+ when '?'
46
+ has?(key)
47
+ when '='
48
+ send(:[]=, key, *args)
49
+ else
50
+ self[name]
51
+ end
52
+ end
53
+
54
+ def respond_to?(name, key = false)
55
+ true
56
+ end
57
+
58
+ class << self
59
+ def setup(value = nil, &blk)
60
+ @@setup_callback = (blk || value)
61
+ end
62
+
63
+ def settings_path
64
+ @@settings_path ||= root.join('config', 'settings.yml').to_s
65
+ end
66
+
67
+ def load_from_file
68
+ if !File.readable?(settings_path)
69
+ $stderr.puts "Unable to load settings from #{settings_path} - Please check it exists and is readable."
70
+ return {}
71
+ end
72
+ if env.nil?
73
+ $stderr.puts 'Could not determine environment. If not using Rails, please set RACK_ENV env variable.'
74
+ return {}
75
+ end
76
+ # Otherwise, try loading...
77
+ contents = File.read(settings_path)
78
+ contents = ERB.new(contents).result
79
+ contents = YAML.load(contents)
80
+ (contents['default'] || {}).deep_merge(contents[env] || {})
81
+ end
82
+
83
+ def default
84
+ @@default ||= new(load_from_file)
85
+ end
86
+
87
+ def reset!
88
+ @@default = nil
89
+ default # Force us to reload the settings
90
+ NiftySettings::Settings.setup_mailer! if defined?(ActionMailer)
91
+ # If a setup block is defined, call it post configuration.
92
+ @setup_callback.call if defined?(@setup_callback) && @setup_callback
93
+ true
94
+ end
95
+
96
+ def method_missing(name, *args, &blk)
97
+ default.send(name, *args, &blk)
98
+ end
99
+
100
+ def respond_to?(name, key = false)
101
+ true
102
+ end
103
+
104
+ def root
105
+ @root ||= defined?(Rails) ? Rails.root : Pathname.new(File.expand_path('.'))
106
+ end
107
+
108
+ def env
109
+ @env ||= defined?(Rails) ? Rails.env : ENV['RACK_ENV']
110
+ end
111
+
112
+ def ssl?
113
+ !disable_ssl? && (force_ssl? || env == 'production')
114
+ end
115
+
116
+ def ssl_protocol
117
+ ssl? ? "https" : "http"
118
+ end
119
+
120
+ # Sets up ActionMailer to use settings from Settings.
121
+ def setup_mailer!
122
+ return unless mailer?
123
+ s = mailer
124
+ ActionMailer::Base.default_url_options[:host] = s.host
125
+ ActionMailer::Base.delivery_method = s.delivery_method.to_sym
126
+ ActionMailer::Base.smtp_settings = s.smtp_settings.to_hash if s.smtp_settings?
127
+ ActionMailer::Base.sendmail_settings = s.sendmail_settings.to_hash if s.sendmail_settings?
128
+ ActionMailer::Base.default :from => s.from
129
+
130
+ # Setup sendgrid if present, sort of a faux-sendgrid helper.
131
+ if s.delivery_method.to_sym == :sendgrid
132
+ ActionMailer::Base.delivery_method = :smtp
133
+ ActionMailer::Base.smtp_settings = {
134
+ :address => "smtp.sendgrid.net",
135
+ :port => "25",
136
+ :authentication => :plain,
137
+ :user_name => s.sendgrid.username,
138
+ :password => s.sendgrid.password,
139
+ :domain => s.sendgrid.domain
140
+ }
141
+ end
142
+ end
143
+ end
144
+
145
+ private
146
+
147
+ def normalize_attr(value)
148
+ case value
149
+ when Hash
150
+ self.class.new(value)
151
+ when Array
152
+ value.map { |v| normalize_attr(v) }
153
+ else
154
+ value
155
+ end
156
+ end
157
+
158
+ def unpack_attr(value)
159
+ case value
160
+ when self.class
161
+ value.to_hash
162
+ when Hash
163
+ Hash.new.tap do |h|
164
+ value.each_pair { |k,v| h[k] = unpack_attr(v) }
165
+ end
166
+ when Array
167
+ value.map { |v| unpack_attr(v) }
168
+ else
169
+ value
170
+ end
171
+ end
172
+ end
173
+ end
174
+
175
+ # From activesupport/lib/active_support/core_ext/hash/deep_merge.rb
176
+ unless Hash.new.respond_to?(:deep_merge)
177
+ class Hash
178
+ def deep_merge(other_hash, &block)
179
+ self.dup.tap do |this_hash|
180
+ other_hash.each_pair do |k,v|
181
+ tv = this_hash[k]
182
+ if tv.is_a?(Hash) && v.is_a?(Hash)
183
+ this_hash[k] = tv.deep_merge(v, &block)
184
+ else
185
+ this_hash[k] = block && tv ? block.call(k, tv, v) : v
186
+ end
187
+ end
188
+ end
189
+ end
190
+ end
191
+ end
@@ -0,0 +1,3 @@
1
+ module NiftySettings
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,21 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ require 'nifty_settings/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = 'nifty_settings'
8
+ gem.version = NiftySettings::VERSION
9
+ gem.platform = Gem::Platform::RUBY
10
+ gem.authors = ['Manuel Meurer']
11
+ gem.email = 'manuel.meurer@gmail.com'
12
+ gem.summary = 'Settings'
13
+ gem.description = 'Settings'
14
+ gem.homepage = ''
15
+ gem.license = 'MIT'
16
+
17
+ gem.files = `git ls-files`.split($/)
18
+ gem.executables = gem.files.grep(%r(^bin/)).map { |f| File.basename(f) }
19
+ gem.test_files = gem.files.grep(%r(^(test|spec|features)/))
20
+ gem.require_paths = ['lib']
21
+ end
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nifty_settings
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Manuel Meurer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-08 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Settings
14
+ email: manuel.meurer@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - LICENSE.txt
20
+ - README.md
21
+ - lib/nifty_settings.rb
22
+ - lib/nifty_settings/railtie.rb
23
+ - lib/nifty_settings/settings.rb
24
+ - lib/nifty_settings/version.rb
25
+ - nifty_settings.gemspec
26
+ homepage: ''
27
+ licenses:
28
+ - MIT
29
+ metadata: {}
30
+ post_install_message:
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubyforge_project:
46
+ rubygems_version: 2.0.2
47
+ signing_key:
48
+ specification_version: 4
49
+ summary: Settings
50
+ test_files: []
51
+ has_rdoc: