Sha256: 14410d5fd81846673b28a7012163e5a42d8dd4120529358d8f25a99bd6da6554

Contents?: true

Size: 1021 Bytes

Versions: 2

Compression:

Stored size: 1021 Bytes

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

    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

2 entries across 2 versions & 1 rubygems

Version Path
undies-3.1.0 lib/undies/io.rb
undies-3.0.0 lib/undies/io.rb