host_config 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +72 -0
  4. data/Rakefile +32 -0
  5. data/lib/host_config.rb +68 -0
  6. metadata +61 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fdaf7af07bea5b22be646a2d7c91f6d15cbc199d
4
+ data.tar.gz: d59f7eeeedc89cf2ec1e685750e971b792d67eb6
5
+ SHA512:
6
+ metadata.gz: b93f9736232f1805ccac535125f0ce5daac02928cfef931975d0e25fe6281c776bc1bd0feb32ffaf872b0fda155ca281771a6a182955bf7ebddf1a8bec81b73a
7
+ data.tar.gz: e5d05a7cf4c397e9b12191eb1dfbfdd2fdb4cd28499dadde7d574e1892eb42204736076df3dd7ec157fa8585026e78805dea2d7a505d15661e499a1ad0dff040
@@ -0,0 +1,20 @@
1
+ Copyright 2013 Ryan Funduk
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,72 @@
1
+ # HostConfig
2
+
3
+ Simple per-host configuration for Rails 4.
4
+
5
+
6
+ ## Setup/Usage
7
+
8
+ First add it to your `Gemfile`:
9
+
10
+ gem 'host_config'
11
+
12
+ Then make an initializer (or add it to one you already have, like so):
13
+
14
+ AppConfig = HostConfig.init!
15
+
16
+ That's really it. This will attempt to load `config/hosts/HOSTNAME.yml`
17
+ and assign an OpenStruct to `AppConfig` which you can then use all over
18
+ your app. You can also override the hostname:
19
+
20
+ override_hostname = Rails.env.production? ? 'aws' :
21
+ Rails.env.staging? ? 'staging' :
22
+ nil
23
+ AppConfig = HostConfig.init! hostname: override_hostname
24
+
25
+ ...or something similar, so that you can share config for production environments.
26
+ This is a good alternative to environment variables. You can handle your configuration
27
+ just like you already handle `database.yml` (eg, by not committing production config
28
+ to the git repo, but adding it to each host out of band).
29
+
30
+ You can also add more values to it for things that aren't host-specific:
31
+
32
+ AppConfig.analytics_id = 'UA-XXXXXXXX-Y'
33
+ AppConfig.sanitize = Sanitize::Config::RELAXED
34
+ AppConfig.sanitize[:attributes]['a'] = %w{ target href name }
35
+ AppConfig.sanitize[:add_attributes] = {
36
+ 'a' => { 'target' => '_blank' }
37
+ }
38
+
39
+ ...can be quite convenient.
40
+
41
+
42
+ ## Configuration
43
+
44
+ Configuration is in YAML files.
45
+
46
+ ---
47
+ force_ssl: true
48
+ protocol: https
49
+ application_host: myapp.com
50
+
51
+ twitter:
52
+ consumer: ABC
53
+ secret: XYZ
54
+
55
+ vegetables:
56
+ - broccoli
57
+ - carrots
58
+ - corn
59
+
60
+ As simple as that. You can also setup common stuff in files prefixed with `_`. Eg,
61
+ `_defaults.yml`, and load either before or after the stuff in the primary config:
62
+
63
+ ---
64
+ load_before:
65
+ - defaults
66
+ other_stuff: 'here'
67
+
68
+ This will look for `_defaults.yml` and deep-merge in the rest. You can also use
69
+ `load_after`, which will of course override values in the primary config... why would
70
+ you ever need this? I don't know :)
71
+
72
+ See the `test/dummy` app and the tests for the details if it isn't clear.
@@ -0,0 +1,32 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'HostConfig'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+ Bundler::GemHelper.install_tasks
21
+
22
+ require 'rake/testtask'
23
+
24
+ Rake::TestTask.new(:test) do |t|
25
+ t.libs << 'lib'
26
+ t.libs << 'test'
27
+ t.pattern = 'test/**/*_test.rb'
28
+ t.verbose = false
29
+ end
30
+
31
+
32
+ task default: :test
@@ -0,0 +1,68 @@
1
+ require 'ostruct'
2
+ require 'socket'
3
+
4
+ module HostConfig
5
+ VERSION = "0.0.1"
6
+
7
+ class MissingConfigFile < StandardError; end
8
+
9
+ class << self
10
+ def init!( opts={} )
11
+ @logger = opts[:logger] || Rails.logger
12
+ @hostname = opts[:hostname] || Socket.gethostname.split('.').shift
13
+ @config = {}
14
+ @config[:hostname] = opts[:hostname]
15
+ @logger.debug "Loading host config #{@hostname}"
16
+ process( load_config( @hostname ) )
17
+ end
18
+
19
+ private
20
+
21
+ def load_config( file )
22
+ begin
23
+ path = "#{Rails.root}/config/hosts/#{file}.yml"
24
+ data = File.read(path)
25
+ YAML.load( data )
26
+ rescue Errno::ENOENT
27
+ raise MissingConfigFile.new( "Missing config file '#{path}', could not setup HostConfig!" )
28
+ end
29
+ end
30
+
31
+ def process( config )
32
+ load_before = config.delete('load_before')
33
+ load_after = config.delete('load_after')
34
+
35
+ if load_before.is_a? Array
36
+ load_before.each do |file|
37
+ @logger.debug " including... _#{file}"
38
+ c = load_config( "_#{file}" )
39
+ process( c )
40
+ end
41
+ end
42
+
43
+ @config.deep_merge! config
44
+
45
+ if load_after.is_a? Array
46
+ load_after.each do |file|
47
+ c = load_config( "_#{file}" )
48
+ process( c )
49
+ end
50
+ end
51
+
52
+ to_openstruct(@config)
53
+ end
54
+
55
+ def to_openstruct( obj )
56
+ case obj
57
+ when Array
58
+ obj.map { |el| to_openstruct(el) }
59
+ when Hash
60
+ ::OpenStruct.new( obj.each_with_object({}) do |(k,v), h|
61
+ h[k] = to_openstruct(v)
62
+ end )
63
+ else
64
+ obj
65
+ end
66
+ end
67
+ end
68
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: host_config
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ryan Funduk
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 4.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 4.0.0
27
+ description: ''
28
+ email:
29
+ - ryan.funduk@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - lib/host_config.rb
35
+ - MIT-LICENSE
36
+ - Rakefile
37
+ - README.md
38
+ homepage: https://github.com/rfunduk/host_config
39
+ licenses: []
40
+ metadata: {}
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubyforge_project:
57
+ rubygems_version: 2.0.2
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: Simple per-host configuration for Rails 4.
61
+ test_files: []