rbrc 0.0.1

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.
data/README.md ADDED
@@ -0,0 +1,32 @@
1
+ # rbrc #
2
+
3
+ App config helpers for ruby apps.
4
+
5
+ ## Install ##
6
+
7
+ Install with gem
8
+
9
+ gem install rbrc
10
+
11
+ Or add to your Gemfile
12
+
13
+ gem 'rbrc'
14
+
15
+ Or get it from `master`
16
+
17
+ git clone 'https://github.com/mikebannister/rbrc
18
+ cd rbrc
19
+ gem install ./rbrc.gemspec
20
+
21
+ ## Usage ##
22
+
23
+ Register a config file somewhere in your app
24
+
25
+ require 'rbrc'
26
+
27
+ register_config(:my_app)
28
+
29
+ Now you can access the config file values like this
30
+
31
+ MyAppConfig.password #=> 'secret_password'
32
+
data/Rakefile ADDED
@@ -0,0 +1,26 @@
1
+ #!/usr/bin/env rake
2
+
3
+ begin
4
+ require 'bundler/setup'
5
+ rescue LoadError
6
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
7
+ end
8
+ begin
9
+ require 'rdoc/task'
10
+ rescue LoadError
11
+ require 'rdoc/rdoc'
12
+ require 'rake/rdoctask'
13
+ RDoc::Task = Rake::RDocTask
14
+ end
15
+
16
+ RDoc::Task.new(:rdoc) do |rdoc|
17
+ rdoc.rdoc_dir = 'rdoc'
18
+ rdoc.title = 'Importable'
19
+ rdoc.options << '--line-numbers'
20
+ rdoc.rdoc_files.include('README.rdoc')
21
+ rdoc.rdoc_files.include('lib/**/*.rb')
22
+ end
23
+
24
+ Bundler::GemHelper.install_tasks
25
+
26
+ task :default => 'spec'
data/lib/rbrc.rb ADDED
@@ -0,0 +1,8 @@
1
+ require 'rbrc/shortcut_class'
2
+ require 'rbrc/registry'
3
+ require 'rbrc/config'
4
+
5
+ module Rbrc
6
+ class ConfigFileDoesNotExistError < Exception
7
+ end
8
+ end
@@ -0,0 +1,31 @@
1
+ require 'active_support/inflector'
2
+ require 'parseconfig'
3
+
4
+ module Rbrc
5
+ class Config
6
+ def initialize(name)
7
+ @name = name
8
+ raise ConfigFileDoesNotExistError unless File.exist?(file_path)
9
+
10
+ init_shortcut_class
11
+ end
12
+
13
+ def file_path
14
+ @file_path ||= File.expand_path("~/.#{@name}rc")
15
+ end
16
+
17
+ def values
18
+ ParseConfig.new(file_path).params
19
+ end
20
+
21
+ def [](key)
22
+ values[key.to_s]
23
+ end
24
+
25
+ def init_shortcut_class
26
+ helper_name = "#{@name.to_s.camelize}Config".to_sym
27
+ shortcut_class = Class.new(Rbrc::ShortcutClass)
28
+ Object.const_set(helper_name, shortcut_class)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,14 @@
1
+ module Rbrc
2
+ class Registry
3
+ class << self
4
+ def register_config(name)
5
+ @configs ||= {}
6
+ @configs[name] = Config.new(name)
7
+ end
8
+
9
+ def [](name)
10
+ @configs[name] if @configs
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,26 @@
1
+ module Rbrc
2
+ class ShortcutClass
3
+ class << self
4
+ def method_missing(sym, *args, &block)
5
+ @sym = sym
6
+ if config_exists? and
7
+ return config[sym]
8
+ end
9
+ super(sym, *args, &block)
10
+ end
11
+
12
+ # get :Foo from FooConfig
13
+ def config_name
14
+ self.name.slice(0..-7).underscore.to_sym
15
+ end
16
+
17
+ def config
18
+ Registry[config_name]
19
+ end
20
+
21
+ def config_exists?
22
+ !!config
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,3 @@
1
+ module Rbrc
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,66 @@
1
+ require 'spec_helper'
2
+
3
+ module Rbrc
4
+ describe Config do
5
+ before(:each) do
6
+ # Note: FakeFS activated globally in spec_helper
7
+ FileUtils.mkdir_p File.expand_path('~')
8
+ FileUtils.touch File.expand_path('~/.foorc')
9
+ FileUtils.touch File.expand_path('~/.barrc')
10
+ end
11
+
12
+ describe "#initialize" do
13
+ it "should throw an error if the file doesn't exist" do
14
+ FileUtils.rm File.expand_path('~/.foorc')
15
+
16
+ lambda {
17
+ Config.new(:foo)
18
+ }.should raise_error Rbrc::ConfigFileDoesNotExistError
19
+ end
20
+
21
+ it "should activate a shortcut class for accessing config values globally" do
22
+ FakeFS.deactivate!
23
+ Config.any_instance.stubs(:file_path).returns('/Users/mike/work/rbrc/spec/files/foorc')
24
+
25
+ Registry.register_config :foo
26
+
27
+ FooConfig.should be_a ShortcutClass.class
28
+ end
29
+ end
30
+
31
+ describe "#file_path" do
32
+ it "should return the path to the config file" do
33
+ config = Config.new(:foo)
34
+ relative_path = config.file_path.split('/').last
35
+
36
+ relative_path.should eq '.foorc'
37
+ end
38
+ end
39
+
40
+ describe "#values" do
41
+ it "should return values from the config file" do
42
+ FakeFS.deactivate!
43
+ Config.any_instance.stubs(:file_path).returns('/Users/mike/work/rbrc/spec/files/foorc')
44
+
45
+ config = Config.new(:foo)
46
+ expected_values = {
47
+ 'moof' => 'doof',
48
+ 'foof' => 'toof'
49
+ }
50
+ config.values.should eq expected_values
51
+ end
52
+ end
53
+
54
+ describe "#[]" do
55
+ it "should return for specified config key" do
56
+ FakeFS.deactivate!
57
+ Config.any_instance.stubs(:file_path).returns('/Users/mike/work/rbrc/spec/files/foorc')
58
+
59
+ Registry.register_config :foo
60
+
61
+ FooConfig.moof.should == 'doof'
62
+ FooConfig.foof.should == 'toof'
63
+ end
64
+ end
65
+ end
66
+ end
data/spec/files/foorc ADDED
@@ -0,0 +1,2 @@
1
+ moof = doof
2
+ foof = toof
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ module Rbrc
4
+ describe Registry do
5
+ describe "#register_config" do
6
+ it "should add a config object to the registry for each registered config" do
7
+ # Note: FakeFS activated globally in spec_helper
8
+ FileUtils.mkdir_p File.expand_path('~')
9
+ FileUtils.touch File.expand_path('~/.foorc')
10
+ FileUtils.touch File.expand_path('~/.barrc')
11
+
12
+ Registry.register_config(:foo)
13
+ Registry.register_config(:bar)
14
+ Registry[:foo].should be_a Rbrc::Config
15
+ Registry[:bar].should be_a Rbrc::Config
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,17 @@
1
+ require 'fakefs/spec_helpers'
2
+
3
+ require File.expand_path("../../lib/rbrc", __FILE__)
4
+
5
+ RSpec.configure do |config|
6
+ config.mock_with :mocha
7
+ config.include FakeFS::SpecHelpers
8
+
9
+ config.before(:each) do
10
+ FakeFS.activate!
11
+ end
12
+
13
+ config.after(:each) do
14
+ FakeFS.deactivate!
15
+ FakeFS::FileSystem.clear
16
+ end
17
+ end
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rbrc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Mike Bannister
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-10-03 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: parseconfig
16
+ requirement: &70154352504120 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 0.5.2
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70154352504120
25
+ - !ruby/object:Gem::Dependency
26
+ name: activesupport
27
+ requirement: &70154352502920 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 3.1.0
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70154352502920
36
+ - !ruby/object:Gem::Dependency
37
+ name: i18n
38
+ requirement: &70154352501640 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 0.6.0
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *70154352501640
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: &70154352500820 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 2.6.0
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70154352500820
58
+ - !ruby/object:Gem::Dependency
59
+ name: guard-rspec
60
+ requirement: &70154352499360 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: 0.4.5
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *70154352499360
69
+ - !ruby/object:Gem::Dependency
70
+ name: fakefs
71
+ requirement: &70154352497460 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ version: 0.4.0
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *70154352497460
80
+ - !ruby/object:Gem::Dependency
81
+ name: mocha
82
+ requirement: &70154352495680 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ~>
86
+ - !ruby/object:Gem::Version
87
+ version: 0.10.0
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: *70154352495680
91
+ description: App config helpers for ruby apps.
92
+ email:
93
+ - mikebannister@gmail.com
94
+ executables: []
95
+ extensions: []
96
+ extra_rdoc_files: []
97
+ files:
98
+ - lib/rbrc/config.rb
99
+ - lib/rbrc/registry.rb
100
+ - lib/rbrc/shortcut_class.rb
101
+ - lib/rbrc/version.rb
102
+ - lib/rbrc.rb
103
+ - Rakefile
104
+ - README.md
105
+ - spec/config_spec.rb
106
+ - spec/files/foorc
107
+ - spec/registry_spec.rb
108
+ - spec/spec_helper.rb
109
+ homepage: https://github.com/mikebannister/rbrc
110
+ licenses: []
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ! '>='
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ segments:
122
+ - 0
123
+ hash: -2253627055374088253
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ none: false
126
+ requirements:
127
+ - - ! '>='
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ segments:
131
+ - 0
132
+ hash: -2253627055374088253
133
+ requirements: []
134
+ rubyforge_project:
135
+ rubygems_version: 1.8.10
136
+ signing_key:
137
+ specification_version: 3
138
+ summary: App config helpers for ruby apps.
139
+ test_files:
140
+ - spec/config_spec.rb
141
+ - spec/files/foorc
142
+ - spec/registry_spec.rb
143
+ - spec/spec_helper.rb