gloo-md 1.2 → 1.3

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 +4 -4
  2. data/lib/md_doc.rb +28 -6
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 26d3d6ba34f0daaa4881b63fa84848e8ad01043d34357e67d93aad97d85bfe64
4
- data.tar.gz: cd27b868d2846861b17031cbf2076c24b1f1d996238f208d5e2a1760533086d3
3
+ metadata.gz: 2bfa6b6b1b63e61071e271da7649cb0d13d0dd7a6ab8f0bcfaeb455e6325efcc
4
+ data.tar.gz: 1fcd9683e59f38daf40984bc0d711fa44eac8ca3d9f39a7e6ce21d2b98568de6
5
5
  SHA512:
6
- metadata.gz: 670f1cc4ca37f23fcd67ae241c26bb8c334d9cef5a49de5b5d4b3ac587e629d5f02a3999fdf390c1642c94aeec829b63ccbc4bb6da49a338fb317d81ff0ca2ba
7
- data.tar.gz: cc147be4c2af1f0a82e64e504e397d6f76f32cc11e34c38d85645e6ef69324339d67b34f416e5d1a3c948cf9abfa5c37fe48236745031712a487c4c29d77aefd
6
+ metadata.gz: 0ef1a0fc0d81d9fc7c145a5385b70dd848fc1de52c74c2c833175c998b04f8eded160ef9241ee579c1a723229f26b8036af4dd2dde7048871358c8b58730513e
7
+ data.tar.gz: a3572eb4470c11e401fef0628b1e506f24b628a07cc8115819abc4f59db7dc59d059591a5f4ef6422c72f4dd7f2fa5e43a4c7dba05d813e218fba1813920040b
data/lib/md_doc.rb CHANGED
@@ -79,7 +79,9 @@ class MdDoc < Gloo::Core::Obj
79
79
 
80
80
  #
81
81
  # Read the file at path, parse frontmatter and body, populate children.
82
- # Frontmatter children are created dynamically from whatever keys are present.
82
+ # Only scalar frontmatter values become gloo string children; complex values
83
+ # (arrays, nested hashes) are skipped — they are preserved on write by
84
+ # re-reading the file.
83
85
  #
84
86
  def msg_read
85
87
  path = resolve_path
@@ -96,6 +98,7 @@ class MdDoc < Gloo::Core::Obj
96
98
  fm_can = find_child FRONTMATTER
97
99
  if fm_can && fm_hash
98
100
  fm_hash.each do |key, val|
101
+ next unless scalar?( val )
99
102
  child = fm_can.find_add_child( key.to_s, 'string' )
100
103
  child.set_value val.to_s
101
104
  end
@@ -107,25 +110,35 @@ class MdDoc < Gloo::Core::Obj
107
110
 
108
111
  #
109
112
  # Serialize frontmatter and body children back to the file at path.
110
- # Creates the file if it does not exist.
113
+ # Re-reads the current file to get the base hash (preserving arrays and other
114
+ # complex values), then overlays the scalar children which may have been
115
+ # modified. Creates the file if it does not exist.
111
116
  #
112
117
  def msg_write
113
118
  path = resolve_path
114
119
  return unless path
115
120
 
121
+ expanded = File.expand_path( path )
122
+
123
+ # Re-read the file so arrays and nested hashes survive unchanged.
124
+ base = {}
125
+ if File.exist?( expanded )
126
+ base, _ = parse_frontmatter( File.read( expanded ) )
127
+ end
128
+
116
129
  fm_can = find_child FRONTMATTER
117
- fm_hash = {}
130
+
131
+ # Overlay scalar children; updating an existing key preserves its position.
118
132
  if fm_can
119
133
  fm_can.children.each do |child|
120
- fm_hash[ child.name ] = child.value
134
+ base[ child.name ] = child.value
121
135
  end
122
136
  end
123
137
 
124
138
  body = find_child BODY
125
139
  body_text = body ? body.value.to_s : ''
126
140
 
127
- content = build_content( fm_hash, body_text )
128
- File.write( File.expand_path( path ), content )
141
+ File.write( expanded, build_content( base, body_text ) )
129
142
  end
130
143
 
131
144
 
@@ -135,6 +148,15 @@ class MdDoc < Gloo::Core::Obj
135
148
 
136
149
  private
137
150
 
151
+ #
152
+ # True for scalar YAML values that can be stored as gloo string children.
153
+ # Arrays, hashes, and other complex types are skipped on read.
154
+ #
155
+ def scalar?( val )
156
+ val.is_a?( String ) || val.is_a?( Integer ) || val.is_a?( Float ) ||
157
+ val.is_a?( TrueClass ) || val.is_a?( FalseClass ) || val.nil?
158
+ end
159
+
138
160
  #
139
161
  # Get the expanded path string from the path child.
140
162
  #
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gloo-md
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.2'
4
+ version: '1.3'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Crane