gloo-yaml 1.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 (5) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +19 -0
  3. data/lib/gloo-yaml.rb +18 -0
  4. data/lib/yaml_obj.rb +156 -0
  5. metadata +48 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1657fd9808c1094ce1bacf4e032a696b517bc857af07eabda0a387064afbdd31
4
+ data.tar.gz: db60fc7cff408eabc5acc9f31165b1e3a3a1104faf4a32df0b3372f204be4f25
5
+ SHA512:
6
+ metadata.gz: 2d5a8be48fed57b74c6a073e961b1481e2cd50bb89a6dfbe6105e301d08fa1fadf77c977963e6d93043eea286ed6f7ff06c68ffe5163cf8b64551f4f03c58900
7
+ data.tar.gz: dc61a19b20c2d39212546c6a931a8ca532302e223efef90bab12838d459b75a091343f2bef1b04aa8ab01cd539dce25b1efcfdbfdc4ec851d514d66101671c28
data/README.md ADDED
@@ -0,0 +1,19 @@
1
+ # gloo-yaml
2
+
3
+ A core gloo library for YAML file operations.
4
+
5
+ ## Usage
6
+
7
+ Load this library in a gloo script:
8
+
9
+ ```gloo
10
+ > load lib yaml
11
+ ```
12
+
13
+ ## Objects
14
+
15
+ - `yaml` (shortcut `yml`) — a YAML file object, holding a path to a YAML file and its parsed data
16
+
17
+ ## Full Reference
18
+
19
+ The object's children and messages are documented in-app once the library is loaded — `help> object yaml`.
data/lib/gloo-yaml.rb ADDED
@@ -0,0 +1,18 @@
1
+ #
2
+ # Shim to allow `require 'gloo-yaml'`
3
+ #
4
+ require 'yaml_obj'
5
+
6
+ #
7
+ # Registers the extension.
8
+ #
9
+ class YamlInit < Gloo::Plugin::Base
10
+
11
+ #
12
+ # Register verbs and objects.
13
+ #
14
+ def register( callback )
15
+ callback.register_obj( YamlObj )
16
+ end
17
+
18
+ end
data/lib/yaml_obj.rb ADDED
@@ -0,0 +1,156 @@
1
+ # Author:: Eric Crane (mailto:eric.crane@mac.com)
2
+ # Copyright:: Copyright (c) 2026 Eric Crane. All rights reserved.
3
+ #
4
+ # A YAML file object. Holds a path to a YAML file and supports
5
+ # loading and saving named fields via a container object.
6
+ #
7
+ require 'yaml'
8
+
9
+ class YamlObj < Gloo::Core::Obj
10
+
11
+ KEYWORD = 'yaml'.freeze
12
+ KEYWORD_SHORT = 'yml'.freeze
13
+
14
+ #
15
+ # The name of the object type.
16
+ #
17
+ def self.typename
18
+ return KEYWORD
19
+ end
20
+
21
+ #
22
+ # The short name of the object type.
23
+ #
24
+ def self.short_typename
25
+ return KEYWORD_SHORT
26
+ end
27
+
28
+ #
29
+ # Set the value with any necessary type conversions.
30
+ #
31
+ def set_value( new_value )
32
+ self.value = new_value.to_s
33
+ end
34
+
35
+
36
+ # ---------------------------------------------------------------------
37
+ # Messages
38
+ # ---------------------------------------------------------------------
39
+
40
+ #
41
+ # Get a list of message names that this object receives.
42
+ #
43
+ def self.messages
44
+ return super + %w[load save]
45
+ end
46
+
47
+ #
48
+ # Load fields from the YAML file into a container.
49
+ # The param is a path to a container object whose children
50
+ # are matched by name to YAML keys.
51
+ #
52
+ def msg_load
53
+ return unless @params&.token_count&.positive?
54
+
55
+ container = resolve_container
56
+ return unless container
57
+
58
+ data = read_yaml_file
59
+ return unless data
60
+
61
+ container.children.each do |child|
62
+ key = child.name
63
+ child.set_value data[ key ].to_s if data.key?( key )
64
+ end
65
+ end
66
+
67
+ #
68
+ # Save fields from a container into the YAML file.
69
+ # The param is a path to a container object whose children
70
+ # are matched by name to YAML keys.
71
+ #
72
+ def msg_save
73
+ return unless @params&.token_count&.positive?
74
+
75
+ container = resolve_container
76
+ return unless container
77
+
78
+ data = read_yaml_file || {}
79
+
80
+ container.children.each do |child|
81
+ data[ child.name ] = child.value
82
+ end
83
+
84
+ write_yaml_file data
85
+ end
86
+
87
+
88
+ # ---------------------------------------------------------------------
89
+ # Helpers
90
+ # ---------------------------------------------------------------------
91
+
92
+ private
93
+
94
+ #
95
+ # Resolve the container object from the first param.
96
+ #
97
+ def resolve_container
98
+ pn = Gloo::Core::Pn.new( @engine, @params.first )
99
+ pn.resolve
100
+ end
101
+
102
+ #
103
+ # Read and parse the YAML file. Returns a hash, or nil on error.
104
+ #
105
+ def read_yaml_file
106
+ path = File.expand_path( self.value )
107
+ unless File.exist?( path )
108
+ @engine.log.error "YAML file not found: #{path}"
109
+ return nil
110
+ end
111
+ YAML.load_file( path ) || {}
112
+ end
113
+
114
+ #
115
+ # Serialize data hash and write it back to the YAML file.
116
+ #
117
+ def write_yaml_file( data )
118
+ path = File.expand_path( self.value )
119
+ File.write( path, data.to_yaml )
120
+ end
121
+
122
+ # ---------------------------------------------------------------------
123
+ # Object Documentation
124
+ # ---------------------------------------------------------------------
125
+
126
+ #
127
+ # Get the object's documentation data.
128
+ #
129
+ def self.doc_data
130
+ {
131
+ :name => KEYWORD,
132
+ :shortcut => KEYWORD_SHORT,
133
+ :description => 'A YAML file object. Holds a path to a YAML ' \
134
+ 'file (as its own value) and supports loading and saving ' \
135
+ 'named fields via a container object.',
136
+ :messages => [
137
+ 'load ({container.path}) — Load fields from the YAML file into the given container. Children of the container are matched by name to YAML keys. A parameter is required.',
138
+ 'save ({container.path}) — Save fields from the given container into the YAML file, matching container children by name to YAML keys. A parameter is required.'
139
+ ],
140
+ :notes => 'No vault documentation exists for this object type — ' \
141
+ 'this was authored directly from the code.',
142
+ :examples => <<~EXAMPLES.strip
143
+ settings [can] :
144
+ path [yaml] : ~/.my_app/settings.yml
145
+ data [can] :
146
+ name [string] :
147
+ theme [string] :
148
+ on_load [script] :
149
+ tell path to load (data)
150
+ put 'dark' into data.theme
151
+ tell path to save (data)
152
+ EXAMPLES
153
+ }
154
+ end
155
+
156
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gloo-yaml
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ platform: ruby
6
+ authors:
7
+ - Eric Crane
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2026-09-08 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Adds YAML file read/write support to Gloo.
14
+ email:
15
+ - eric.crane@mac.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - README.md
21
+ - lib/gloo-yaml.rb
22
+ - lib/yaml_obj.rb
23
+ homepage: https://github.com/ecrane/gloo
24
+ licenses:
25
+ - MIT
26
+ metadata:
27
+ gloo.type: core-library
28
+ documentation_uri: https://github.com/ecrane/gloo
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubygems_version: 3.5.16
45
+ signing_key:
46
+ specification_version: 4
47
+ summary: Gloo core library. YAML file support.
48
+ test_files: []