configureasy 0.0.2
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.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/Guardfile +15 -0
- data/LICENSE.txt +22 -0
- data/README.md +68 -0
- data/Rakefile +2 -0
- data/configureasy.gemspec +27 -0
- data/lib/configureasy.rb +38 -0
- data/lib/configureasy/config.rb +15 -0
- data/lib/configureasy/configurable.rb +57 -0
- data/lib/configureasy/version.rb +3 -0
- data/spec/lib/configureasy/config_spec.rb +13 -0
- data/spec/lib/configureasy/configurable_spec.rb +81 -0
- data/spec/lib/configureasy_spec.rb +0 -0
- data/spec/spec_helper.rb +9 -0
- metadata +133 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 27a4dc67f6dbb0c1a719a09453e3621c91365ea1
|
4
|
+
data.tar.gz: 7110800425b8880687c7a78e5c23b285b58d3705
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: eb1870f7f84b47e4b08483dc958dc3de2cc4859b66d4066e38786c9f553cf28bc220a278797a8cc43aea450d244fb79365b5d398df0d5a0cd087abac4e35b23a
|
7
|
+
data.tar.gz: 2f88c2dfe20f5ed7b34ec131691c81a228e9b39104583b289620d1c81c98ffe4b548a65b62a25ad83d84806aac2429692e6d9c6e16d85db2137a3152c46592c3
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
clearing :on
|
2
|
+
guard :rspec, cmd: "bundle exec rspec" do
|
3
|
+
require "guard/rspec/dsl"
|
4
|
+
dsl = Guard::RSpec::Dsl.new(self)
|
5
|
+
|
6
|
+
# RSpec files
|
7
|
+
rspec = dsl.rspec
|
8
|
+
watch(rspec.spec_helper) { rspec.spec_dir }
|
9
|
+
watch(rspec.spec_support) { rspec.spec_dir }
|
10
|
+
watch(rspec.spec_files)
|
11
|
+
|
12
|
+
# Ruby files
|
13
|
+
ruby = dsl.ruby
|
14
|
+
dsl.watch_spec_files_for(ruby.lib_files)
|
15
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Alexandre Prates
|
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,68 @@
|
|
1
|
+
Configureasy
|
2
|
+
=============
|
3
|
+
|
4
|
+
A forma mais simples de carregar arquivos de configuração em suas Classe/Módulos.
|
5
|
+
|
6
|
+
## Instalação
|
7
|
+
|
8
|
+
Basta adicionar esta linha no seu Gemfile
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'configureasy'
|
12
|
+
```
|
13
|
+
|
14
|
+
E entao executar:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Ou instale manualmente:
|
19
|
+
|
20
|
+
$ gem install configureasy
|
21
|
+
|
22
|
+
## Como usar
|
23
|
+
|
24
|
+
Basta incluir na classe e começar a usar
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
class Foo
|
28
|
+
include Configureasy
|
29
|
+
end
|
30
|
+
|
31
|
+
Foo.config.some_key_in_config
|
32
|
+
# => 'some value for that key'
|
33
|
+
```
|
34
|
+
|
35
|
+
Por padrão ele busca um arquivo (.yml) no diretório _config_, caso precise especificar um nome diferente utilize o método _config_name_.
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
class Foo
|
39
|
+
include Configureasy
|
40
|
+
# load APP_ROOT/config/foo_config.yml
|
41
|
+
config_name :foo_config
|
42
|
+
end
|
43
|
+
```
|
44
|
+
|
45
|
+
Caso precise você tambem pode especificar de onde carregar a configuração usando o método _config_filename_.
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
class Foo
|
49
|
+
include Configureasy
|
50
|
+
# load APP_ROOT/.hide_dir/secrets.yml
|
51
|
+
config_filename '.hide_dir/secrets.yml'
|
52
|
+
end
|
53
|
+
```
|
54
|
+
|
55
|
+
Caso precise ver o conteúdo original do arquivo utilize o método _raw_content_.
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
class Foo
|
59
|
+
include Configureasy
|
60
|
+
end
|
61
|
+
|
62
|
+
Foo.config.raw_content
|
63
|
+
# => {'foo' => 'bar'}
|
64
|
+
```
|
65
|
+
|
66
|
+
## Duvidas, questões e desconfianças em geral
|
67
|
+
|
68
|
+
mail me: ajfprates@gmail.com
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'configureasy/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "configureasy"
|
8
|
+
spec.version = Configureasy::VERSION
|
9
|
+
spec.authors = ["Alexandre Prates"]
|
10
|
+
spec.email = ["ajfprates@gmail.com"]
|
11
|
+
spec.summary = %q{A Simple way to get configs in your Classes/Modules.}
|
12
|
+
spec.description = %q{A Simple way to get configs in your Classes/Modules.}
|
13
|
+
spec.homepage = "https://gitlab.ir7.com.br/videos/configureasy"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
|
24
|
+
spec.add_development_dependency "rspec", "~> 3.1"
|
25
|
+
spec.add_development_dependency "guard", "~> 2.10"
|
26
|
+
spec.add_development_dependency "guard-rspec", "~> 4.5"
|
27
|
+
end
|
data/lib/configureasy.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require "configureasy/version"
|
2
|
+
|
3
|
+
# Configureasy is a easy way to getting configs into you class/model
|
4
|
+
#
|
5
|
+
# Example
|
6
|
+
#
|
7
|
+
# class Foo
|
8
|
+
# include Configureasy
|
9
|
+
# end
|
10
|
+
# Foo.config.some_key
|
11
|
+
# => 'some value'
|
12
|
+
#
|
13
|
+
# class Bar
|
14
|
+
# include Configureasy
|
15
|
+
# config_name :barz #looks for APP_DIR/config/barz.yml
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
# class FooBar
|
19
|
+
# include Configureasy
|
20
|
+
# def internal_access
|
21
|
+
# value = self.class.some_key
|
22
|
+
# ...
|
23
|
+
# end
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
module Configureasy
|
27
|
+
# Class to raise error on missing config file _yaml_
|
28
|
+
class ConfigNotFound < Exception; end
|
29
|
+
# Class to raise error on invalid config file
|
30
|
+
class ConfigInvalid < Exception; end
|
31
|
+
|
32
|
+
def self.included(receiver)
|
33
|
+
receiver.extend Configurable
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
require 'configureasy/config'
|
38
|
+
require 'configureasy/configurable'
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
|
3
|
+
##
|
4
|
+
# Class for store and retrive config values
|
5
|
+
#
|
6
|
+
# Example
|
7
|
+
# >> Confugureasy::Config.new(foo: 'foo').foo
|
8
|
+
# => 'foo'
|
9
|
+
#
|
10
|
+
class Configureasy::Config < OpenStruct
|
11
|
+
# return config as hash
|
12
|
+
def raw_content
|
13
|
+
@table
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
|
3
|
+
module Configureasy::Configurable
|
4
|
+
# Setting config file name
|
5
|
+
#
|
6
|
+
# Example
|
7
|
+
# config_name :foo
|
8
|
+
# => APP_ROOT/config/foo.yml
|
9
|
+
#
|
10
|
+
# config_name :bar
|
11
|
+
# => APP_ROOT/config/bar.yml
|
12
|
+
#
|
13
|
+
def config_name(name = nil)
|
14
|
+
@config_name = name if name
|
15
|
+
@config_name || self.name.downcase
|
16
|
+
end
|
17
|
+
|
18
|
+
def config_filename(filename = nil)
|
19
|
+
@config_filename = File.expand_path filename if filename
|
20
|
+
@config_filename || File.expand_path("./config/#{config_name}.yml")
|
21
|
+
end
|
22
|
+
|
23
|
+
# Load the config yaml and return Configureasy::Config instance
|
24
|
+
def config
|
25
|
+
@config ||= load_configs
|
26
|
+
end
|
27
|
+
|
28
|
+
# Looks for config file an return true if file exists
|
29
|
+
def have_config?
|
30
|
+
File.exist? config_filename
|
31
|
+
end
|
32
|
+
|
33
|
+
# Reload configs
|
34
|
+
def reset_config!
|
35
|
+
@config = nil
|
36
|
+
config
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def load_configs
|
42
|
+
configs_hash = parse_configs
|
43
|
+
raise Configureasy::ConfigInvalid, "invalid config file '#{config_filename}'", caller unless configs_hash.is_a? Hash
|
44
|
+
|
45
|
+
Configureasy::Config.new configs_hash[_current_env] || configs_hash
|
46
|
+
end
|
47
|
+
|
48
|
+
def parse_configs
|
49
|
+
raise Configureasy::ConfigNotFound, "file not found '#{config_filename}'", caller unless have_config?
|
50
|
+
YAML.load_file config_filename
|
51
|
+
end
|
52
|
+
|
53
|
+
def _current_env
|
54
|
+
ENV['RAILS_ENV'] || ENV['RUBY_ENV'] || ENV['RACK_ENV'] || 'development'
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class Foo
|
4
|
+
extend Configureasy::Configurable
|
5
|
+
end
|
6
|
+
|
7
|
+
describe Configureasy::Configurable do
|
8
|
+
describe ".config_name" do
|
9
|
+
it "by default config name is class name" do
|
10
|
+
expect(Foo.config_name).to eq('foo')
|
11
|
+
end
|
12
|
+
|
13
|
+
it "changes config name" do
|
14
|
+
expect(Foo.config_name).to eq('foo')
|
15
|
+
Foo.config_name "bar"
|
16
|
+
expect(Foo.config_name).to eq('bar')
|
17
|
+
end
|
18
|
+
|
19
|
+
it "config name reflects on config_filename" do
|
20
|
+
Foo.config_name "other_conf"
|
21
|
+
expect(Foo.send :config_filename).to match(/other_conf\.yml$/)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe '.config_filename' do
|
26
|
+
it "set config filename" do
|
27
|
+
Foo.config_filename '.hidedir/secret_config.yml'
|
28
|
+
expect(Foo.config_filename).to end_with('.hidedir/secret_config.yml')
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe '#config' do
|
33
|
+
let(:yaml_content) { {'development' => {'foo' => 'bar'}, 'test' => {'foo' => 'test'} } }
|
34
|
+
|
35
|
+
it "raise excepion when file missing" do
|
36
|
+
expect { Foo.config }.to raise_exception Configureasy::ConfigNotFound
|
37
|
+
end
|
38
|
+
|
39
|
+
it "raise exception when file content is not valid yaml" do
|
40
|
+
allow(YAML).to receive(:load_file).and_return("wrong yaml content")
|
41
|
+
allow(File).to receive(:exist?).and_return(true)
|
42
|
+
|
43
|
+
expect { Foo.config }.to raise_exception Configureasy::ConfigInvalid
|
44
|
+
end
|
45
|
+
|
46
|
+
it "access values on config file" do
|
47
|
+
allow(YAML).to receive(:load_file).and_return(yaml_content)
|
48
|
+
allow(File).to receive(:exist?).and_return(true)
|
49
|
+
|
50
|
+
expect(Foo.config).to respond_to(:foo)
|
51
|
+
expect(Foo.config.foo).to eq('bar')
|
52
|
+
end
|
53
|
+
|
54
|
+
it "load contend for current environment" do
|
55
|
+
allow(YAML).to receive(:load_file).and_return(yaml_content)
|
56
|
+
allow(File).to receive(:exist?).and_return(true)
|
57
|
+
|
58
|
+
ENV['RUBY_ENV'] = 'development'
|
59
|
+
expect(Foo.reset_config!.foo).to eq('bar')
|
60
|
+
|
61
|
+
ENV['RUBY_ENV'] = 'test'
|
62
|
+
expect(Foo.reset_config!.foo).to eq('test')
|
63
|
+
|
64
|
+
ENV['RUBY_ENV'] = nil
|
65
|
+
|
66
|
+
ENV['RACK_ENV'] = 'development'
|
67
|
+
expect(Foo.reset_config!.foo).to eq('bar')
|
68
|
+
ENV['RACK_ENV'] = nil
|
69
|
+
|
70
|
+
ENV['RAILS_ENV'] = 'test'
|
71
|
+
expect(Foo.reset_config!.foo).to eq('test')
|
72
|
+
end
|
73
|
+
|
74
|
+
it "config is kind of Configs class" do
|
75
|
+
allow(YAML).to receive(:load_file).and_return(yaml_content)
|
76
|
+
allow(File).to receive(:exist?).and_return(true)
|
77
|
+
|
78
|
+
expect(Foo.config).to be_kind_of(Configureasy::Config)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
File without changes
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
require './lib/configureasy'
|
2
|
+
RSpec.configure do |config|
|
3
|
+
config.expect_with :rspec do |expectations|
|
4
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
5
|
+
end
|
6
|
+
config.mock_with :rspec do |mocks|
|
7
|
+
mocks.verify_partial_doubles = true
|
8
|
+
end
|
9
|
+
end
|
metadata
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: configureasy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alexandre Prates
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-03-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.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: '3.1'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.1'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: guard
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '2.10'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '2.10'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: guard-rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '4.5'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '4.5'
|
83
|
+
description: A Simple way to get configs in your Classes/Modules.
|
84
|
+
email:
|
85
|
+
- ajfprates@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- Gemfile
|
92
|
+
- Guardfile
|
93
|
+
- LICENSE.txt
|
94
|
+
- README.md
|
95
|
+
- Rakefile
|
96
|
+
- configureasy.gemspec
|
97
|
+
- lib/configureasy.rb
|
98
|
+
- lib/configureasy/config.rb
|
99
|
+
- lib/configureasy/configurable.rb
|
100
|
+
- lib/configureasy/version.rb
|
101
|
+
- spec/lib/configureasy/config_spec.rb
|
102
|
+
- spec/lib/configureasy/configurable_spec.rb
|
103
|
+
- spec/lib/configureasy_spec.rb
|
104
|
+
- spec/spec_helper.rb
|
105
|
+
homepage: https://gitlab.ir7.com.br/videos/configureasy
|
106
|
+
licenses:
|
107
|
+
- MIT
|
108
|
+
metadata: {}
|
109
|
+
post_install_message:
|
110
|
+
rdoc_options: []
|
111
|
+
require_paths:
|
112
|
+
- lib
|
113
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - ">="
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '0'
|
123
|
+
requirements: []
|
124
|
+
rubyforge_project:
|
125
|
+
rubygems_version: 2.4.5
|
126
|
+
signing_key:
|
127
|
+
specification_version: 4
|
128
|
+
summary: A Simple way to get configs in your Classes/Modules.
|
129
|
+
test_files:
|
130
|
+
- spec/lib/configureasy/config_spec.rb
|
131
|
+
- spec/lib/configureasy/configurable_spec.rb
|
132
|
+
- spec/lib/configureasy_spec.rb
|
133
|
+
- spec/spec_helper.rb
|