Module
A stub yaml serializer that can handle only hashes and strings (as of now).
Constants
No documentation available
No documentation available
Instance Methods
lib/rubygems/yaml_serializer.rb
View on GitHub
# File tmp/rubies/ruby-3.3.0/lib/rubygems/yaml_serializer.rb, line 84
def convert_to_backward_compatible_key!(key)
key << "/" if /https?:/i.match?(key) && !%r{/\Z}.match?(key)
key.gsub!(".", "__")
end
for settings’ keys
lib/rubygems/yaml_serializer.rb
View on GitHub
# File tmp/rubies/ruby-3.3.0/lib/rubygems/yaml_serializer.rb, line 8
def dump(hash)
yaml = String.new("---")
yaml << dump_hash(hash)
end
No documentation available
lib/rubygems/yaml_serializer.rb
View on GitHub
# File tmp/rubies/ruby-3.3.0/lib/rubygems/yaml_serializer.rb, line 13
def dump_hash(hash)
yaml = String.new("\n")
hash.each do |k, v|
yaml << k << ":"
if v.is_a?(Hash)
yaml << dump_hash(v).gsub(/^(?!$)/, " ") # indent all non-empty lines
elsif v.is_a?(Array) # Expected to be array of strings
if v.empty?
yaml << " []\n"
else
yaml << "\n- " << v.map {|s| s.to_s.gsub(/\s+/, " ").inspect }.join("\n- ") << "\n"
end
else
yaml << " " << v.to_s.gsub(/\s+/, " ").inspect << "\n"
end
end
yaml
end
No documentation available
lib/rubygems/yaml_serializer.rb
View on GitHub
# File tmp/rubies/ruby-3.3.0/lib/rubygems/yaml_serializer.rb, line 53
def load(str)
res = {}
stack = [res]
last_hash = nil
last_empty_key = nil
str.split(/\r?\n/) do |line|
if match = HASH_REGEX.match(line)
indent, key, quote, val = match.captures
convert_to_backward_compatible_key!(key)
depth = indent.size / 2
if quote.empty? && val.empty?
new_hash = {}
stack[depth][key] = new_hash
stack[depth + 1] = new_hash
last_empty_key = key
last_hash = stack[depth]
else
val = [] if val == "[]" # empty array
stack[depth][key] = val
end
elsif match = ARRAY_REGEX.match(line)
_, val = match.captures
last_hash[last_empty_key] = [] unless last_hash[last_empty_key].is_a?(Array)
last_hash[last_empty_key].push(val)
end
end
res
end
No documentation available