easy_configs 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.gitignore +8 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +16 -0
- data/MIT-LICENSE +20 -0
- data/README.md +82 -0
- data/Rakefile +16 -0
- data/easy_config.gemspec +21 -0
- data/lib/easy_config.rb +15 -0
- data/lib/easy_config/deep_struct.rb +26 -0
- data/lib/easy_config/version.rb +3 -0
- data/lib/generators/easy_config/install/install_generator.rb +23 -0
- data/lib/generators/easy_config/install/templates/easy_config.rb +5 -0
- data/lib/generators/easy_config/install/templates/easy_config.yml +16 -0
- data/spec/easy_config_spec.rb +42 -0
- data/spec/fixtures/basic.yml +4 -0
- data/spec/fixtures/erbiefed.yml +4 -0
- data/spec/fixtures/namespaced.yml +11 -0
- data/spec/helper.rb +5 -0
- data/spec/lib/deep_struct_spec.rb +32 -0
- metadata +68 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
MjkwNWNiMGNkZGZiOTFhMTU1NmRlMjQ0MWRhZDk0ZGViYzY0YWZlNg==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
MjViNmY1MzBmMWM4ZDRiOWQ5MzQ4MmI2MDRhMjZkNDMwMjMwM2EwMA==
|
7
|
+
!binary "U0hBNTEy":
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
YTk3ZDllZWY2YmY2NzcyMzAxN2I1ZWU2Mjk5MzBjYjIyNmRhNDVmMGJjYzdi
|
10
|
+
NjNhMGNkODUxNTg4Nzc3NDE3M2QxMGM4NDg3YjRlOWJmOGUwMjAzMjcyOTcx
|
11
|
+
NTYxZjkzMzYwODZiYmQyOTU1ZDZiNjUwNGIxMmRkODE3MWI4Yjk=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
NGVlMzE2OGY5M2Y0OTQ2Y2MyY2QzYTU5OTlkNDRmZmVkMjc0YmE3Yzk1MDg5
|
14
|
+
YjdjZjQzZjlmOTZkNTM0YTgwYzgxNWM1MGFkZjIxMGZlMWIyNDQ1M2IzNDZl
|
15
|
+
ZjhmNzJkOTEyMDlmNjYyMWFiYjEzNjhjZGY2NGM5YTI4NTRjODM=
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2013 Andrea Pavoni - http://andreapavoni.com
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
# EasyConfig
|
2
|
+
|
3
|
+
A simple gem to store custom app settings the easiest way. It can be used in any
|
4
|
+
Ruby *or* Rails application.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```gem 'easy_configs'```
|
11
|
+
|
12
|
+
then execute:
|
13
|
+
|
14
|
+
```$ bundle install```
|
15
|
+
|
16
|
+
or install it yourself as:
|
17
|
+
|
18
|
+
```$ gem install easy_configs```
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
EasyConfig loads a YAML file (with eventual ERB tags, so you can use ```ENV``` variables).
|
23
|
+
For example, given a YAML file with a bunch of keys:
|
24
|
+
|
25
|
+
```
|
26
|
+
foo:
|
27
|
+
bar: 1
|
28
|
+
env: <%= ENV['USER'] %>
|
29
|
+
baz:
|
30
|
+
nested: 2
|
31
|
+
```
|
32
|
+
|
33
|
+
Here's how to use it:
|
34
|
+
|
35
|
+
```
|
36
|
+
# Load yaml file
|
37
|
+
MyConfig = EasyConfig.load_yaml 'path_to_yaml.yml'
|
38
|
+
|
39
|
+
# Retrieve keys
|
40
|
+
MyConfig.bar # => 1
|
41
|
+
MyConfig.env # => YOUR_USERNAME
|
42
|
+
|
43
|
+
# Retuns a nested object (EasyConfig::DeepStruct is a modified version of Ruby's OpenStruct)
|
44
|
+
MyConfig.baz # => <#EasyConfig::DeepStruct>
|
45
|
+
|
46
|
+
# Access a nested key
|
47
|
+
MyConfig.baz.nested # => 2
|
48
|
+
|
49
|
+
# Returns an Hash
|
50
|
+
MyConfig.to_h # => {foo: {bar: 1, env: ..., baz: {nested: 2} }}
|
51
|
+
|
52
|
+
```
|
53
|
+
|
54
|
+
## Rails support
|
55
|
+
|
56
|
+
EasyConfig has built-in support for Rails. It provides a generator to create an initializer and a sample config:
|
57
|
+
|
58
|
+
* run the generator
|
59
|
+
|
60
|
+
```rails generate easy_config:install```
|
61
|
+
* edit the sample configuration in ```config/easy_config.yml```
|
62
|
+
|
63
|
+
You might want to modify ```config/initializers/easy_config.rb``` to choose another name for the object that should store the settings.
|
64
|
+
(default name is ```EConfig```), or add additional logic. No problem, this is just a *decent default* for the lazy.
|
65
|
+
|
66
|
+
## Contributing
|
67
|
+
|
68
|
+
1. Fork it!
|
69
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
70
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
71
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
72
|
+
5. Create new Pull Request
|
73
|
+
|
74
|
+
### Testing
|
75
|
+
|
76
|
+
* clone this repo
|
77
|
+
* run `bundle install`
|
78
|
+
* run `rake spec`
|
79
|
+
|
80
|
+
|
81
|
+
## License
|
82
|
+
Copyright (c) 2013 Andrea Pavoni http://andreapavoni.com
|
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
Bundler::GemHelper.install_tasks
|
8
|
+
|
9
|
+
require 'rake/testtask'
|
10
|
+
|
11
|
+
Rake::TestTask.new(:spec) do |t|
|
12
|
+
t.test_files = Dir["spec/**/*_spec.rb"]
|
13
|
+
t.verbose = false
|
14
|
+
end
|
15
|
+
|
16
|
+
task :default => :spec
|
data/easy_config.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
$:.push File.expand_path("../lib", __FILE__)
|
2
|
+
|
3
|
+
# Maintain your gem's version:
|
4
|
+
require "easy_config/version"
|
5
|
+
|
6
|
+
# Describe your gem and declare its dependencies:
|
7
|
+
Gem::Specification.new do |s|
|
8
|
+
s.name = "easy_configs"
|
9
|
+
s.description = "Store Ruby or Rails app settings in the easiest way."
|
10
|
+
s.summary = "Store custom Ruby or Rails app settings in the easiest way."
|
11
|
+
s.version = EasyConfig::VERSION
|
12
|
+
|
13
|
+
s.authors = ["Andrea Pavoni"]
|
14
|
+
s.email = ["andrea.pavoni@gmail.com"]
|
15
|
+
s.homepage = "http://github.com/apeacox/easy_config"
|
16
|
+
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
s.files = `git ls-files`.split($\)
|
19
|
+
s.test_files = Dir["spec/**/*"]
|
20
|
+
|
21
|
+
end
|
data/lib/easy_config.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require 'erb'
|
3
|
+
require 'easy_config/deep_struct'
|
4
|
+
require 'easy_config/version'
|
5
|
+
|
6
|
+
module EasyConfig
|
7
|
+
|
8
|
+
def self.load_yaml(path, key = nil)
|
9
|
+
result = YAML.load(ERB.new(File.read(path)).result)
|
10
|
+
result = result[key.to_s] unless key.nil?
|
11
|
+
|
12
|
+
DeepStruct.new result
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
|
3
|
+
module EasyConfig
|
4
|
+
|
5
|
+
class DeepStruct < OpenStruct
|
6
|
+
def initialize(hash=nil)
|
7
|
+
@table = {}
|
8
|
+
@hash_table = {}
|
9
|
+
|
10
|
+
if hash
|
11
|
+
hash.each do |k,v|
|
12
|
+
@table[k.to_sym] = (v.is_a?(Hash) ? self.class.new(v) : v)
|
13
|
+
@hash_table[k.to_sym] = v
|
14
|
+
|
15
|
+
new_ostruct_member(k)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def to_h
|
21
|
+
@hash_table
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'rails/generators/base'
|
2
|
+
|
3
|
+
module EasyConfig
|
4
|
+
class InstallGenerator < ::Rails::Generators::Base #:nodoc:
|
5
|
+
|
6
|
+
desc 'Generates EasyConfig files.'
|
7
|
+
|
8
|
+
def self.source_root
|
9
|
+
@_easy_config_source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.banner
|
13
|
+
"rails generate easy_config:install"
|
14
|
+
end
|
15
|
+
|
16
|
+
def copy_files
|
17
|
+
template 'easy_config.rb', 'config/initializers/easy_config.rb'
|
18
|
+
template 'easy_config.yml', 'config/easy_config.yml'
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require_relative 'helper'
|
2
|
+
|
3
|
+
describe EasyConfig do
|
4
|
+
|
5
|
+
describe ".load" do
|
6
|
+
|
7
|
+
describe "it loads a basic YAML" do
|
8
|
+
let(:path) { File.expand_path './spec/fixtures/basic.yml' }
|
9
|
+
let(:obj) { EasyConfig.load_yaml path }
|
10
|
+
|
11
|
+
it "returns a EasyConfig::DeepStruct object" do
|
12
|
+
obj.must_be_instance_of EasyConfig::DeepStruct
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "it loads a namespace inside YAML" do
|
17
|
+
let(:path) { File.expand_path './spec/fixtures/namespaced.yml' }
|
18
|
+
|
19
|
+
it "loads only the given namespace" do
|
20
|
+
obj1 = EasyConfig.load_yaml path, 'namespace1'
|
21
|
+
obj2 = EasyConfig.load_yaml path, 'namespace2'
|
22
|
+
|
23
|
+
obj1.test.foo.must_equal 1
|
24
|
+
obj2.test.foo.must_equal 3
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "it loads ERB tags" do
|
30
|
+
let(:path) { File.expand_path './spec/fixtures/erbiefed.yml' }
|
31
|
+
|
32
|
+
it "loads only the given namespace" do
|
33
|
+
obj = EasyConfig.load_yaml path
|
34
|
+
|
35
|
+
obj.test.foo.must_equal 'this is from ERB'
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
data/spec/helper.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require_relative '../helper'
|
2
|
+
|
3
|
+
describe EasyConfig::DeepStruct do
|
4
|
+
let(:hash) { {a: 1, b: {c: 3}} }
|
5
|
+
let(:deep_struct) { EasyConfig::DeepStruct.new hash }
|
6
|
+
|
7
|
+
describe ".new" do
|
8
|
+
it "returns a DeepStruct object" do
|
9
|
+
deep_struct.must_be_instance_of EasyConfig::DeepStruct
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
it "inherits from OpenStruct" do
|
14
|
+
deep_struct.class.ancestors.must_include OpenStruct
|
15
|
+
end
|
16
|
+
|
17
|
+
it "exposes method names to access keys" do
|
18
|
+
deep_struct.a.must_equal 1
|
19
|
+
end
|
20
|
+
|
21
|
+
it "can wrap another DeepStruct" do
|
22
|
+
deep_struct.b.must_be_instance_of EasyConfig::DeepStruct
|
23
|
+
deep_struct.b.c.must_equal 3
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "#to_h" do
|
27
|
+
it "returns the original Hash" do
|
28
|
+
deep_struct.to_h.must_equal hash
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: easy_configs
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andrea Pavoni
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-04-26 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Store Ruby or Rails app settings in the easiest way.
|
14
|
+
email:
|
15
|
+
- andrea.pavoni@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- .gitignore
|
21
|
+
- Gemfile
|
22
|
+
- Gemfile.lock
|
23
|
+
- MIT-LICENSE
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- easy_config.gemspec
|
27
|
+
- lib/easy_config.rb
|
28
|
+
- lib/easy_config/deep_struct.rb
|
29
|
+
- lib/easy_config/version.rb
|
30
|
+
- lib/generators/easy_config/install/install_generator.rb
|
31
|
+
- lib/generators/easy_config/install/templates/easy_config.rb
|
32
|
+
- lib/generators/easy_config/install/templates/easy_config.yml
|
33
|
+
- spec/easy_config_spec.rb
|
34
|
+
- spec/fixtures/basic.yml
|
35
|
+
- spec/fixtures/erbiefed.yml
|
36
|
+
- spec/fixtures/namespaced.yml
|
37
|
+
- spec/helper.rb
|
38
|
+
- spec/lib/deep_struct_spec.rb
|
39
|
+
homepage: http://github.com/apeacox/easy_config
|
40
|
+
licenses: []
|
41
|
+
metadata: {}
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options: []
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ! '>='
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ! '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
requirements: []
|
57
|
+
rubyforge_project:
|
58
|
+
rubygems_version: 2.0.3
|
59
|
+
signing_key:
|
60
|
+
specification_version: 4
|
61
|
+
summary: Store custom Ruby or Rails app settings in the easiest way.
|
62
|
+
test_files:
|
63
|
+
- spec/easy_config_spec.rb
|
64
|
+
- spec/fixtures/basic.yml
|
65
|
+
- spec/fixtures/erbiefed.yml
|
66
|
+
- spec/fixtures/namespaced.yml
|
67
|
+
- spec/helper.rb
|
68
|
+
- spec/lib/deep_struct_spec.rb
|