class Gem::YAMLSerializer::Emitter - Documentation for Ruby 4.1
Description: Documentation for the Gem::YAMLSerializer::Emitter class
Keywords: ruby,class,Gem::YAMLSerializer::Emitter

🔍

No recent searches

# class Gem::YAMLSerializer::Emitter

### Public Instance Methods

emit (obj)
Source

    # File lib/rubygems/yaml_serializer.rb, line 658
    def emit(obj)
      "---#{emit_node(obj, 0)}"
    end

### Private Instance Methods

emit_array (arr, indent)
Source

    # File lib/rubygems/yaml_serializer.rb, line 747
    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

emit_block_scalar (str, indent)
Source

    # File lib/rubygems/yaml_serializer.rb, line 773
    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)
Source

    # File lib/rubygems/yaml_serializer.rb, line 722
    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)
Source

    # File lib/rubygems/yaml_serializer.rb, line 733
    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)
Source

    # File lib/rubygems/yaml_serializer.rb, line 664
    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
        " #{obj.to_s.inspect}\n"
      end
    end

emit_platform (plat, indent)
Source

    # File lib/rubygems/yaml_serializer.rb, line 710
    def emit_platform(plat, indent)
      " !ruby/object:Gem::Platform\n" \
        "#{pad(indent)}cpu:#{emit_node(plat.cpu, indent + 2)}" \
        "#{pad(indent)}os:#{emit_node(plat.os, indent + 2)}" \
        "#{pad(indent)}version:#{emit_node(plat.version, indent + 2)}"
    end

emit_requirement (req, indent)
Source

    # File lib/rubygems/yaml_serializer.rb, line 717
    def emit_requirement(req, indent)
      " !ruby/object:Gem::Requirement\n" \
        "#{pad(indent)}requirements:#{emit_node(req.requirements, indent + 2)}"
    end

emit_specification (spec, indent)
Source

    # File lib/rubygems/yaml_serializer.rb, line 684
    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)
Source

    # File lib/rubygems/yaml_serializer.rb, line 763
    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)
Source

    # File lib/rubygems/yaml_serializer.rb, line 759
    def emit_time(time)
      " #{time.utc.strftime("%Y-%m-%d %H:%M:%S.%N Z")}\n"
    end

emit_version (ver, indent)
Source

    # File lib/rubygems/yaml_serializer.rb, line 705
    def emit_version(ver, indent)
      " !ruby/object:Gem::Version\n" \
        "#{pad(indent)}version: #{emit_node(ver.version.to_s, indent + 2).lstrip}"
    end

needs_quoting? (str, quote)
Source

    # File lib/rubygems/yaml_serializer.rb, line 783
    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)
Source

    # File lib/rubygems/yaml_serializer.rb, line 791
    def pad(indent)
      " " * indent
    end