Sha256: 9c465de7a07b9a79c3a124aec3aca9eebad79fc2ef726f29503c45a63b1bbf03

Contents?: true

Size: 1.71 KB

Versions: 2

Compression:

Stored size: 1.71 KB

Contents

module TOML  
  class Generator
    attr_reader :body, :doc

    def initialize(doc)
      # Ensure all the to_toml methods are injected into the base Ruby classes
      # used by TOML.
      self.class.inject!
      
      @body = ""
      @doc = doc
      
      visit(@doc)
      
      return @body
    end
    
    # Whether or not the injections have already been done.
    @@injected = false
    # Inject to_toml methods into the Ruby classes used by TOML (booleans,
    # String, Numeric, Array). You can add to_toml methods to your own classes
    # to allow them to be easily serialized by the generator (and it will shout
    # if something doesn't have a to_toml method).
    def self.inject!
      return if @@injected
      require 'toml/monkey_patch'
      @@injected = true
    end
    
    def visit(hash, path = "")
      hash_pairs = [] # Sub-hashes
      other_pairs = []
      
      hash.keys.sort.each do |key|
        val = hash[key]
        # TODO: Refactor for other hash-likes (OrderedHash)
        if val.is_a? Hash
          hash_pairs << [key, val]
        else
          other_pairs << [key, val]
        end
      end
      
      # Handle all the key-values
      if !path.empty? && !other_pairs.empty?
        @body += "[#{path}]\n"
      end
      other_pairs.each do |pair|
        key, val = pair
        @body += "#{key} = #{format(val)}\n"
      end
      @body += "\n" unless other_pairs.empty?
      
      # Then deal with sub-hashes
      hash_pairs.each do |pair|
        key, hash = pair
        visit(hash, (path.empty? ? key : [path, key].join(".")))
      end
    end#visit
    
    # Returns the value formatted for TOML.
    def format(val)
      val.to_toml
    end
    
  end#Generator
end#TOML

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
toml-0.0.4 lib/toml/generator.rb
toml-0.0.3 lib/toml/generator.rb