tiny_dot 1.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 +7 -0
  2. data/lib/tiny_dot.rb +63 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a504ee11ac88fe637eda112ecb1a4f85cc8de98e652962666ecbd808c091837d
4
+ data.tar.gz: e43d65b6e60f61bb02476e40d3dfb9f28ef0651d5b0d984f6dad63d7579fbdf8
5
+ SHA512:
6
+ metadata.gz: 752a76684804dec7a794d87bc444d7805c04bfd43b2a1cc67ed8818fca219753df8b07015912b9e8088938b0283264615fd1b48d1923604e763a22384c876488
7
+ data.tar.gz: aba04b5338a31643d859acbeefb456cd868959ecab8f3b7fa6c3744afa85b3deb112518351320ee90d66cb932df18b86fa1725ef0593d51a06ea09cdb44dbcff
data/lib/tiny_dot.rb ADDED
@@ -0,0 +1,63 @@
1
+ require 'yaml'
2
+ require 'json'
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:
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' }
13
+ #
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.
17
+ #
18
+ # if you add '!' to the last method in a chain it will return the value under
19
+ # that key.
20
+ #
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:
23
+ #
24
+ # > t.foo.bar.baz.whatever.as.deep.as.you.want
25
+ # => nil
26
+ #
27
+ # ... which is sort of safe navigation operator-like without the save
28
+ # navigation operator
29
+ class TinyDot
30
+ # returns a TinyDot instance after parsing the YAML in the named filename
31
+ def self.from_yaml_file(filename)
32
+ TinyDot.new(YAML.safe_load_file(filename))
33
+ end
34
+
35
+ # returns a TinyDot instance after parsing the JSON in the named filename
36
+ def self.from_json_file(filename)
37
+ TinyDot.new(JSON.parse(IO.read(filename)))
38
+ end
39
+
40
+ # give it a Hash and it'll give you dot notation over it
41
+ def initialize(hash)
42
+ @data = hash
43
+ end
44
+
45
+ def method_missing(m)
46
+ ms = m.to_s
47
+
48
+ case @data
49
+ when Hash
50
+ if ms.end_with?('!')
51
+ @data[ms[0..-2]]
52
+ else
53
+ if @data.has_key?(ms)
54
+ TinyDot.new(@data[ms])
55
+ else
56
+ TinyDot.new({})
57
+ end
58
+ end
59
+ else
60
+ TinyDot.new({})
61
+ end
62
+ end
63
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tiny_dot
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jeff Lunt
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-11-18 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: a tiny read-only, dot notation wrapper for Hash, JSON, and YAML
14
+ email: jefflunt@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/tiny_dot.rb
20
+ homepage: https://github.com/jefflunt/tiny_dot
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubygems_version: 3.0.3.1
40
+ signing_key:
41
+ specification_version: 4
42
+ summary: want to have easy dot notation access to data that comes from Hashes, JSON,
43
+ and YAML - and absolutely nothing else? then this is the library for you.
44
+ test_files: []