figly 1.0.4 → 1.1.0
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 +4 -4
- data/README.md +14 -0
- data/lib/figly.rb +1 -1
- data/lib/figly/settings.rb +52 -14
- data/lib/figly/version.rb +1 -1
- data/spec/figly_spec.rb +22 -0
- data/spec/support/config.yml +3 -0
- data/spec/support/test_env.rb +1 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ed42e2f75afa1d3e7f14e70184fd7fb2a62a0f82
|
4
|
+
data.tar.gz: 6a62de81f60e6137f8d054936fed6562a1b4dadc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b78d9f57c8ceee1bb99ef86929596371bdcae81babd53ac2c664f917f5e61b67a559e347f71f9f490471f53904dac519018081e37731617c02e95bc3aa349f15
|
7
|
+
data.tar.gz: e79ac988669c6cba31ade4c4fef568cbdc5fb2a1e59f037036bc37037a3435577892b8dcb527989333bec0dc841546c62d17612e5495bbc0c5cf8011ef114235
|
data/README.md
CHANGED
@@ -48,6 +48,20 @@ Figly currently supports the following file extensions, and will infer the parse
|
|
48
48
|
- .toml => TOML
|
49
49
|
- .json => JSON
|
50
50
|
|
51
|
+
|
52
|
+
## UPDATE: As of version 1.1.0
|
53
|
+
|
54
|
+
You can now use your settings directly from Figly::Settings with indifferent access. So using the above config, you can access
|
55
|
+
the value `Yay` in all of the following ways, plus the ways you were able to before.
|
56
|
+
|
57
|
+
```
|
58
|
+
Figly::Settings[:nest1][:nest2].nest3
|
59
|
+
Figly::Settings['nest1'][:nest2]['nest3']
|
60
|
+
|
61
|
+
# Or if calling a method on a module makes you uncomfortable, you can use #to_h
|
62
|
+
Figly::Settings.to_h['nest1'][:nest2]['nest3']
|
63
|
+
```
|
64
|
+
|
51
65
|
## Testing
|
52
66
|
|
53
67
|
If you want to contribute start by making sure the tests work:
|
data/lib/figly.rb
CHANGED
data/lib/figly/settings.rb
CHANGED
@@ -1,27 +1,65 @@
|
|
1
1
|
module Figly
|
2
2
|
module Settings
|
3
3
|
module SettingsHash
|
4
|
-
def method_missing(
|
5
|
-
|
6
|
-
if
|
7
|
-
|
8
|
-
|
9
|
-
|
4
|
+
def method_missing(m, *args, &blk)
|
5
|
+
value = self[m]
|
6
|
+
return value.extend(SettingsHash) if value.instance_of? Hash
|
7
|
+
value
|
8
|
+
end
|
9
|
+
|
10
|
+
def [](key)
|
11
|
+
if self.key?(key.to_s)
|
12
|
+
self.fetch(key.to_s)
|
13
|
+
elsif self.key?(key.to_sym)
|
14
|
+
self.fetch(key.to_sym)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def symbolize_keys
|
19
|
+
_symbolize_keys(self)
|
20
|
+
end
|
21
|
+
|
22
|
+
def symbolize_keys!
|
23
|
+
_symbolize_keys(self, in_place: true)
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
def _symbolize_keys(hash, in_place: false)
|
28
|
+
h = in_place ? hash : hash.dup
|
29
|
+
h.tap do |h|
|
30
|
+
h.keys.each do |key|
|
31
|
+
if key.is_a?(String)
|
32
|
+
val = h.delete(key)
|
33
|
+
h[key.to_sym] = if val.is_a?(Hash)
|
34
|
+
_symbolize_keys(val, in_place: in_place)
|
35
|
+
else
|
36
|
+
val
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
10
40
|
end
|
11
41
|
end
|
12
42
|
end
|
13
43
|
|
44
|
+
def self.to_h
|
45
|
+
Figly.data
|
46
|
+
end
|
47
|
+
|
14
48
|
def self.method_missing(meth, *args, &block)
|
49
|
+
m = meth == :[] ? args[0] : meth
|
15
50
|
data = Figly.data
|
16
|
-
if data.
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
51
|
+
val = if data.key?(m.to_s)
|
52
|
+
data[m.to_s]
|
53
|
+
elsif data.key?(m.to_sym)
|
54
|
+
data[m.to_sym]
|
55
|
+
end
|
56
|
+
|
57
|
+
if val.instance_of?(Hash)
|
58
|
+
val.extend(SettingsHash)
|
59
|
+
elsif val.instance_of? Array
|
60
|
+
val.each_with_index{ |item ,idx| item.extend(SettingsHash) if item.instance_of? Hash }
|
24
61
|
end
|
62
|
+
return val
|
25
63
|
end
|
26
64
|
end
|
27
65
|
end
|
data/lib/figly/version.rb
CHANGED
data/spec/figly_spec.rb
CHANGED
@@ -28,6 +28,26 @@ describe Figly do
|
|
28
28
|
it 'should return nil when accessing a key that doesnt exist' do
|
29
29
|
expect(Figly::Settings.blah).to eq(nil)
|
30
30
|
end
|
31
|
+
|
32
|
+
context 'indifferent access' do
|
33
|
+
it 'should be able to access a symbol key from a string hash', focus: true do
|
34
|
+
expect(Figly::Settings.to_h[:a]).to eq({'aa' => {'aaa' => 'ohai'}})
|
35
|
+
expect(Figly::Settings.a[:aa]).to eq({'aaa' => 'ohai'})
|
36
|
+
expect(Figly::Settings.a.aa[:aaa]).to eq('ohai')
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'should be able to access a string key from a symbol hash' do
|
40
|
+
expect(Figly::Settings.to_h['a']).to eq({'aa' => {'aaa' => 'ohai'}})
|
41
|
+
expect(Figly::Settings['a']['aa']).to eq({'aaa' => 'ohai'})
|
42
|
+
expect(Figly::Settings['a']['aa']['aaa']).to eq('ohai')
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
context 'symbolize_keys' do
|
47
|
+
it 'should deep symbolize the keys' do
|
48
|
+
expect(Figly::Settings.a.symbolize_keys).to eq(({:aa => {:aaa => 'ohai'}}))
|
49
|
+
end
|
50
|
+
end
|
31
51
|
end
|
32
52
|
|
33
53
|
context 'TOML' do
|
@@ -55,6 +75,8 @@ describe Figly do
|
|
55
75
|
it 'should have both sets of data merged' do
|
56
76
|
expect(Figly::Settings.userId).to eq(1)
|
57
77
|
expect(Figly::Settings.a.b.c).to eq({"d"=>"test"})
|
78
|
+
expect(Figly::Settings.to_h[:a][:b][:c]).to eq({"d"=>"test"})
|
79
|
+
expect(Figly::Settings[:a][:b][:c][:d]).to eq("test")
|
58
80
|
end
|
59
81
|
end
|
60
82
|
|
data/spec/support/config.yml
CHANGED
data/spec/support/test_env.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: figly
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan Canty
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-02-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: toml
|
@@ -130,7 +130,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
130
130
|
version: '0'
|
131
131
|
requirements: []
|
132
132
|
rubyforge_project:
|
133
|
-
rubygems_version: 2.
|
133
|
+
rubygems_version: 2.6.10
|
134
134
|
signing_key:
|
135
135
|
specification_version: 4
|
136
136
|
summary: A tiny gem that allows you to access config settings from YAML, TOML, or
|