dot_options 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +44 -0
- data/lib/dot_options/version.rb +3 -0
- data/lib/dot_options.rb +66 -0
- metadata +49 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: fb3d62c652e1453a6a6d4fcc5df1334c3998c257089fe8ac455bee3b93a7a3b2
|
4
|
+
data.tar.gz: d52505910635b560f397d91d59a49001a625011eff3971e246791d46c62e65bd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8c1e1384ad4a64014e35921c903a7994e38c97cab01441e3716ce659b5e1051390d9755065e6e6c4f8ec5af2507c34f32011569b5a080908ff219100c3c8f75d
|
7
|
+
data.tar.gz: 2e6a4e3df06791a9eae4b10d2eac41b29bb814b0392f0ae600ee6d47fc8e99616aa975f37b8c1dd6e40b4df4ff53f0d3bb32ed46f735d70364df22b623c194ff
|
data/README.md
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# DotOptions - Options object with dot-notation access
|
2
|
+
|
3
|
+
Convert any hash to a an object with deep read/write dot-notation access.
|
4
|
+
|
5
|
+
[![Gem Version](https://badge.fury.io/rb/dot_options.svg)](https://badge.fury.io/rb/dot_options)
|
6
|
+
[![Build Status](https://github.com/DannyBen/dot_options/workflows/Test/badge.svg)](https://github.com/DannyBen/dot_options/actions?query=workflow%3ATest)
|
7
|
+
|
8
|
+
---
|
9
|
+
|
10
|
+
## Install
|
11
|
+
|
12
|
+
```
|
13
|
+
$ gem install dot_options
|
14
|
+
```
|
15
|
+
|
16
|
+
## Usage
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
# Initialize a DotOptions object with a hash:
|
20
|
+
opts = {
|
21
|
+
debug: true,
|
22
|
+
output: { color: true },
|
23
|
+
skin: { background: { color: :black, texture: 'Stripes' } },
|
24
|
+
}
|
25
|
+
options = DotOptions.new opts
|
26
|
+
|
27
|
+
# Read any option with dot-notation:
|
28
|
+
p options.skin.background.color # => :black
|
29
|
+
|
30
|
+
# Update any option with dot-notation:
|
31
|
+
options.skin.background.color = :black
|
32
|
+
|
33
|
+
# ... or create an entire branch by providing a hash
|
34
|
+
options.skin.foreground = { color: :white, font: 'JetBrains Mono' }
|
35
|
+
p options.skin.foreground.font # => 'JetBrains Mono'
|
36
|
+
```
|
37
|
+
|
38
|
+
## Contributing / Support
|
39
|
+
|
40
|
+
If you experience any issue, have a question or a suggestion, or if you wish
|
41
|
+
to contribute, feel free to [open an issue][issues].
|
42
|
+
|
43
|
+
|
44
|
+
[issues]: https://github.com/DannyBen/dot_options/issues
|
data/lib/dot_options.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
class DotOptions
|
2
|
+
OptionNotFoundError = Class.new StandardError
|
3
|
+
|
4
|
+
attr_reader :options, :key, :parent
|
5
|
+
|
6
|
+
def initialize(options = {}, key: nil, parent: nil)
|
7
|
+
@options = options
|
8
|
+
@key = key
|
9
|
+
@parent = parent
|
10
|
+
build_options
|
11
|
+
end
|
12
|
+
|
13
|
+
def inspect
|
14
|
+
"{ #{options.map { |key, value| "#{key}: #{value.inspect}" }.join(', ')} }"
|
15
|
+
end
|
16
|
+
|
17
|
+
def to_s
|
18
|
+
flat_options.map { |key, value| "#{key} = #{value.inspect}" }.join "\n"
|
19
|
+
end
|
20
|
+
|
21
|
+
def flat_options(prefix = nil)
|
22
|
+
result = {}
|
23
|
+
options.each do |key, value|
|
24
|
+
full_key = [prefix, key].compact.join '.'
|
25
|
+
opts = value.is_a?(DotOptions) ? value.flat_options(full_key) : { full_key => value }
|
26
|
+
result.merge! opts
|
27
|
+
end
|
28
|
+
|
29
|
+
result
|
30
|
+
end
|
31
|
+
|
32
|
+
def method_missing(name, *args)
|
33
|
+
if name.to_s.end_with?('=')
|
34
|
+
key = name.to_s.chomp('=').to_sym
|
35
|
+
options[key] = args.first
|
36
|
+
build_options if args.first.is_a? Hash
|
37
|
+
elsif options.has_key? name
|
38
|
+
options[name]
|
39
|
+
else
|
40
|
+
raise OptionNotFoundError, "Option '#{full_path(name)}' not found"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def respond_to_missing?(name, include_private = false)
|
45
|
+
options.has_key?(name.to_s.chomp('=').to_sym) || super
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def build_options
|
51
|
+
options.each do |key, value|
|
52
|
+
options[key] = DotOptions.new(value, key: key, parent: self) if value.is_a?(Hash)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def full_path(name)
|
57
|
+
ancestors = [name]
|
58
|
+
current = self
|
59
|
+
while current.is_a? DotOptions
|
60
|
+
ancestors.unshift(current.key) if current.key
|
61
|
+
current = current.parent
|
62
|
+
end
|
63
|
+
|
64
|
+
ancestors.join '.'
|
65
|
+
end
|
66
|
+
end
|
metadata
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dot_options
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Danny Ben Shitrit
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-07-30 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Convert any hash to a deep dot-notation object
|
14
|
+
email: db@dannyben.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- README.md
|
20
|
+
- lib/dot_options.rb
|
21
|
+
- lib/dot_options/version.rb
|
22
|
+
homepage: https://github.com/dannyben/dot_notation
|
23
|
+
licenses:
|
24
|
+
- MIT
|
25
|
+
metadata:
|
26
|
+
bug_tracker_uri: https://github.com/DannyBen/dot_options/issues
|
27
|
+
changelog_uri: https://github.com/DannyBen/dot_options/blob/master/CHANGELOG.md
|
28
|
+
source_code_uri: https://github.com/DannyBen/dot_options
|
29
|
+
rubygems_mfa_required: 'true'
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options: []
|
32
|
+
require_paths:
|
33
|
+
- lib
|
34
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '3.1'
|
39
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
requirements: []
|
45
|
+
rubygems_version: 3.5.14
|
46
|
+
signing_key:
|
47
|
+
specification_version: 4
|
48
|
+
summary: Options object with dot-notation support
|
49
|
+
test_files: []
|