Sha256: fdc7e554ac9fe96ee1188eac0d5a6582298007804931b3fd9d0f5aceae3ec6de

Contents?: true

Size: 1.06 KB

Versions: 3

Compression:

Stored size: 1.06 KB

Contents

module Undies
  class IO

    # the IO class wraps a stream (anything that responds to '<<' and
    # gathers streaming options options.  handles writing markup to the
    # stream.

    attr_reader :stream, :indent, :newline, :node_stack
    attr_accessor :level

    def initialize(stream, opts={})
      @stream = stream
      @node_stack = []
      self.options = opts
    end

    def options=(opts)
      if !opts.kind_of?(::Hash)
        raise ArgumentError, "please provide a hash to set IO options"
      end

      @indent = opts[:pp] || 0
      @newline = opts[:pp].nil? ? "" : "\n"
      @level = opts[:level] || 0
    end

    def line_indent(relative_level=0)
      "#{' '*(@level+relative_level)*@indent}"
    end

    # TODO: threaded/forked writing for performance improvement
    def <<(markup)
      @stream << markup
    end

    def push(scope); @level += 1; push!(scope); end
    def push!(scope); @node_stack.push(scope); end
    def pop; @level -= 1; @node_stack.pop; end
    def current; @node_stack.last; end
    def empty?; @node_stack.empty? end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
undies-3.0.0.rc.3 lib/undies/io.rb
undies-3.0.0.rc.2 lib/undies/io.rb
undies-3.0.0.rc.1 lib/undies/io.rb