Class: Gem::YAMLSerializer::Emitter
| Relationships & Source Files | |
| Inherits: | Object |
| Defined in: | lib/rubygems/yaml_serializer.rb |
Constant Summary
-
STRING_ESCAPES =
# File 'lib/rubygems/yaml_serializer.rb', line 697{ "\\" => "\\\\", "\"" => "\\\"", "\0" => "\\0", "\a" => "\\a", "\b" => "\\b", "\t" => "\\t", "\n" => "\\n", "\v" => "\\v", "\f" => "\\f", "\r" => "\\r", "\e" => "\\e", }.freeze
Instance Method Summary
- #emit(obj)
- #emit_array(arr, indent) private
- #emit_block_scalar(str, indent) private
- #emit_dependency(dep, indent) private
- #emit_hash(hash, indent) private
- #emit_node(obj, indent, quote: false) private
- #emit_platform(plat, indent) private
- #emit_requirement(req, indent) private
- #emit_specification(spec, indent) private
- #emit_string(str, indent, quote: false) private
- #emit_time(time) private
- #emit_version(ver, indent) private
- #needs_quoting?(str, quote = false) ⇒ Boolean private
- #pad(indent) private
- #quote_string(str) private
Instance Method Details
#emit(obj)
[ GitHub ]# File 'lib/rubygems/yaml_serializer.rb', line 711
def emit(obj) "---#{emit_node(obj, 0)}" end
#emit_array(arr, indent) (private)
[ GitHub ]#emit_block_scalar(str, indent) (private)
[ GitHub ]# File 'lib/rubygems/yaml_serializer.rb', line 827
def emit_block_scalar(str, indent) parts = [str.end_with?("\n") ? " |\n" : " |-\n"] str.each_line do |line| parts << "#{pad(indent + 2)}#{line}" end res = parts.join res << "\n" unless res.end_with?("\n") res end
#emit_dependency(dep, indent) (private)
[ GitHub ]# File 'lib/rubygems/yaml_serializer.rb', line 775
def emit_dependency(dep, indent) [ " !ruby/object:Gem::Dependency\n", "#{pad(indent)}name: #{emit_node(dep.name, indent + 2).lstrip}", "#{pad(indent)}requirement:#{emit_node(dep.requirement, indent + 2)}", "#{pad(indent)}type: #{emit_node(dep.type, indent + 2).lstrip}", "#{pad(indent)}prerelease: #{emit_node(dep.prerelease?, indent + 2).lstrip}", "#{pad(indent)}version_requirements:#{emit_node(dep.requirement, indent + 2)}", ].join end
#emit_hash(hash, indent) (private)
[ GitHub ]# File 'lib/rubygems/yaml_serializer.rb', line 786
def emit_hash(hash, indent) if hash.empty? " {}\n" else parts = ["\n"] hash.each do |k, v| is_symbol = k.is_a?(Symbol) || (k.is_a?(String) && k.start_with?(":")) key_str = k.is_a?(Symbol) ? k.inspect : k.to_s key_str = quote_string(key_str) if !is_symbol && needs_quoting?(key_str) parts << "#{pad(indent)}#{key_str}:#{emit_node(v, indent + 2, quote: is_symbol)}" end parts.join end end
#emit_node(obj, indent, quote: false) (private)
[ GitHub ]# File 'lib/rubygems/yaml_serializer.rb', line 717
def emit_node(obj, indent, quote: false) case obj when Gem::Specification then emit_specification(obj, indent) when Gem::Version then emit_version(obj, indent) when Gem::Platform then emit_platform(obj, indent) when Gem::Requirement then emit_requirement(obj, indent) when Gem::Dependency then emit_dependency(obj, indent) when Hash then emit_hash(obj, indent) when Array then emit_array(obj, indent) when Time then emit_time(obj) when String then emit_string(obj, indent, quote: quote) when NilClass "\n" when Numeric, Symbol, TrueClass, FalseClass " #{obj.inspect}\n" else " #{quote_string(obj.to_s)}\n" end end
#emit_platform(plat, indent) (private)
[ GitHub ]#emit_requirement(req, indent) (private)
[ GitHub ]#emit_specification(spec, indent) (private)
[ GitHub ]# File 'lib/rubygems/yaml_serializer.rb', line 737
def emit_specification(spec, indent) parts = [" !ruby/object:Gem::Specification\n"] parts << "#{pad(indent)}name:#{emit_node(spec.name, indent + 2)}" parts << "#{pad(indent)}version:#{emit_node(spec.version, indent + 2)}" parts << "#{pad(indent)}platform: #{spec.platform}\n" if spec.platform.to_s != spec.original_platform.to_s parts << "#{pad(indent)}original_platform: #{spec.original_platform}\n" end attributes = Gem::Specification.attribute_names.map(&:to_s).sort - %w[name version platform] attributes.each do |name| val = spec.instance_variable_get("@#{name}") next if val.nil? parts << "#{pad(indent)}#{name}:#{emit_node(val, indent + 2)}" end res = parts.join res << "\n" unless res.end_with?("\n") res end
#emit_string(str, indent, quote: false) (private)
[ GitHub ]# File 'lib/rubygems/yaml_serializer.rb', line 817
def emit_string(str, indent, quote: false) if str.include?("\n") emit_block_scalar(str, indent) elsif needs_quoting?(str, quote) " #{quote_string(str)}\n" else " #{str}\n" end end
#emit_time(time) (private)
[ GitHub ]#emit_version(ver, indent) (private)
[ GitHub ]
#needs_quoting?(str, quote = false) ⇒ Boolean (private)
# File 'lib/rubygems/yaml_serializer.rb', line 837
def needs_quoting?(str, quote = false) quote || str.empty? || str != str.strip || str =~ /[[:cntrl:]]/ || str =~ /^[!*&:@%$"'|`]/ || str =~ /^-?\d(\.\d)?$/ || str =~ /^[<>=-]/ || str == "true" || str == "false" || str == "nil" || str == "null" || str == "~" || str.include?(":") || str.include?("#") || str.include?("[") || str.include?("]") || str.include?("{") || str.include?("}") || str.include?(",") end
#pad(indent) (private)
[ GitHub ]# File 'lib/rubygems/yaml_serializer.rb', line 849
def pad(indent) " " * indent end
#quote_string(str) (private)
[ GitHub ]# File 'lib/rubygems/yaml_serializer.rb', line 845
def quote_string(str) %("#{str.gsub(/[\\"]|[[:cntrl:]]/) {|c| STRING_ESCAPES[c] || format("\\x%02X", c.ord) }}") end