rsyc 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 288017edc2b1dfedb6ef4c081f722f25ba3555e5
4
+ data.tar.gz: 27870a746683d9371f35fa80a840bd81eccd4fc3
5
+ SHA512:
6
+ metadata.gz: d3d62916b805c874c2363eb7c6afb13aee6918425fb6387821811e688931f834831aa7c554167c785ea0df74754d87b758b048b2db807161a5868fcde0ffc36f
7
+ data.tar.gz: 6c4241931571015dd9d8bd26001e8fd82b96cfca614ff7b7aa34cd1547afdada57131fa01e49c09458720ccf3d950b096b18308ca6ae4f20442e58e51e09b61d
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ doc/
2
+ .yardoc/
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source "https://rubygems.org"
2
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,50 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rsyc (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ coveralls (0.7.0)
10
+ multi_json (~> 1.3)
11
+ rest-client
12
+ simplecov (>= 0.7)
13
+ term-ansicolor
14
+ thor
15
+ diff-lcs (1.2.5)
16
+ docile (1.1.3)
17
+ mime-types (2.2)
18
+ multi_json (1.9.2)
19
+ rake (10.1.1)
20
+ rest-client (1.6.7)
21
+ mime-types (>= 1.16)
22
+ rspec (2.14.1)
23
+ rspec-core (~> 2.14.0)
24
+ rspec-expectations (~> 2.14.0)
25
+ rspec-mocks (~> 2.14.0)
26
+ rspec-core (2.14.8)
27
+ rspec-expectations (2.14.5)
28
+ diff-lcs (>= 1.1.3, < 2.0)
29
+ rspec-mocks (2.14.6)
30
+ simplecov (0.8.2)
31
+ docile (~> 1.1.0)
32
+ multi_json
33
+ simplecov-html (~> 0.8.0)
34
+ simplecov-html (0.8.0)
35
+ term-ansicolor (1.3.0)
36
+ tins (~> 1.0)
37
+ thor (0.18.1)
38
+ tins (1.0.1)
39
+ yard (0.8.7.3)
40
+
41
+ PLATFORMS
42
+ ruby
43
+
44
+ DEPENDENCIES
45
+ bundler
46
+ coveralls
47
+ rake
48
+ rspec
49
+ rsyc!
50
+ yard
data/README.md ADDED
@@ -0,0 +1,60 @@
1
+ # Rsyc [![Build Status](https://travis-ci.org/bsm/rsyc.png?branch=master)](https://travis-ci.org/bsm/rsyc) [![Coverage Status](https://coveralls.io/repos/bsm/rsyc/badge.png?branch=master)](https://coveralls.io/r/bsm/rsyc?branch=master)
2
+
3
+ Simplest-possible Rails application configuration accessor.
4
+
5
+ ## Usage
6
+
7
+ Include it in your Gemfile:
8
+
9
+ ```ruby
10
+ gem "rsyc"
11
+ ```
12
+
13
+ Create a config/app.yml with your configuration:
14
+
15
+ ```yaml
16
+ development:
17
+ redis: "http://localhost:6379"
18
+ nested:
19
+ user: me
20
+ pass: secret
21
+
22
+ production:
23
+ redis: "http://remote.service:27841"
24
+ nested:
25
+ user: real
26
+ pass: s3cret
27
+ ```
28
+
29
+ Use in in your Rails app:
30
+
31
+ ```ruby
32
+ Rails.application.config.app.redis # => "http://localhost:6379"
33
+ Rails.application.config.app[:redis] # => "http://localhost:6379"
34
+ Rails.application.config.app.nested.pass # => "secret"
35
+ ```
36
+
37
+ ## Licence
38
+
39
+ ```
40
+ Copyright (c) 2014 Black Square Media
41
+
42
+ Permission is hereby granted, free of charge, to any person obtaining
43
+ a copy of this software and associated documentation files (the
44
+ "Software"), to deal in the Software without restriction, including
45
+ without limitation the rights to use, copy, modify, merge, publish,
46
+ distribute, sublicense, and/or sell copies of the Software, and to
47
+ permit persons to whom the Software is furnished to do so, subject to
48
+ the following conditions:
49
+
50
+ The above copyright notice and this permission notice shall be
51
+ included in all copies or substantial portions of the Software.
52
+
53
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
54
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
55
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
56
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
57
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
58
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
59
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
60
+ ```
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ require 'rake'
2
+ require 'bundler/gem_tasks'
3
+
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ require 'yard'
8
+ require 'yard/rake/yardoc_task'
9
+ YARD::Rake::YardocTask.new
10
+
11
+ require 'coveralls/rake/task'
12
+ Coveralls::RakeTask.new
13
+ namespace :spec do
14
+ task coveralls: [:spec, 'coveralls:push']
15
+ end
16
+
17
+ desc 'Default: run specs.'
18
+ task :default => :spec
data/lib/rsyc.rb ADDED
@@ -0,0 +1,61 @@
1
+ require 'ostruct'
2
+ require 'yaml'
3
+ require 'erb'
4
+ require 'pathname'
5
+
6
+ module Rsyc
7
+
8
+ def self.new(*args)
9
+ Config.new(*args)
10
+ end
11
+
12
+ class Options < ::Hash
13
+
14
+ def initialize(hash)
15
+ super().update(hash)
16
+
17
+ keys.each do |key|
18
+ value = delete(key)
19
+ self[key.to_s] = case value when Hash then Options.new(value) else value end
20
+ end
21
+ end
22
+
23
+ def [](key)
24
+ super key.to_s
25
+ end
26
+
27
+ def fetch(key)
28
+ super key.to_s
29
+ end
30
+
31
+ def method_missing(name, *)
32
+ key?(name.to_s) ? fetch(name) : super
33
+ end
34
+
35
+ def respond_to_missing?(*)
36
+ key?(name.to_s) || super
37
+ end
38
+
39
+ end
40
+
41
+ class Config < Options
42
+
43
+ # @param [String] path configuration file path
44
+ # @param [String] env environment name
45
+ def initialize(path, env)
46
+ path = Pathname.new(path.to_s).expand_path
47
+ raise ArgumentError, "Unable to find a configuration in #{path}" unless path.exist?
48
+
49
+ conf = YAML.load(ERB.new(path.read).result)
50
+ raise ArgumentError, "Invalid configuration in #{path}" unless conf.is_a?(Hash)
51
+
52
+ conf = conf[env.to_s]
53
+ raise ArgumentError, "No configuration for '#{env}' found in #{path}" unless conf.is_a?(Hash)
54
+
55
+ super(conf)
56
+ end
57
+
58
+ end
59
+ end
60
+
61
+ require 'rsyc/railtie.rb' if defined?(Rails)
@@ -0,0 +1,10 @@
1
+ require "rails"
2
+ require "rsyc"
3
+
4
+ module Rsyc
5
+ class Railtie < Rails::Railtie
6
+ initializer "rsyc.init" do
7
+ Rails.application.config.app = Rsyc.new Rails.root.join('config', 'app.yml'), Rails.env
8
+ end
9
+ end
10
+ end
data/rsyc.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.required_ruby_version = '>= 1.9.1'
5
+ s.required_rubygems_version = ">= 1.8.0"
6
+
7
+ s.name = File.basename(__FILE__, '.gemspec')
8
+ s.summary = "Rsyc"
9
+ s.description = "A simple YAML configuration builder and accessor"
10
+ s.version = "0.1.0"
11
+
12
+ s.authors = ["Black Square Media"]
13
+ s.email = "info@blacksquaremedia.com"
14
+ s.homepage = "https://github.com/bsm/rsyc"
15
+ s.licenses = ["MIT"]
16
+
17
+ s.require_path = 'lib'
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.files = `git ls-files`.split("\n")
20
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
21
+
22
+ s.add_development_dependency "rake"
23
+ s.add_development_dependency "bundler"
24
+ s.add_development_dependency "rspec"
25
+ s.add_development_dependency "yard"
26
+ s.add_development_dependency "coveralls"
27
+ end
@@ -0,0 +1,8 @@
1
+ test:
2
+ simple: "value"
3
+ nested:
4
+ scope: "name"
5
+ url: "http://test.host.com/path?a=1"
6
+
7
+ other:
8
+ special: "value"
File without changes
@@ -0,0 +1,62 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rsyc::Config do
4
+
5
+ subject { described_class.new FIXTURE_PATH.join("app.yml"), "test" }
6
+
7
+ it 'should load configuration' do
8
+ subject.should be_a(Rsyc::Options)
9
+ end
10
+
11
+ it 'should fail when file is missing' do
12
+ -> {
13
+ described_class.new FIXTURE_PATH.join("missing.yml"), "test"
14
+ }.should raise_error(ArgumentError)
15
+ end
16
+
17
+ it 'should fail when config is missing' do
18
+ -> {
19
+ described_class.new FIXTURE_PATH.join("blank.yml"), "test"
20
+ }.should raise_error(ArgumentError)
21
+ end
22
+
23
+ it 'should fail when env is missing' do
24
+ -> {
25
+ described_class.new FIXTURE_PATH.join("app.yml"), "missing"
26
+ }.should raise_error(ArgumentError)
27
+ end
28
+
29
+ it 'should have accessors' do
30
+ subject.simple.should == "value"
31
+ subject[:simple].should == "value"
32
+ subject["simple"].should == "value"
33
+
34
+ -> { subject.missing }.should raise_error(NoMethodError)
35
+ subject[:missing].should be_nil
36
+ subject["missing"].should be_nil
37
+
38
+ subject.nested.should == {"scope"=>"name", "url"=>"http://test.host.com/path?a=1"}
39
+ subject.nested.url.should == "http://test.host.com/path?a=1"
40
+ subject.nested[:url].should == "http://test.host.com/path?a=1"
41
+ subject.nested["url"].should == "http://test.host.com/path?a=1"
42
+
43
+ -> { subject.nested.missing }.should raise_error(NoMethodError)
44
+ subject.nested[:missing].should be_nil
45
+ subject.nested["missing"].should be_nil
46
+ end
47
+
48
+ end
49
+
50
+ describe Rsyc::Options do
51
+
52
+ subject { described_class.new some: "value", nested: { "status" => :ok } }
53
+
54
+ it { should be_a(Hash) }
55
+ it { should == {"some"=>"value", "nested"=>{"status"=>:ok}} }
56
+
57
+ its(:some) { should == "value" }
58
+ its(["some"]) { should == "value" }
59
+ its([:some]) { should == "value" }
60
+ its(:nested) { should be_instance_of(described_class) }
61
+
62
+ end
@@ -0,0 +1,4 @@
1
+ require 'rspec'
2
+ require 'rsyc'
3
+
4
+ FIXTURE_PATH = Pathname.new File.expand_path("../fixtures", __FILE__)
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rsyc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Black Square Media
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: yard
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: coveralls
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: A simple YAML configuration builder and accessor
84
+ email: info@blacksquaremedia.com
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files: []
88
+ files:
89
+ - ".gitignore"
90
+ - Gemfile
91
+ - Gemfile.lock
92
+ - README.md
93
+ - Rakefile
94
+ - lib/rsyc.rb
95
+ - lib/rsyc/railtie.rb
96
+ - rsyc.gemspec
97
+ - spec/fixtures/app.yml
98
+ - spec/fixtures/wrong.yml
99
+ - spec/lib/rsyc_spec.rb
100
+ - spec/spec_helper.rb
101
+ homepage: https://github.com/bsm/rsyc
102
+ licenses:
103
+ - MIT
104
+ metadata: {}
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: 1.9.1
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: 1.8.0
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 2.2.2
122
+ signing_key:
123
+ specification_version: 4
124
+ summary: Rsyc
125
+ test_files: []
126
+ has_rdoc: