Class: Gem::YAMLSerializer::Emitter
| Relationships & Source Files | |
| Inherits: | Object |
| Defined in: | lib/rubygems/yaml_serializer.rb |
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) ⇒ Boolean private
- #pad(indent) private
Instance Method Details
#emit(obj)
[ GitHub ]# File 'lib/rubygems/yaml_serializer.rb', line 640
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 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
#emit_dependency(dep, indent) (private)
[ GitHub ]# 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
#emit_hash(hash, indent) (private)
[ GitHub ]# 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
#emit_node(obj, indent, quote: false) (private)
[ GitHub ]# 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
#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 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
#emit_string(str, indent, quote: false) (private)
[ GitHub ]# 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
#emit_time(time) (private)
[ GitHub ]#emit_version(ver, indent) (private)
[ GitHub ]
#needs_quoting?(str, quote) ⇒ Boolean (private)
# 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
#pad(indent) (private)
[ GitHub ]# File 'lib/rubygems/yaml_serializer.rb', line 771
def pad(indent) " " * indent end