yaml_to_struct 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.
- checksums.yaml +7 -0
- data/lib/yaml_to_struct.rb +31 -0
- metadata +44 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 0523fc7b85af29be4cd593b68e3fed312f1c1e59a02f563b6939532f02493b32
|
4
|
+
data.tar.gz: 1b388c40f9aa8cf59fbf09af0925eb700e6b76369384a6a744cd50528e8a1ce8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ad0f458215d4dc222c716974d8fb17c89b6a38952cdbda5a655f34b5ecd252445d5cf84689cc93dc9d3958d0c9fc1338968e4f124fe3cf1b2bef3727969c8d0c
|
7
|
+
data.tar.gz: f369de89e1285361cff62be142a0fa581ffe6d3b64a0cae8f88d78be30fd1719ea10a34377a41d9d9a519c65b13660f1e8715f9231cf0c2b3aab1ebb9bbc426e
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'psych'
|
2
|
+
|
3
|
+
# YamlToStruct
|
4
|
+
#
|
5
|
+
# This module provides a way to convert YAML string to a nested Struct. It
|
6
|
+
# converts the YAML keys to Symbols before sending them to the Struct for
|
7
|
+
# initialization.
|
8
|
+
#
|
9
|
+
# Example:
|
10
|
+
# yaml_string = 'foo: {bar: {baz: 1}}'
|
11
|
+
# struct = YamlToStruct.load(yaml_string)
|
12
|
+
# struct.foo.bar.baz #=> 1
|
13
|
+
module YamlToStruct
|
14
|
+
def self.load(yaml_string)
|
15
|
+
yaml = Psych.load(yaml_string)
|
16
|
+
_load_helper(yaml)
|
17
|
+
end
|
18
|
+
|
19
|
+
def self._load_helper(yaml)
|
20
|
+
case yaml
|
21
|
+
when Hash
|
22
|
+
struct = ::Struct.new(*yaml.keys.map(&:to_sym))
|
23
|
+
struct.new(*yaml.values.map { |v| _load_helper(v) })
|
24
|
+
when Array
|
25
|
+
yaml.map { |v| _load_helper(v) }
|
26
|
+
else
|
27
|
+
yaml
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
metadata
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: yaml_to_struct
|
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: 2023-01-27 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: converts a Yaml String to a nested Struct
|
14
|
+
email: jefflunt@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/yaml_to_struct.rb
|
20
|
+
homepage: https://github.com/jefflunt/yaml_to_struct
|
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.4.1
|
40
|
+
signing_key:
|
41
|
+
specification_version: 4
|
42
|
+
summary: converts a Yaml String to a tree of nested Struct objects, so you can use
|
43
|
+
dot notation
|
44
|
+
test_files: []
|