backhead 0.1.0

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.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
@@ -0,0 +1,4 @@
1
+ ## 2012-12-13 v0.1.0
2
+
3
+ * specify engine configuration options
4
+ * set configuration options via environment variables
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in backhead.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Charles Lowell
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,36 @@
1
+ # Backhead
2
+
3
+ The "backhead" was where the the gauges and controls sat on the old steam engines. This
4
+ gem removes the tedium of defining rails engine configuration options
5
+
6
+ ![An Engine Backhead](http://upload.wikimedia.org/wikipedia/commons/thumb/c/c5/4017_Backhead_20040426.jpg/640px-4017_Backhead_20040426.jpg)
7
+
8
+ ## Usage
9
+
10
+ Include backhead into class. You can now define configuration options
11
+ for your engine.
12
+
13
+ class MyEngine < Rails::Engine
14
+ include Backhead
15
+
16
+ configuration :my_engine do
17
+ option :fast
18
+ option :efficient
19
+ end
20
+
21
+ # standard engine stuff
22
+ initializer 'my_engine.setup' do |app|
23
+ MyEngine.enable_nitrus_mode() if app.config.my_engine.fast
24
+ MyEngine.decrease_fuel_consumption() if app.config.my_engine.efficient
25
+ end
26
+ end
27
+
28
+ This will create two new configuration options that users can set in their environment files:
29
+ `config.my_engine.fast` and `config.my_engine.efficient`.
30
+
31
+ As a bonus, it will also allow users to set these configuration parameters via the environment
32
+ variables `MY_ENGINE_FAST` and `MY_ENGINE_EFFICIENT` if they so choose. This gives more baked in
33
+ flexibility to control the app and runtime rather than via source control.
34
+
35
+ Configuration options that are explicitly set during initialization will take precedence over
36
+ those specified by environment variables
@@ -0,0 +1,4 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'backhead/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "backhead"
8
+ gem.version = Backhead::VERSION
9
+ gem.authors = ["Charles Lowell"]
10
+ gem.email = ["cowboyd@thefrontside.net"]
11
+ gem.description = %q{removes the tedium of defining rails engine configuration options}
12
+ gem.summary = %q{specify which configuration options your engine supports. Optionally set them with environment variables}
13
+ gem.homepage = "https://github.com/cowboyd/backhead"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency 'rails', '>= 3'
21
+
22
+ gem.add_development_dependency 'rspec'
23
+ gem.add_development_dependency 'rspec-spies'
24
+ end
@@ -0,0 +1,31 @@
1
+ require "backhead/version"
2
+ require 'active_support/concern'
3
+ require 'rails/engine'
4
+
5
+ module Backhead
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ fail TypeError, "Backhead can only be included into subclasses of Rails::Railtie" unless self < Rails::Railtie
10
+ end
11
+
12
+ module ClassMethods
13
+ def configuration(name, &block)
14
+ options = @_current_configuration_options = Set.new
15
+ yield if block_given?
16
+ struct = Struct.new(*options.to_a).new
17
+ config.send("#{name}=", struct)
18
+ initializer "#{name}.setup" do |app|
19
+ options.each do |option|
20
+ if struct.send(option).nil?
21
+ struct.send("#{option}=", ENV["#{name}_#{option}".upcase])
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+ def option(name)
28
+ @_current_configuration_options << name.to_sym
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,3 @@
1
+ module Backhead
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,65 @@
1
+ require 'spec_helper'
2
+
3
+ describe Backhead do
4
+ it 'cannot be included into a non-railtie' do
5
+ expect {Class.new.send(:include, Backhead)}.to raise_error(TypeError)
6
+ end
7
+ describe 'included into a rails engine and configured' do
8
+ before do
9
+ @engine = Class.new(Rails::Engine)
10
+ @initializers = []
11
+ @engine.stub(:initializer) {|name, options, &body| @initializers << body}
12
+ @engine.class_eval do
13
+ include Backhead
14
+ configuration :foo do
15
+ option :bar
16
+ option :baz_bang
17
+ end
18
+ end
19
+ @app = mock('Rails App', :config => @engine.config)
20
+ @initialize = proc do
21
+ @initializers.each do |i|
22
+ i.call(@app)
23
+ end
24
+ end
25
+ end
26
+
27
+ describe "setting a configuration parameter" do
28
+ before do
29
+ @engine.config.foo.bar = 'bif'
30
+ end
31
+ it "actually sets it." do
32
+ @engine.config.foo.bar.should eql 'bif'
33
+ end
34
+ end
35
+
36
+ describe "when an environment variable is set" do
37
+ before do
38
+ ENV['FOO_BAZ_BANG'] = 'booyeah'
39
+ end
40
+ after do
41
+ ENV.delete 'FOO_BAZ_BANG'
42
+ end
43
+
44
+ describe 'when initialized' do
45
+ before do
46
+ @initialize.call
47
+ end
48
+ it 'sets the configuration parameter from the environment' do
49
+ @engine.config.foo.baz_bang.should eql 'booyeah'
50
+ end
51
+ end
52
+
53
+ describe 'but when initialized when option is set explicitly from ruby code' do
54
+ before do
55
+ @engine.config.foo.baz_bang = 'blort'
56
+ @initialize.call
57
+ end
58
+ it 'uses the value set from ruby code' do
59
+ @engine.config.foo.baz_bang.should eql 'blort'
60
+ end
61
+ end
62
+ end
63
+
64
+ end
65
+ end
@@ -0,0 +1,2 @@
1
+ require 'rspec-spies'
2
+ require 'backhead'
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: backhead
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Charles Lowell
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '3'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec-spies
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: removes the tedium of defining rails engine configuration options
63
+ email:
64
+ - cowboyd@thefrontside.net
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - Changelog.md
71
+ - Gemfile
72
+ - LICENSE.txt
73
+ - README.md
74
+ - Rakefile
75
+ - backhead.gemspec
76
+ - lib/backhead.rb
77
+ - lib/backhead/version.rb
78
+ - spec/backhead_spec.rb
79
+ - spec/spec_helper.rb
80
+ homepage: https://github.com/cowboyd/backhead
81
+ licenses: []
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 1.8.24
101
+ signing_key:
102
+ specification_version: 3
103
+ summary: specify which configuration options your engine supports. Optionally set
104
+ them with environment variables
105
+ test_files:
106
+ - spec/backhead_spec.rb
107
+ - spec/spec_helper.rb