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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 65ac42decd3b9d8da0ffa6a4d00401300a0e83dc
4
- data.tar.gz: d257ea30a2c4242b96d4e3213a4aab91540e213f
3
+ metadata.gz: ed42e2f75afa1d3e7f14e70184fd7fb2a62a0f82
4
+ data.tar.gz: 6a62de81f60e6137f8d054936fed6562a1b4dadc
5
5
  SHA512:
6
- metadata.gz: 170370e713d00bfc38576067ce7ae0b6b4d82bea2febbebb1224c6ecf220aefeef1d88894a5130c18b4344ae96e07276d68d5640134c1cfa52946c87dd2e64b4
7
- data.tar.gz: 7e04253fef6b344ad1c7a36d54c33344835f14c831033b0fabf18f24a904e69bd4b5174483bdef7ce1de1658269abe028ec17e3b0b59d28b9d0c436ad54e3380
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:
@@ -51,7 +51,7 @@ module Figly
51
51
  if _config_loaded?
52
52
  _deep_merge(@@data, data)
53
53
  else
54
- @@data = data
54
+ @@data = data.extend(Settings::SettingsHash)
55
55
  end
56
56
  end
57
57
 
@@ -1,27 +1,65 @@
1
1
  module Figly
2
2
  module Settings
3
3
  module SettingsHash
4
- def method_missing(meth, *args, &blk)
5
- m = meth.to_s
6
- if has_key? m
7
- value = self[m]
8
- return value.extend(SettingsHash) if value.instance_of? Hash
9
- value
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.has_key? meth.to_s
17
- val = data[meth.to_s]
18
- if val.instance_of?(Hash)
19
- val.extend(SettingsHash)
20
- elsif val.instance_of? Array
21
- val.each_with_index{ |item ,idx| item.extend(SettingsHash) if item.instance_of? Hash }
22
- end
23
- return val
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
@@ -1,3 +1,3 @@
1
1
  module Figly
2
- VERSION = "1.0.4"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -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
 
@@ -15,3 +15,6 @@ ary:
15
15
  'Woot'
16
16
  - 3
17
17
 
18
+ a:
19
+ aa:
20
+ aaa: ohai
@@ -1,3 +1,4 @@
1
+ require 'pry'
1
2
  module TestEnv
2
3
  def config_file(ext = 'yml')
3
4
  File.join(root, "spec/support/config.#{ext}")
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
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: 2016-10-26 00:00:00.000000000 Z
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.4.8
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