class Gem::YAMLSerializer::Emitter
Public Instance Methods
Source
# File lib/rubygems/yaml_serializer.rb, line 640 def emit(obj) "---#{emit_node(obj, 0)}" end
Private Instance Methods
Source
# File lib/rubygems/yaml_serializer.rb, line 727 def emit_array(arr, indent) if arr.empty? " []\n" else parts = ["\n"] arr.each do |v| parts << "#{pad(indent)}-#{emit_node(v, indent + 2)}" end parts.join end end
Source
# File lib/rubygems/yaml_serializer.rb, line 753 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
Source
# File lib/rubygems/yaml_serializer.rb, line 702 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
Source
# File lib/rubygems/yaml_serializer.rb, line 713 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 parts << "#{pad(indent)}#{key_str}:#{emit_node(v, indent + 2, quote: is_symbol)}" end parts.join end end
Source
# File lib/rubygems/yaml_serializer.rb, line 646 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 Numeric, Symbol, TrueClass, FalseClass, nil " #{obj.inspect}\n" else " #{obj.to_s.inspect}\n" end end
Source
# File lib/rubygems/yaml_serializer.rb, line 690 def emit_platform(plat, indent) " !ruby/object:Gem::Platform\n" \ "#{pad(indent)}cpu: #{plat.cpu.inspect}\n" \ "#{pad(indent)}os: #{plat.os.inspect}\n" \ "#{pad(indent)}version: #{plat.version.inspect}\n" end
Source
# File lib/rubygems/yaml_serializer.rb, line 697 def emit_requirement(req, indent) " !ruby/object:Gem::Requirement\n" \ "#{pad(indent)}requirements:#{emit_node(req.requirements, indent + 2)}" end
Source
# File lib/rubygems/yaml_serializer.rb, line 664 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
Source
# File lib/rubygems/yaml_serializer.rb, line 743 def emit_string(str, indent, quote: false) if str.include?("\n") emit_block_scalar(str, indent) elsif needs_quoting?(str, quote) " #{str.to_s.inspect}\n" else " #{str}\n" end end
Source
# File lib/rubygems/yaml_serializer.rb, line 739 def emit_time(time) " #{time.utc.strftime("%Y-%m-%d %H:%M:%S.%N Z")}\n" end
Source
# File lib/rubygems/yaml_serializer.rb, line 685 def emit_version(ver, indent) " !ruby/object:Gem::Version\n" \ "#{pad(indent)}version: #{emit_node(ver.version.to_s, indent + 2).lstrip}" end
Source
# File lib/rubygems/yaml_serializer.rb, line 763 def needs_quoting?(str, quote) quote || str.empty? || str =~ /^[!*&:@%$]/ || str =~ /^-?\d+(\.\d+)?$/ || str =~ /^[<>=-]/ || str == "true" || str == "false" || str == "nil" || str.include?(":") || str.include?("#") || str.include?("[") || str.include?("]") || str.include?("{") || str.include?("}") || str.include?(",") end
Source
# File lib/rubygems/yaml_serializer.rb, line 771 def pad(indent) " " * indent end