tiny_dot 2.3.2 → 3.0.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/lib/tiny_dot.rb +51 -78
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 44f7c9f36e7b3d775cf41774060f0144b5db9006922f271a246aed612288d2bc
|
4
|
+
data.tar.gz: db865874e1f5b854d6b7b34362e1e5efab6f8340551c5725007f09cd0bc67ca9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c5bddb3ad6272bb1b3362f79be814ed6c4bc8585b9d1639595055c44c7d9969da4c099a0228ed389cae4eeab3453ac448cdc5570fee0228e5feefb476f01c624
|
7
|
+
data.tar.gz: 953bcffe9fc13b98ed22b40fdd7a56bde7f889eb775d90eb3b9480e94167573fa875b07f57be6ea887d0a92010e7563827d0e2068b2dc89f54165c081e2ed909
|
data/lib/tiny_dot.rb
CHANGED
@@ -1,95 +1,68 @@
|
|
1
|
-
require '
|
2
|
-
require '
|
1
|
+
require 'psych'
|
2
|
+
require 'oj'
|
3
3
|
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
4
|
+
# The TinyDot module provides methods for converting data from different
|
5
|
+
# formats (environment variables, YAML, JSON and Hashes) into a nested Struct.
|
6
|
+
# This allows for easy access to the data using dot notation.
|
7
7
|
#
|
8
|
-
#
|
9
|
-
#
|
10
|
-
# => #<TinyDot:0x0201243 @data={ 'bar' => 'baz' }>
|
11
|
-
# > t.foo! # ! returns the value under the chain of keys
|
12
|
-
# => { 'bar' => 'baz' }
|
8
|
+
# The `from_env` method converts the current environment variables into a
|
9
|
+
# Struct.
|
13
10
|
#
|
14
|
-
#
|
15
|
-
#
|
16
|
-
# object that's essentially a #dig into the top-level hash.
|
11
|
+
# The `from_yaml` method takes a string containing YAML data and converts it
|
12
|
+
# into a Struct.
|
17
13
|
#
|
18
|
-
#
|
19
|
-
#
|
14
|
+
# The `from_json` method takes a string containing JSON data and converts it
|
15
|
+
# into a Struct.
|
20
16
|
#
|
21
|
-
#
|
22
|
-
#
|
17
|
+
# The `from_hash` method takes a Hash and converts it into a Struct. It really
|
18
|
+
# just calls the `_hash_to_struct` method.
|
23
19
|
#
|
24
|
-
#
|
25
|
-
#
|
20
|
+
# The `_hash_to_struct` is a private method that is used by the other methods
|
21
|
+
# to convert the data from a hash into a Struct. It handles nested data by
|
22
|
+
# recursively calling itself when it encounters a Hash or an array within the
|
23
|
+
# data.
|
26
24
|
#
|
27
|
-
#
|
28
|
-
#
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
TinyDot.new(YAML.safe_load_file(filename, permitted_classes: permitted_classes))
|
38
|
-
end
|
39
|
-
|
40
|
-
# returns a TinyDot instance after parsing the JSON in the named filename
|
41
|
-
def self.from_json_file(filename)
|
42
|
-
TinyDot.new(JSON.parse(IO.read(filename)))
|
43
|
-
end
|
25
|
+
# The `_to_attr_friendly_symbols` is a private method that is used to convert
|
26
|
+
# keys in the hash to symbols that can be used as attributes in a Struct. It
|
27
|
+
# replaces spaces and dashes with underscores and converts the keys to symbols.
|
28
|
+
#
|
29
|
+
# Please note that the JSON and YAML are required to be passed as String.
|
30
|
+
module TinyDot
|
31
|
+
class << self
|
32
|
+
def from_env
|
33
|
+
_hash_to_struct(ENV.to_h)
|
34
|
+
end
|
44
35
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
36
|
+
def from_yaml(yaml_string)
|
37
|
+
yaml = Psych.load(yaml_string)
|
38
|
+
_hash_to_struct(yaml)
|
39
|
+
end
|
49
40
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
41
|
+
def self.from_json(json_string)
|
42
|
+
json = Oj.load(json_string)
|
43
|
+
_hash_to_struct(json)
|
44
|
+
end
|
54
45
|
|
55
|
-
|
56
|
-
|
57
|
-
|
46
|
+
def self.from_hash(hash)
|
47
|
+
_hash_to_struct(json)
|
48
|
+
end
|
58
49
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
50
|
+
def _hash_to_struct(hash)
|
51
|
+
case hash
|
52
|
+
when Hash
|
53
|
+
struct = ::Struct.new(*_to_attr_friendly_symbols(hash.keys))
|
54
|
+
struct.new(*hash.values.map { |v| _hash_to_struct(v) })
|
55
|
+
when Array
|
56
|
+
hash.map { |v| _hash_to_struct(v) }
|
65
57
|
else
|
66
|
-
|
67
|
-
TinyDot.new(@data[ms])
|
68
|
-
else
|
69
|
-
TinyDot.new({})
|
70
|
-
end
|
58
|
+
hash
|
71
59
|
end
|
72
|
-
else
|
73
|
-
TinyDot.new({})
|
74
60
|
end
|
75
|
-
end
|
76
|
-
|
77
|
-
def ==(other)
|
78
|
-
@data == other.to_hash
|
79
|
-
end
|
80
61
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
# if you modify anything here you're modifying the internal data in this
|
87
|
-
# TinyDot instance
|
88
|
-
def to_hash
|
89
|
-
@data
|
90
|
-
end
|
91
|
-
|
92
|
-
def to_yaml
|
93
|
-
@data.to_yaml
|
62
|
+
def _to_attr_friendly_symbols(keys)
|
63
|
+
keys
|
64
|
+
.map{|k| k.gsub(/[-\s]/, '_') }
|
65
|
+
.map(&:to_sym)
|
66
|
+
end
|
94
67
|
end
|
95
68
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tiny_dot
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 3.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeff Lunt
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-01-
|
11
|
+
date: 2023-01-27 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: a tiny read/write dot notation wrapper for Hash, JSON, YAML, and ENV
|
14
14
|
email: jefflunt@gmail.com
|
@@ -36,7 +36,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
36
36
|
- !ruby/object:Gem::Version
|
37
37
|
version: '0'
|
38
38
|
requirements: []
|
39
|
-
rubygems_version: 3.
|
39
|
+
rubygems_version: 3.3.7
|
40
40
|
signing_key:
|
41
41
|
specification_version: 4
|
42
42
|
summary: want to have easy dot notation access to data that comes from Hashes, JSON,
|