strong_yaml 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/schema/config_group.rb +32 -0
- data/lib/schema/config_integer.rb +10 -0
- data/lib/schema/config_list.rb +10 -0
- data/lib/schema/config_string.rb +10 -0
- data/lib/schema/schema.rb +35 -0
- data/lib/strong_yaml.rb +116 -0
- metadata +52 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 4d9fbf0e956d82b087df9a89ee34ed584b87bd15dd96fd69bb3fa7bc4c23568a
|
4
|
+
data.tar.gz: 9ac87edf2ff09038300c7b74f7171332f9e8f4379f9d0daa1a01693aaf70eea1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b524ca35ed3137d72309934f9287fef33d62da0fb77a1303850b7420013c319b1d06520441f614a50b32ffaf9a0e5ef454036a4accb1cdc4842cf831627d7c12
|
7
|
+
data.tar.gz: ac3613443faf96bfcaa0e960f36f66aafb35275fac07a3f1684b57287c6d757108d5a6cfe2b67835db421e38889b7e4adbc8a720f9570bfbd8ff4f03e3b521cb
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require_relative "config_string"
|
2
|
+
require_relative "config_integer"
|
3
|
+
require_relative "config_list"
|
4
|
+
|
5
|
+
module StrongYAML
|
6
|
+
class ConfigGroup
|
7
|
+
attr_reader :name, :elements
|
8
|
+
|
9
|
+
def initialize(name)
|
10
|
+
@name = name
|
11
|
+
@elements = []
|
12
|
+
end
|
13
|
+
|
14
|
+
def group(name, &block)
|
15
|
+
group = StrongYAML::ConfigGroup.new(name)
|
16
|
+
@elements << group
|
17
|
+
group.instance_eval(&block)
|
18
|
+
end
|
19
|
+
|
20
|
+
def string(name, **args)
|
21
|
+
@elements << StrongYAML::ConfigString.new(name, **args)
|
22
|
+
end
|
23
|
+
|
24
|
+
def integer(name, **args)
|
25
|
+
@elements << StrongYAML::ConfigInteger.new(name, **args)
|
26
|
+
end
|
27
|
+
|
28
|
+
def list(name, **args)
|
29
|
+
@elements << StrongYAML::ConfigList.new(name, **args)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require_relative "config_group"
|
2
|
+
require_relative "config_string"
|
3
|
+
require_relative "config_integer"
|
4
|
+
require_relative "config_list"
|
5
|
+
|
6
|
+
module StrongYAML
|
7
|
+
class ConfigSchema
|
8
|
+
attr_reader :elements
|
9
|
+
|
10
|
+
class SchemaError < StandardError
|
11
|
+
end
|
12
|
+
|
13
|
+
def initialize
|
14
|
+
@elements = []
|
15
|
+
end
|
16
|
+
|
17
|
+
def group(name, &block)
|
18
|
+
group = StrongYAML::ConfigGroup.new(name)
|
19
|
+
@elements << group
|
20
|
+
group.instance_eval(&block)
|
21
|
+
end
|
22
|
+
|
23
|
+
def string(name, **args)
|
24
|
+
@elements << StrongYAML::ConfigString.new(name, **args)
|
25
|
+
end
|
26
|
+
|
27
|
+
def integer(name, **args)
|
28
|
+
@elements << StrongYAML::ConfigInteger.new(name, **args)
|
29
|
+
end
|
30
|
+
|
31
|
+
def list(name, **args)
|
32
|
+
@elements << StrongYAML::ConfigList.new(name, **args)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/lib/strong_yaml.rb
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
require "yaml"
|
2
|
+
require "json"
|
3
|
+
require "ostruct"
|
4
|
+
require "fileutils"
|
5
|
+
require_relative "schema/schema"
|
6
|
+
|
7
|
+
module StrongYAML
|
8
|
+
def self.included(base)
|
9
|
+
base.extend(ClassMethods)
|
10
|
+
end
|
11
|
+
|
12
|
+
module ClassMethods
|
13
|
+
def file(config_location)
|
14
|
+
@config_location = config_location
|
15
|
+
end
|
16
|
+
|
17
|
+
def schema(&block)
|
18
|
+
@schema = StrongYAML::ConfigSchema.new
|
19
|
+
@schema.instance_eval(&block)
|
20
|
+
end
|
21
|
+
|
22
|
+
def load
|
23
|
+
@config = YAML.load_file(@config_location)
|
24
|
+
|
25
|
+
if @schema
|
26
|
+
@schema.elements.each do |schema_entry|
|
27
|
+
obj = build_schema_entry schema_entry, @config, []
|
28
|
+
|
29
|
+
define_singleton_method(schema_entry.name) do
|
30
|
+
obj
|
31
|
+
end
|
32
|
+
end
|
33
|
+
else
|
34
|
+
@config.each do |key, value|
|
35
|
+
define_singleton_method(key) do
|
36
|
+
if value.is_a? Hash
|
37
|
+
JSON.parse value.to_json, object_class: OpenStruct
|
38
|
+
else
|
39
|
+
value
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def create_or_load
|
47
|
+
if File.exist? @config_location
|
48
|
+
load
|
49
|
+
elsif @schema
|
50
|
+
File.write(@config_location, hash_from_schema.to_yaml)
|
51
|
+
|
52
|
+
load
|
53
|
+
else
|
54
|
+
FileUtils.touch(@config_location)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
def build_schema_entry(schema_entry, config, path)
|
61
|
+
if schema_entry.is_a? StrongYAML::ConfigGroup
|
62
|
+
obj = OpenStruct.new
|
63
|
+
schema_entry.elements.each do |se|
|
64
|
+
obj[se.name] = build_schema_entry(se, config, path + [schema_entry.name])
|
65
|
+
end
|
66
|
+
obj
|
67
|
+
else
|
68
|
+
path << schema_entry.name
|
69
|
+
value = config.dig(*path.map(&:to_s))
|
70
|
+
|
71
|
+
case schema_entry
|
72
|
+
when StrongYAML::ConfigString
|
73
|
+
if value.nil? || value.is_a?(String)
|
74
|
+
value
|
75
|
+
else
|
76
|
+
raise StrongYAML::ConfigSchema::SchemaError, "#{path.map(&:to_s).join(".")} is of type #{value.class}, but should be String"
|
77
|
+
end
|
78
|
+
when StrongYAML::ConfigInteger
|
79
|
+
if value.nil? || value.is_a?(Integer)
|
80
|
+
value
|
81
|
+
else
|
82
|
+
raise StrongYAML::ConfigSchema::SchemaError, "#{path.map(&:to_s).join(".")} is of type #{value.class}, but should be Integer"
|
83
|
+
end
|
84
|
+
when StrongYAML::ConfigList
|
85
|
+
if value.nil? || value.is_a?(Array)
|
86
|
+
value
|
87
|
+
else
|
88
|
+
raise StrongYAML::ConfigSchema::SchemaError, "#{path.map(&:to_s).join(".")} is of type #{value.class}, but should be Array"
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def hash_from_schema
|
95
|
+
hash = {}
|
96
|
+
|
97
|
+
@schema.elements.each do |schema_entry|
|
98
|
+
hash[schema_entry.name.to_s] = value_from_schema_entry(schema_entry)
|
99
|
+
end
|
100
|
+
|
101
|
+
hash
|
102
|
+
end
|
103
|
+
|
104
|
+
def value_from_schema_entry(schema_entry)
|
105
|
+
if schema_entry.is_a? StrongYAML::ConfigGroup
|
106
|
+
hash = {}
|
107
|
+
schema_entry.elements.each do |se|
|
108
|
+
hash[se.name.to_s] = value_from_schema_entry(se)
|
109
|
+
end
|
110
|
+
hash
|
111
|
+
else
|
112
|
+
schema_entry.default
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
metadata
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: strong_yaml
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Philipp Schlesinger
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-11-15 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Allows for easy YAML parsing and type checking, as well as generators
|
14
|
+
email:
|
15
|
+
- info@philcomm.dev
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/schema/config_group.rb
|
21
|
+
- lib/schema/config_integer.rb
|
22
|
+
- lib/schema/config_list.rb
|
23
|
+
- lib/schema/config_string.rb
|
24
|
+
- lib/schema/schema.rb
|
25
|
+
- lib/strong_yaml.rb
|
26
|
+
homepage: http://rubygems.org/gems/strong_yaml
|
27
|
+
licenses:
|
28
|
+
- MIT
|
29
|
+
metadata:
|
30
|
+
documentation_uri: https://github.com/OfficialPhilcomm/strong_yaml
|
31
|
+
source_code_uri: https://github.com/OfficialPhilcomm/strong_yaml
|
32
|
+
changelog_uri: https://github.com/OfficialPhilcomm/strong_yaml/blob/master/changelog.md
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options: []
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: 3.1.0
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
requirements: []
|
48
|
+
rubygems_version: 3.3.7
|
49
|
+
signing_key:
|
50
|
+
specification_version: 4
|
51
|
+
summary: Easy YAML type checking
|
52
|
+
test_files: []
|