config-hash 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.ruby-version +1 -0
- data/Gemfile +3 -0
- data/LICENSE +21 -0
- data/README.md +75 -0
- data/config-hash.gemspec +13 -0
- data/lib/config-hash.rb +119 -0
- metadata +49 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c7f537363101a24d151beb368c314f42bc796eb78ae656b35653ca2499674ed3
|
4
|
+
data.tar.gz: 8c8ff79e58f1801c8e930beb1edf645b9d7761ca98c9a76b043af37a3e12ba65
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 326d2911fe5917de0b68c5041e82cd70a80deb2b4bff3c506185d19075105129039a8125e7fa7089068c374b56061df508697877f805a62d501f5011f7a9c7aa
|
7
|
+
data.tar.gz: a43d76d8d41c52e002328d3484a8b77aa48eac43e3068f3eaaf967c66ab2d164917e617bf069fafdf25531f0c60560572430b48617c8e9e99f81dfa162f0a3d7
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.5.0
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Steve Shreeve
|
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,75 @@
|
|
1
|
+
# config-hash
|
2
|
+
|
3
|
+
`config-hash` is a Ruby gem that provides a safe, homoiconic, Ruby hash supporting dot notation.
|
4
|
+
|
5
|
+
## Examples
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
# basic config
|
9
|
+
config.age = 33
|
10
|
+
|
11
|
+
# you can use the dotenv gem to populate ENV values
|
12
|
+
config.database = {
|
13
|
+
database: "movies",
|
14
|
+
adapter: "mysql2",
|
15
|
+
host: ENV["DB_HOST"],
|
16
|
+
username: ENV["DB_USER"],
|
17
|
+
}
|
18
|
+
|
19
|
+
# some nesting of values
|
20
|
+
config.school = {
|
21
|
+
teachers: {
|
22
|
+
veterans: %w[ William Peggy Thomas ],
|
23
|
+
upstarts: ["Connor", "Amber", "Bob" ],
|
24
|
+
unsure: {
|
25
|
+
hired: ["Trey"],
|
26
|
+
processing: ["Mike", "Larry", "Tina"],
|
27
|
+
}
|
28
|
+
}
|
29
|
+
}
|
30
|
+
|
31
|
+
# ==[ Add comments, functions, and data right in the config file ]==
|
32
|
+
|
33
|
+
# proc to create grouping hash
|
34
|
+
grouper = proc do |str|
|
35
|
+
str.split("\n").inject({}) do |obj, row|
|
36
|
+
row = row.split("#", 2)[0].split(/[,|]/).map(&:strip)
|
37
|
+
row.each {|col| obj[col] = row[0]} if row.size > 1
|
38
|
+
obj
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# supply some data to group
|
43
|
+
config.foods = grouper[<<-end]
|
44
|
+
meat|chicken,beef,salmon # ,pork
|
45
|
+
dairy|milk,cheese,ice cream
|
46
|
+
# junk|candy,monster,burrito
|
47
|
+
dessert|cake,pie,brownies
|
48
|
+
end
|
49
|
+
|
50
|
+
# the above yields:
|
51
|
+
# config.foods = {
|
52
|
+
# "meat" => "meat",
|
53
|
+
# "chicken" => "meat",
|
54
|
+
# "beef" => "meat",
|
55
|
+
# "salmon" => "meat",
|
56
|
+
# "dairy" => "dairy",
|
57
|
+
# "milk" => "dairy",
|
58
|
+
# "cheese" => "dairy",
|
59
|
+
# "ice cream" => "dairy",
|
60
|
+
# "dessert" => "dessert",
|
61
|
+
# "cake" => "dessert",
|
62
|
+
# "pie" => "dessert",
|
63
|
+
# "brownies" => "dessert",
|
64
|
+
# }
|
65
|
+
|
66
|
+
# this allows the following:
|
67
|
+
config.foods.cake # => "dessert"
|
68
|
+
config.foods.mean # => "meal"
|
69
|
+
config["foods.milk"] # => "dairy"
|
70
|
+
config["foods.jerky"] # => nil
|
71
|
+
```
|
72
|
+
|
73
|
+
## License
|
74
|
+
|
75
|
+
This software is licensed under terms of the MIT License.
|
data/config-hash.gemspec
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = "config-hash"
|
5
|
+
s.version = "0.5.0"
|
6
|
+
s.author = "Steve Shreeve"
|
7
|
+
s.email = "steve.shreeve@gmail.com"
|
8
|
+
s.summary = "A safe, homoiconic, Ruby hash supporting dot notation"
|
9
|
+
s.description = "This gem makes it easy to work with configuration data."
|
10
|
+
s.homepage = "https://github.com/shreeve/config-hash"
|
11
|
+
s.license = "MIT"
|
12
|
+
s.files = `git ls-files`.split("\n") - %w[.gitignore]
|
13
|
+
end
|
data/lib/config-hash.rb
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
Encoding.default_external = "UTF-8"
|
2
|
+
|
3
|
+
class Hash
|
4
|
+
def -@
|
5
|
+
NormalHash.new.merge!(self)
|
6
|
+
end
|
7
|
+
def +@
|
8
|
+
ConfigHash.new(self)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class NormalHash < Hash
|
13
|
+
end
|
14
|
+
|
15
|
+
class ConfigHash < Hash
|
16
|
+
SEPARATORS = %r|[./]|
|
17
|
+
|
18
|
+
def self.load(path="config.rb", var="config")
|
19
|
+
path = File.expand_path(path)
|
20
|
+
eval <<-"end", binding, path, 0
|
21
|
+
#{var} ||= new
|
22
|
+
#{IO.read(path, encoding: 'utf-8') if File.exists?(path)}
|
23
|
+
#{var}
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def initialize(hash=nil)
|
28
|
+
super()
|
29
|
+
merge!(hash) if hash
|
30
|
+
end
|
31
|
+
|
32
|
+
def import(root, glob)
|
33
|
+
root = File.expand_path(root)
|
34
|
+
pref = root.size + 1
|
35
|
+
Dir[File.join(root, glob)].each do |path|
|
36
|
+
keys = File.dirname(path[pref..-1])
|
37
|
+
data = ConfigHash.load(path)
|
38
|
+
self[keys] = data
|
39
|
+
end
|
40
|
+
self
|
41
|
+
end
|
42
|
+
|
43
|
+
def [](key)
|
44
|
+
key = key.to_s
|
45
|
+
if !key?(key) && key =~ SEPARATORS
|
46
|
+
val = self
|
47
|
+
key.split(SEPARATORS).each do |tag|
|
48
|
+
if !val.instance_of?(self.class)
|
49
|
+
return super(key)
|
50
|
+
elsif val.key?(tag)
|
51
|
+
val = val[tag]
|
52
|
+
elsif tag == "*" && val.size == 1
|
53
|
+
val = val[val.keys.first]
|
54
|
+
else
|
55
|
+
return super(key)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
val
|
59
|
+
else
|
60
|
+
super(key)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def []=(key, val)
|
65
|
+
our = self.class
|
66
|
+
key = key.to_s
|
67
|
+
val = our.new(val) if val.instance_of?(Hash)
|
68
|
+
if val.instance_of?(NormalHash)
|
69
|
+
super(key, val)
|
70
|
+
elsif key =~ SEPARATORS
|
71
|
+
all = key.split(SEPARATORS)
|
72
|
+
key = all.pop
|
73
|
+
top = all.inject(self) do |top, tag|
|
74
|
+
if top.key?(tag) && (try = top[tag]).instance_of?(our)
|
75
|
+
top = try
|
76
|
+
else
|
77
|
+
top = top[tag] = our.new
|
78
|
+
end
|
79
|
+
end
|
80
|
+
top[key] = val
|
81
|
+
else
|
82
|
+
super(key, val)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
alias store []=
|
87
|
+
|
88
|
+
def key?(key)
|
89
|
+
super(key.to_s)
|
90
|
+
end
|
91
|
+
|
92
|
+
def merge!(other_hash)
|
93
|
+
raise ArgumentError unless Hash === other_hash
|
94
|
+
other_hash.each do |k, v|
|
95
|
+
if block_given? && key?(k)
|
96
|
+
self[k] = yield(k, self[k], v)
|
97
|
+
else
|
98
|
+
self[k] = v
|
99
|
+
end
|
100
|
+
end
|
101
|
+
self
|
102
|
+
end
|
103
|
+
|
104
|
+
alias update merge!
|
105
|
+
|
106
|
+
def to_hash
|
107
|
+
Hash[self]
|
108
|
+
end
|
109
|
+
|
110
|
+
def method_missing(sym, *args, &block)
|
111
|
+
if sym =~ /=$/
|
112
|
+
self[$`] = args.first
|
113
|
+
elsif args.empty? # or... key?(sym)
|
114
|
+
self[sym]
|
115
|
+
else
|
116
|
+
super
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
metadata
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: config-hash
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.5.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Steve Shreeve
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-05-25 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: This gem makes it easy to work with configuration data.
|
14
|
+
email: steve.shreeve@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- ".ruby-version"
|
20
|
+
- Gemfile
|
21
|
+
- LICENSE
|
22
|
+
- README.md
|
23
|
+
- config-hash.gemspec
|
24
|
+
- lib/config-hash.rb
|
25
|
+
homepage: https://github.com/shreeve/config-hash
|
26
|
+
licenses:
|
27
|
+
- MIT
|
28
|
+
metadata: {}
|
29
|
+
post_install_message:
|
30
|
+
rdoc_options: []
|
31
|
+
require_paths:
|
32
|
+
- lib
|
33
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
requirements: []
|
44
|
+
rubyforge_project:
|
45
|
+
rubygems_version: 2.7.6
|
46
|
+
signing_key:
|
47
|
+
specification_version: 4
|
48
|
+
summary: A safe, homoiconic, Ruby hash supporting dot notation
|
49
|
+
test_files: []
|