tiny_dot 2.3.2 → 3.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/tiny_dot.rb +62 -78
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f31f44aa3ecf2c601378a248041012313341a63f75db817baa6df5d82be0151e
|
4
|
+
data.tar.gz: 1787bff26e5ba21dfab2f696aa33bd4f17e0b35100073963db99e348e09081bc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9ec17f00dde5f6a9df6c7a8cb431b4bc3a349a5f84c8ffba1cbcea6b720ec1764cd635183132d6590e71e34ad49c84417b6c051bda6220bd44cb12dc7031c230
|
7
|
+
data.tar.gz: 25e0fc3511f15e829a886cfb9c29d7fce6d891c2af8d1032ba39fc5a062da8efbe8d6224d5091ad4923f700fae51ab64ff59974dcb89bdaca820e210b6a08c8c
|
data/lib/tiny_dot.rb
CHANGED
@@ -1,95 +1,79 @@
|
|
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
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
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
|
+
#
|
31
|
+
# Because this module constructs a series of nested Structs from Hash-like
|
32
|
+
# inputs, it means you can read/write data pretty easily:
|
33
|
+
#
|
34
|
+
# > s = TinyDot.from_json(<some deeply nested JSON>)
|
35
|
+
# => <struct ... >
|
36
|
+
# > s.foo.bar.baz
|
37
|
+
# => 5
|
38
|
+
# > s.foo.bar.baz = 99
|
39
|
+
# < s.foo.bar.baz
|
40
|
+
# => 99
|
41
|
+
module TinyDot
|
42
|
+
class << self
|
43
|
+
def from_env
|
44
|
+
_hash_to_struct(ENV.to_h)
|
45
|
+
end
|
44
46
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
47
|
+
def from_yaml(yaml_string)
|
48
|
+
yaml = Psych.load(yaml_string)
|
49
|
+
_hash_to_struct(yaml)
|
50
|
+
end
|
49
51
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
52
|
+
def self.from_json(json_string)
|
53
|
+
json = Oj.load(json_string)
|
54
|
+
_hash_to_struct(json)
|
55
|
+
end
|
54
56
|
|
55
|
-
|
56
|
-
|
57
|
-
|
57
|
+
def self.from_hash(hash)
|
58
|
+
_hash_to_struct(json)
|
59
|
+
end
|
58
60
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
61
|
+
def _hash_to_struct(hash)
|
62
|
+
case hash
|
63
|
+
when Hash
|
64
|
+
struct = ::Struct.new(*_to_attr_friendly_symbols(hash.keys))
|
65
|
+
struct.new(*hash.values.map { |v| _hash_to_struct(v) })
|
66
|
+
when Array
|
67
|
+
hash.map { |v| _hash_to_struct(v) }
|
65
68
|
else
|
66
|
-
|
67
|
-
TinyDot.new(@data[ms])
|
68
|
-
else
|
69
|
-
TinyDot.new({})
|
70
|
-
end
|
69
|
+
hash
|
71
70
|
end
|
72
|
-
else
|
73
|
-
TinyDot.new({})
|
74
71
|
end
|
75
|
-
end
|
76
|
-
|
77
|
-
def ==(other)
|
78
|
-
@data == other.to_hash
|
79
|
-
end
|
80
72
|
|
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
|
73
|
+
def _to_attr_friendly_symbols(keys)
|
74
|
+
keys
|
75
|
+
.map{|k| k.gsub(/[-\s]/, '_') }
|
76
|
+
.map(&:to_sym)
|
77
|
+
end
|
94
78
|
end
|
95
79
|
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.1
|
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
|