non_config 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +2 -0
- data/.rubocop.yml +5 -0
- data/.travis.yml +4 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +2 -0
- data/LICENSE.txt +21 -0
- data/README.md +48 -0
- data/Rakefile +6 -0
- data/lib/non_config/base.rb +109 -0
- data/lib/non_config/config_file.rb +29 -0
- data/lib/non_config/exceptions.rb +5 -0
- data/lib/non_config/non_hash.rb +55 -0
- data/lib/non_config/version.rb +3 -0
- data/lib/non_config.rb +2 -0
- data/non_config.gemspec +25 -0
- metadata +117 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c9d1956f2bf767aef4c05a4268d6d77b5ece9043
|
4
|
+
data.tar.gz: b987ee5dd2d4c24900f090e95a2656fa7ebe566e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7bb1a16e91fe9026a95ab1394a7899941589aef7768b983cb7708ad8f6ebe80fabbe7671a1f168453801436f121228a292487c31dd4bfb5a4e8cc89f92ba4b2a
|
7
|
+
data.tar.gz: c6b2c41117a4e5da9e4967c8d8c0da58c811eeefa960aa5fd018e872a71e5529c88e2ba7d24d6b32df85f0d2c3ba6d669405ed577b056dd6f20f6bcb5ca10714
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at non.dmitriy@gmail.com. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Dmitriy Non
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# NonConfig
|
2
|
+
|
3
|
+
This gem was created because
|
4
|
+
I am not enough satisfied with [Settingslogic](https://github.com/settingslogic/settingslogic).
|
5
|
+
|
6
|
+
E.g. it does not support multiple files, it can't save config files.
|
7
|
+
|
8
|
+
*But I still thinking Settinglogic is pretty good*
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'non_config'
|
16
|
+
```
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install non_config
|
25
|
+
|
26
|
+
## Usage
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
require 'non_config'
|
30
|
+
|
31
|
+
class Config < NonConfig::Base # I thought it would be funny :\
|
32
|
+
source :db, 'db/config.yml'
|
33
|
+
source :app, 'app_config.yml'
|
34
|
+
source :else_config, 'something.yml'
|
35
|
+
|
36
|
+
env :development
|
37
|
+
end
|
38
|
+
|
39
|
+
```
|
40
|
+
|
41
|
+
## Contributing
|
42
|
+
|
43
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/Nondv/non_config. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
44
|
+
|
45
|
+
|
46
|
+
## License
|
47
|
+
|
48
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
require_relative 'config_file'
|
2
|
+
|
3
|
+
module NonConfig
|
4
|
+
#
|
5
|
+
# Base class you need to inherit
|
6
|
+
#
|
7
|
+
class Base
|
8
|
+
ENVIRONMENTS = [:development, :test, :production].freeze
|
9
|
+
@@files = {}
|
10
|
+
@@env = nil
|
11
|
+
|
12
|
+
#
|
13
|
+
# Loads file.
|
14
|
+
#
|
15
|
+
# Files that were added last are considered to be more important
|
16
|
+
# i.e. if you have
|
17
|
+
#
|
18
|
+
# source :file1, '...'
|
19
|
+
# source :file2, '...'
|
20
|
+
# source :file3, '...'
|
21
|
+
#
|
22
|
+
# all operations will be executed from file3 to file1
|
23
|
+
#
|
24
|
+
# @param file_alias [Symbol, String] alias for file
|
25
|
+
# @param path [String] full path to config
|
26
|
+
# @raise [StandardError]
|
27
|
+
#
|
28
|
+
def self.source(file_alias, path)
|
29
|
+
fail('file alias is already taken') if @@files[file_alias]
|
30
|
+
|
31
|
+
@@files[file_alias] = ConfigFile.new(path)
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.config_file(file_alias = nil)
|
35
|
+
file_alias = @@files.to_a.last && @@files.to_a.last[0] unless file_alias
|
36
|
+
fail FileNotRegistered unless file_alias && @@files.key?(file_alias)
|
37
|
+
|
38
|
+
@@files[file_alias]
|
39
|
+
end
|
40
|
+
|
41
|
+
# sets environment. See Base::ENVIRONMENTS
|
42
|
+
def self.env(env)
|
43
|
+
return @@env = nil unless env
|
44
|
+
|
45
|
+
env = env.to_sym
|
46
|
+
fail UnsupportedEnvironment unless ENVIRONMENTS.include? env
|
47
|
+
|
48
|
+
@@env = env.to_s
|
49
|
+
end
|
50
|
+
|
51
|
+
# Sets value to one of config files
|
52
|
+
def self.set(key, value, file_alias = nil)
|
53
|
+
file = config_file file_alias
|
54
|
+
|
55
|
+
if @@env
|
56
|
+
file[@@env] ||= {}
|
57
|
+
file[@@env][key] = value
|
58
|
+
else
|
59
|
+
file[key] = value
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
# alias for #set key, value
|
64
|
+
def self.[]=(key, value)
|
65
|
+
set(key, value)
|
66
|
+
end
|
67
|
+
|
68
|
+
#
|
69
|
+
# Tries to find object.
|
70
|
+
#
|
71
|
+
# Unlike Base.key it won't raise exception on not found
|
72
|
+
#
|
73
|
+
# @return nil if not found
|
74
|
+
#
|
75
|
+
def self.[](key)
|
76
|
+
default = nil
|
77
|
+
@@files.reverse_each do |_name, file|
|
78
|
+
default = file[key] if default.nil?
|
79
|
+
|
80
|
+
return file[@@env][key] if @@env && file[@@env][key]
|
81
|
+
return file[key] if !@@env && file[key]
|
82
|
+
end
|
83
|
+
|
84
|
+
default
|
85
|
+
end
|
86
|
+
|
87
|
+
# If `args` empty it will try to find `m.to_s` and `m` in config
|
88
|
+
# else (or if not found) it will call super
|
89
|
+
def self.method_missing(m, *args)
|
90
|
+
result = self[m.to_s]
|
91
|
+
return result if result
|
92
|
+
|
93
|
+
result = self[m]
|
94
|
+
return result if result
|
95
|
+
|
96
|
+
super
|
97
|
+
end
|
98
|
+
|
99
|
+
def self.save(file_alias = nil)
|
100
|
+
fail FileNotRegistered if file_alias && !@@files.key?(file_alias)
|
101
|
+
|
102
|
+
if file_alias
|
103
|
+
@@files[file_alias].save
|
104
|
+
else
|
105
|
+
@@files.values.each &:save
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require_relative 'non_hash'
|
3
|
+
require_relative 'exceptions'
|
4
|
+
|
5
|
+
module NonConfig
|
6
|
+
#
|
7
|
+
# Hash with filepath and {#save} method
|
8
|
+
#
|
9
|
+
class ConfigFile < NonHash
|
10
|
+
attr_reader :path
|
11
|
+
|
12
|
+
# parses yaml from path
|
13
|
+
def initialize(path = nil)
|
14
|
+
super()
|
15
|
+
return unless path
|
16
|
+
fail FileNotFound unless File.exist? path
|
17
|
+
|
18
|
+
@path = path
|
19
|
+
file_data = File.read @path
|
20
|
+
records = YAML.load file_data
|
21
|
+
merge! self.class.from_hash(records)
|
22
|
+
end
|
23
|
+
|
24
|
+
# rewrite file so you can save your changes
|
25
|
+
def save
|
26
|
+
File.open(@path, 'w') { |f| f.write to_yaml }
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module NonConfig
|
2
|
+
#
|
3
|
+
# hash with overriden #method_missing
|
4
|
+
#
|
5
|
+
class NonHash < Hash
|
6
|
+
# creates NonHash from Hash with recursive conversion of sub hashes
|
7
|
+
def self.from_hash(hash)
|
8
|
+
hash.each do |k, v|
|
9
|
+
hash[k] = from_hash(v) if v.is_a? Hash
|
10
|
+
hash[k] = convert_array_subhashes(v) if v.is_a? Array
|
11
|
+
end
|
12
|
+
|
13
|
+
new.merge(hash)
|
14
|
+
end
|
15
|
+
|
16
|
+
# recursively converts subhashes of `array` to `self.class`
|
17
|
+
def self.convert_array_subhashes(array)
|
18
|
+
array.map do |e|
|
19
|
+
next convert_array_subhashes(e) if e.is_a? Array
|
20
|
+
next from_hash(e) if e.is_a? Hash
|
21
|
+
e
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# it allows constructions like non_hash.value1.value2
|
26
|
+
def []=(key, value)
|
27
|
+
value = self.class.from_hash(value) if value.is_a? Hash
|
28
|
+
super(key, value)
|
29
|
+
end
|
30
|
+
|
31
|
+
def method_missing(m, *args)
|
32
|
+
super unless args.empty?
|
33
|
+
self[m.to_s] || self[m.to_sym]
|
34
|
+
end
|
35
|
+
|
36
|
+
def to_hash
|
37
|
+
each do |k, v|
|
38
|
+
case v.class
|
39
|
+
when self.class then self[k] = v.to_hash
|
40
|
+
when Array then self[k] = reconvert_array(v)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
def reconvert_array(array)
|
48
|
+
array.map do |e|
|
49
|
+
next reconvert_array(e) if e.is_a? Array
|
50
|
+
next e.to_hash if e.is_a? self.class
|
51
|
+
e
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
data/lib/non_config.rb
ADDED
data/non_config.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'non_config/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'non_config'
|
8
|
+
spec.version = NonConfig::VERSION
|
9
|
+
spec.authors = ['Dmitriy Non']
|
10
|
+
spec.email = ['non.dmitriy@gmail.com']
|
11
|
+
|
12
|
+
spec.summary = 'Good (I hope) way to use your config files'
|
13
|
+
spec.homepage = 'https://github.com/Nondv/non_config'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = 'exe'
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_development_dependency 'bundler', '~> 1.11'
|
22
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
23
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
24
|
+
spec.add_development_dependency 'rubocop', '0.36'
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: non_config
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dmitriy Non
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-01-29 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.11'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
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.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rubocop
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.36'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.36'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- non.dmitriy@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".rubocop.yml"
|
79
|
+
- ".travis.yml"
|
80
|
+
- CODE_OF_CONDUCT.md
|
81
|
+
- Gemfile
|
82
|
+
- LICENSE.txt
|
83
|
+
- README.md
|
84
|
+
- Rakefile
|
85
|
+
- lib/non_config.rb
|
86
|
+
- lib/non_config/base.rb
|
87
|
+
- lib/non_config/config_file.rb
|
88
|
+
- lib/non_config/exceptions.rb
|
89
|
+
- lib/non_config/non_hash.rb
|
90
|
+
- lib/non_config/version.rb
|
91
|
+
- non_config.gemspec
|
92
|
+
homepage: https://github.com/Nondv/non_config
|
93
|
+
licenses:
|
94
|
+
- MIT
|
95
|
+
metadata: {}
|
96
|
+
post_install_message:
|
97
|
+
rdoc_options: []
|
98
|
+
require_paths:
|
99
|
+
- lib
|
100
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
requirements: []
|
111
|
+
rubyforge_project:
|
112
|
+
rubygems_version: 2.4.6
|
113
|
+
signing_key:
|
114
|
+
specification_version: 4
|
115
|
+
summary: Good (I hope) way to use your config files
|
116
|
+
test_files: []
|
117
|
+
has_rdoc:
|