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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/tiny_dot.rb +51 -78
  3. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8d79073c7292e31f88919b88dcf146c42118d81e94a2e94a3bfe7712b22124bd
4
- data.tar.gz: 59dff524acb74dc64ed050e2cc50ed4bd889199656782968326a5ec135ee0a9c
3
+ metadata.gz: 44f7c9f36e7b3d775cf41774060f0144b5db9006922f271a246aed612288d2bc
4
+ data.tar.gz: db865874e1f5b854d6b7b34362e1e5efab6f8340551c5725007f09cd0bc67ca9
5
5
  SHA512:
6
- metadata.gz: 1dfd42dfc4bc8bf15f94a5198c80124394b6a9302c70c731b9de69acad6fb57fc22706eda6a144932bb6c11bf200f21402d5886fa8c97f98a94da25bb285dcaf
7
- data.tar.gz: 7cd0fdf624c556b0993ca411a5824b5aae0f0fe453b20980b76d1a14410d125403e703eff9cec35abfbdf342bae9e90f2aea287c526c4a61227c9e08536ee019
6
+ metadata.gz: c5bddb3ad6272bb1b3362f79be814ed6c4bc8585b9d1639595055c44c7d9969da4c099a0228ed389cae4eeab3453ac448cdc5570fee0228e5feefb476f01c624
7
+ data.tar.gz: 953bcffe9fc13b98ed22b40fdd7a56bde7f889eb775d90eb3b9480e94167573fa875b07f57be6ea887d0a92010e7563827d0e2068b2dc89f54165c081e2ed909
data/lib/tiny_dot.rb CHANGED
@@ -1,95 +1,68 @@
1
- require 'yaml'
2
- require 'json'
1
+ require 'psych'
2
+ require 'oj'
3
3
 
4
- # this class gives you read-only access to Hases, JSON, and YAML files using
5
- # dot notation. it makes clever use of #method_missing to allow you to do the
6
- # following:
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
- # > t = TinyDot.new({ 'foo' => { 'bar' => 'baz' }})
9
- # > t.foo # returns another instance of TinyDot
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
- # ... in other words, it gives you a convenient dot notation syntax for
15
- # accessing nested hashes. you can chain calls to this and you'll get a new
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
- # if you add '!' to the last method in a chain it will return the value under
19
- # that key.
14
+ # The `from_json` method takes a string containing JSON data and converts it
15
+ # into a Struct.
20
16
  #
21
- # finally, you can use dot syntax as deep as you want, and if there's no key at
22
- # that level you'll just get `nil' back:
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
- # > t.foo.bar.baz.whatever.as.deep.as.you.want
25
- # => nil
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
- # ... which is sort of safe navigation operator-like without the safe
28
- # navigation operator
29
- class TinyDot
30
- # returns a TinyDot instance from the ENV constant
31
- def self.from_env
32
- TinyDot.new(ENV)
33
- end
34
-
35
- # returns a TinyDot instance after parsing the YAML in the named filename
36
- def self.from_yaml_file(filename, permitted_classes: [])
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
- # returns a TinyDot instance after parsing the JSON in the string
46
- def self.from_json_string(s)
47
- TinyDot.new(JSON.parse(s))
48
- end
36
+ def from_yaml(yaml_string)
37
+ yaml = Psych.load(yaml_string)
38
+ _hash_to_struct(yaml)
39
+ end
49
40
 
50
- # give it a Hash and it'll give you dot notation over it
51
- def initialize(hash={})
52
- @data = hash
53
- end
41
+ def self.from_json(json_string)
42
+ json = Oj.load(json_string)
43
+ _hash_to_struct(json)
44
+ end
54
45
 
55
- def method_missing(m, *args)
56
- val = args.first
57
- ms = m.to_s
46
+ def self.from_hash(hash)
47
+ _hash_to_struct(json)
48
+ end
58
49
 
59
- case @data
60
- when Hash
61
- if ms.end_with?('!')
62
- @data[ms[0..-2]]
63
- elsif ms.end_with?('=')
64
- @data[m[0..-2]] = val
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
- if @data.has_key?(ms)
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
- def to_json
82
- @data.to_json
83
- end
84
-
85
- # NOTE: this returns the raw hash inside of the TinyDot instance, which means
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: 2.3.2
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-26 00:00:00.000000000 Z
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.4.1
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,