a9n 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,14 @@
1
+ *.gem
2
+ .bundle
3
+ .config
4
+ .yardoc
5
+ Gemfile.lock
6
+ _yardoc
7
+ coverage
8
+ doc/
9
+ lib/bundler/man
10
+ pkg
11
+ rdoc
12
+ spec/reports
13
+ tmp
14
+ nbproject/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+ gem 'rspec'
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 knapo
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.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # A9n
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'a9n'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install a9n
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
data/a9n.gemspec ADDED
@@ -0,0 +1,17 @@
1
+ # encoding: utf-8
2
+ require File.expand_path('../lib/a9n/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Krzysztof Knapik"]
6
+ gem.email = ["knapo@knapo.net"]
7
+ gem.description = %q{Simple tool for managing app configuration}
8
+ gem.summary = %q{Simple tool for managing app configuration}
9
+ gem.homepage = "https://github.com/knapo/a9n"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "a9n"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = A9n::VERSION
17
+ end
data/lib/a9n.rb ADDED
@@ -0,0 +1,56 @@
1
+ require 'a9n/version'
2
+ require 'a9n/store'
3
+
4
+ module A9n
5
+ class ConfigurationNotLoaded < StandardError; end
6
+ class MissingConfigurationFile < StandardError; end
7
+ class MissingConfigurationVariables < StandardError; end
8
+ class NoSuchConfigurationVariable < StandardError; end
9
+
10
+ class << self
11
+
12
+ def cfg
13
+ @@configuration
14
+ rescue NameError
15
+ raise ConfigurationNotLoaded.new("Configuration does not seem to be loaded. Plase call A9n.load.")
16
+ end
17
+
18
+ def local_app
19
+ @@local_app ||= Rails
20
+ end
21
+
22
+ def local_app=(local_app)
23
+ @@local_app = local_app
24
+ end
25
+
26
+ def load
27
+ base = load_yml('config/configuration.yml.example')
28
+ local = load_yml('config/configuration.yml')
29
+
30
+ if base.nil? && local.nil?
31
+ raise MissingConfigurationFile.new("Neither config/configuration.yml.example nor config/configuration.yml was found")
32
+ end
33
+
34
+ if !base.nil? && !local.nil?
35
+ verify!(base, local)
36
+ end
37
+
38
+ @@configuration = Store.new(local || base)
39
+ end
40
+
41
+ def load_yml(file)
42
+ path = File.join(local_app.root, file)
43
+ return unless File.exists?(path)
44
+ YAML.load_file(path)[local_app.env]
45
+ end
46
+
47
+ private
48
+
49
+ def verify!(base, local)
50
+ missing_keys = base.keys - local.keys
51
+ if missing_keys.any?
52
+ raise MissingConfigurationVariables.new("Following variables are missing in your configuration file: #{missing_keys.join(',')}")
53
+ end
54
+ end
55
+ end
56
+ end
data/lib/a9n/store.rb ADDED
@@ -0,0 +1,14 @@
1
+ require 'ostruct'
2
+
3
+ module A9n
4
+ class Store < OpenStruct
5
+ def method_missing(name, *args)
6
+ value = @table[name]
7
+ if value.nil?
8
+ raise NoSuchConfigurationVariable.new(name)
9
+ else
10
+ return value
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module A9n
2
+ VERSION = "0.0.1"
3
+ end
data/spec/a9n_spec.rb ADDED
@@ -0,0 +1,123 @@
1
+ require 'spec_helper'
2
+
3
+ describe A9n do
4
+ describe '.local_app' do
5
+ context 'when no rails found' do
6
+ it 'raises error' do
7
+ expect {
8
+ described_class.local_app
9
+ }.to raise_error(NameError, 'uninitialized constant Module::Rails')
10
+ end
11
+ end
12
+
13
+ context 'when custom non-rails app is being used' do
14
+ let(:local_app) { stub(:env => 'test', :root => 'local_app') }
15
+ before { described_class.local_app = local_app}
16
+
17
+ specify { described_class.local_app.should == local_app }
18
+ end
19
+ end
20
+
21
+ describe '.load' do
22
+ let(:base_sample_config){
23
+ { :app_url => 'http://127.0.0.1:3000' }
24
+ }
25
+ let(:local_sample_config){
26
+ { :app_host => '127.0.0.1:3000' }
27
+ }
28
+ subject { described_class.cfg }
29
+
30
+ context 'when no configuration file exists' do
31
+ before do
32
+ described_class.should_receive(:load_yml).with('config/configuration.yml.example').and_return(nil)
33
+ described_class.should_receive(:load_yml).with('config/configuration.yml').and_return(nil)
34
+ described_class.should_receive(:verify!).never
35
+ end
36
+ it 'raises expection' do
37
+ lambda {
38
+ described_class.load
39
+ }.should raise_error(described_class::MissingConfigurationFile)
40
+ end
41
+ end
42
+
43
+ context 'when base configuration file exists' do
44
+ before do
45
+ described_class.should_receive(:load_yml).with('config/configuration.yml.example').and_return(base_sample_config)
46
+ described_class.should_receive(:load_yml).with('config/configuration.yml').and_return(nil)
47
+ described_class.should_receive(:verify!).never
48
+ described_class.load
49
+ end
50
+ it { should_not be_nil }
51
+ its(:app_url) { should_not be_nil }
52
+ specify {
53
+ expect { subject.app_host }.to raise_error(described_class::NoSuchConfigurationVariable)
54
+ }
55
+ end
56
+
57
+ context 'when local configuration file exists' do
58
+ before do
59
+ described_class.should_receive(:load_yml).with('config/configuration.yml.example').and_return(nil)
60
+ described_class.should_receive(:load_yml).with('config/configuration.yml').and_return(local_sample_config)
61
+ described_class.should_receive(:verify!).never
62
+ described_class.load
63
+ end
64
+
65
+ it { should_not be_nil }
66
+ its(:app_host) { should_not be_nil }
67
+ specify {
68
+ expect { subject.app_url }.to raise_error(described_class::NoSuchConfigurationVariable)
69
+ }
70
+ end
71
+
72
+ context 'when both local and base configuration file exists' do
73
+ context 'with same data' do
74
+ before do
75
+ described_class.should_receive(:load_yml).with('config/configuration.yml.example').and_return(base_sample_config)
76
+ described_class.should_receive(:load_yml).with('config/configuration.yml').and_return(base_sample_config)
77
+ described_class.load
78
+ end
79
+ it { should_not be_nil }
80
+ its(:app_url) { should_not be_nil }
81
+ specify {
82
+ expect { subject.app_host }.to raise_error(described_class::NoSuchConfigurationVariable)
83
+ }
84
+ end
85
+
86
+ context 'with different data' do
87
+ before do
88
+ described_class.should_receive(:load_yml).with('config/configuration.yml.example').and_return(base_sample_config)
89
+ described_class.should_receive(:load_yml).with('config/configuration.yml').and_return(local_sample_config)
90
+ end
91
+ it 'raises expection' do
92
+ expect {
93
+ described_class.load
94
+ }.to raise_error(described_class::MissingConfigurationVariables)
95
+ end
96
+ end
97
+ end
98
+ end
99
+
100
+ describe '.load_yml' do
101
+ let(:root) { File.dirname(__FILE__) }
102
+ subject { described_class.load_yml(file_path) }
103
+
104
+ before do
105
+ described_class.should_receive(:local_app).at_least(:once).and_return(stub(:env => 'test', :root => root))
106
+ end
107
+
108
+ context 'when file not exists' do
109
+ let(:file_path) { 'file_not_existing_in_universe.yml' }
110
+ it 'returns nil' do
111
+ subject.should be_nil
112
+ end
113
+ end
114
+
115
+ context 'when file exists' do
116
+ let(:file_path) { 'fixtures/configuration.yml'}
117
+ it 'returns non-empty hash' do
118
+ subject.should be_kind_of(Hash)
119
+ subject.keys.should_not be_empty
120
+ end
121
+ end
122
+ end
123
+ end
@@ -0,0 +1,5 @@
1
+ development:
2
+ app_url: 'http://127.0.0.1:3000'
3
+
4
+ test:
5
+ app_url: 'http://127.0.0.1:3000'
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'a9n'
5
+ require 'a9n/store'
6
+
7
+ RSpec.configure do |config|
8
+ config.order = "random"
9
+ end
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ describe A9n::Store do
4
+ subject { described_class.new(:app_host => 'http://127.0.0.1:3000') }
5
+
6
+ it 'gets value' do
7
+ subject.app_host.should == 'http://127.0.0.1:3000'
8
+ end
9
+
10
+ it 'raises exception when value not exists' do
11
+ expect {
12
+ subject.non_existing
13
+ }.to raise_error(A9n::NoSuchConfigurationVariable)
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: a9n
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Krzysztof Knapik
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-05 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Simple tool for managing app configuration
15
+ email:
16
+ - knapo@knapo.net
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE
24
+ - README.md
25
+ - Rakefile
26
+ - a9n.gemspec
27
+ - lib/a9n.rb
28
+ - lib/a9n/store.rb
29
+ - lib/a9n/version.rb
30
+ - spec/a9n_spec.rb
31
+ - spec/fixtures/configuration.yml
32
+ - spec/spec_helper.rb
33
+ - spec/store_spec.rb
34
+ homepage: https://github.com/knapo/a9n
35
+ licenses: []
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubyforge_project:
54
+ rubygems_version: 1.8.24
55
+ signing_key:
56
+ specification_version: 3
57
+ summary: Simple tool for managing app configuration
58
+ test_files:
59
+ - spec/a9n_spec.rb
60
+ - spec/fixtures/configuration.yml
61
+ - spec/spec_helper.rb
62
+ - spec/store_spec.rb