config_module 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -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
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in config_module.gemspec
4
+ gemspec
data/README.markdown ADDED
@@ -0,0 +1,35 @@
1
+ ConfigModule
2
+ =============
3
+
4
+ Load important configuration files into their own modules!
5
+
6
+ Usage
7
+ -----
8
+
9
+ Set up your module:
10
+
11
+ ```ruby
12
+ require 'config_module'
13
+
14
+ module ExampleConfig
15
+ extend ConfigModule
16
+
17
+ config_file './config/example.yml'
18
+ namespace Rails.env
19
+
20
+ module_function
21
+
22
+ def kanoodle
23
+ 'ka' + config.noodle
24
+ end
25
+ end
26
+ ```
27
+
28
+ Then use it:
29
+
30
+ ```ruby
31
+ ExampleConfig.foo #=> 'bar'
32
+ ExampleConfig.kanoodle #=> 'kaboom!'
33
+ ```
34
+
35
+ Done!
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'config_module/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "config_module"
8
+ gem.version = ConfigModule::VERSION
9
+ gem.authors = ["Anthony Cook"]
10
+ gem.email = ["anthonymichaelcook@gmail.com"]
11
+ gem.description = %q{Wrap a configuration file in a module for easy use throughout your application. Inspired by Rails.}
12
+ gem.summary = %q{Load important configuration files into their own modules!}
13
+ gem.homepage = "http://github.com/acook/config_module"
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
+ end
@@ -0,0 +1,3 @@
1
+ module ConfigModule
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,43 @@
1
+ require 'config_module/version'
2
+ require 'ostruct'
3
+ require 'yaml'
4
+
5
+ module ConfigModule
6
+ def [] key
7
+ config.send key
8
+ end
9
+
10
+ def config
11
+ @config ||= load_config
12
+ end
13
+
14
+ def config_file file
15
+ @config_file = file
16
+ end
17
+
18
+ def namespace name
19
+ @namespace = name
20
+ end
21
+
22
+ protected
23
+
24
+ def wrap data
25
+ if data.is_a? Hash then
26
+ ConfigOption.new data
27
+ else
28
+ data
29
+ end
30
+ end
31
+
32
+ def load_config
33
+ file = YAML.load_file(@config_file)
34
+ wrap @namespace ? (file[@namespace] || file[@namespace.to_sym]) : file
35
+ end
36
+
37
+ def method_missing name, *args, &block
38
+ wrap(config.send name, *args, &block) || super
39
+ end
40
+
41
+ class ConfigOption < OpenStruct
42
+ end
43
+ end
@@ -0,0 +1,6 @@
1
+ ---
2
+ :production:
3
+ :foo: bar
4
+ :noodle: boom!
5
+ :dictionary:
6
+ :configuration: An arrangement of elements in a particular form, figure, or combination.
@@ -0,0 +1,12 @@
1
+ module ExampleConfig
2
+ extend ConfigModule
3
+
4
+ config_file './config/example.yml'
5
+ namespace Rails.env
6
+
7
+ module_function
8
+
9
+ def kanoodle
10
+ 'ka' + config.noodle
11
+ end
12
+ end
data/test/mutest.rb ADDED
@@ -0,0 +1,112 @@
1
+ module Mutest
2
+ def spec description
3
+ print ' -- ', description
4
+
5
+ return puts(': ' + yellow('pending') + vspace) unless block_given?
6
+
7
+ begin
8
+ result = yield
9
+ rescue => result
10
+ end
11
+
12
+ print ': ', colorize(result, caller), "\n"
13
+ end
14
+
15
+ module Color
16
+
17
+ def colors
18
+ {
19
+ red: 1,
20
+ green: 2,
21
+ yellow: 3,
22
+ white: 7
23
+ }
24
+ end
25
+
26
+ def color hue, text = nil
27
+ esc("3#{colors[hue]};1") + "#{text}#{normal if text}"
28
+ end
29
+
30
+ def esc seq
31
+ "\e[#{seq}m"
32
+ end
33
+
34
+ def normal text=nil
35
+ esc(0) + text.to_s
36
+ end
37
+
38
+ def colorize result, source
39
+ if result == true then
40
+ green result
41
+ elsif result == false then
42
+ red result
43
+ elsif result.is_a? Exception then
44
+ [
45
+ red('Exception'), vspace,
46
+ hspace, 'Spec encountered an Exception ', newline,
47
+ hspace, 'in spec at ', source.first, vspace,
48
+ hspace, message(result), vspace,
49
+ white(trace result)
50
+ ].join
51
+ else
52
+ [
53
+ red('Unknown Result'), vspace,
54
+ hspace, 'Spec did not return a boolean value ', newline,
55
+ hspace, 'in spec at ', source.first, vspace,
56
+ hspace, red(classinfo(result)), result.inspect, newline
57
+ ].join
58
+ end
59
+ end
60
+
61
+ def trace error
62
+ error.backtrace.inject(String.new) do |text, line|
63
+ text << hspace + line + newline
64
+ end
65
+ end
66
+
67
+ def message error
68
+ red(classinfo error) + error.message
69
+ end
70
+
71
+ def classinfo object
72
+ "#{classify object} < #{superclass object}: "
73
+ end
74
+
75
+ def classify object
76
+ object.is_a?(Module) ? object : object.class
77
+ end
78
+
79
+ def superclass object
80
+ classify(object).superclass
81
+ end
82
+
83
+ def hspace
84
+ ' '
85
+ end
86
+
87
+ def vspace
88
+ newline + newline
89
+ end
90
+
91
+ def newline
92
+ $/
93
+ end
94
+
95
+ private
96
+
97
+ def method_missing name, *args, &block
98
+ if colors.keys.include? name then
99
+ color name, *args
100
+ else
101
+ super
102
+ end
103
+ end
104
+
105
+ end
106
+
107
+ private
108
+
109
+ include Color
110
+ end
111
+
112
+
data/test/test.rb ADDED
@@ -0,0 +1,20 @@
1
+ require_relative 'mutest'
2
+ extend Mutest
3
+
4
+ require_relative '../config_module'
5
+ module Rails; def self.env; 'production'; end; end
6
+ require_relative 'example_config'
7
+
8
+ Dir.chdir File.dirname(__FILE__)
9
+
10
+ spec 'modules extended with ConfigModule will load configurations' do
11
+ ExampleConfig.foo == 'bar'
12
+ end
13
+
14
+ spec 'modules extended with ConfigModule have "namespace" methods' do
15
+ ExampleConfig.methods.include? :namespace
16
+ end
17
+
18
+ spec 'nested hash values are properly wrapped' do
19
+ ExampleConfig.dictionary.class == ConfigModule::ConfigOption
20
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: config_module
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Anthony Cook
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-14 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Wrap a configuration file in a module for easy use throughout your application.
15
+ Inspired by Rails.
16
+ email:
17
+ - anthonymichaelcook@gmail.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - .gitignore
23
+ - Gemfile
24
+ - README.markdown
25
+ - Rakefile
26
+ - config_module.gemspec
27
+ - lib/config_module.rb
28
+ - lib/config_module/version.rb
29
+ - test/config/example.yml
30
+ - test/example_config.rb
31
+ - test/mutest.rb
32
+ - test/test.rb
33
+ homepage: http://github.com/acook/config_module
34
+ licenses: []
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ requirements: []
52
+ rubyforge_project:
53
+ rubygems_version: 1.8.25
54
+ signing_key:
55
+ specification_version: 3
56
+ summary: Load important configuration files into their own modules!
57
+ test_files:
58
+ - test/config/example.yml
59
+ - test/example_config.rb
60
+ - test/mutest.rb
61
+ - test/test.rb