Class: Undies::IO

Inherits:
Object
  • Object
show all
Defined in:
lib/undies/io.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stream, opts = {}) ⇒ IO

Returns a new instance of IO.



11
12
13
14
15
# File 'lib/undies/io.rb', line 11

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

Instance Attribute Details

#indentObject (readonly)

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



8
9
10
# File 'lib/undies/io.rb', line 8

def indent
  @indent
end

#levelObject

Returns the value of attribute level.



9
10
11
# File 'lib/undies/io.rb', line 9

def level
  @level
end

#newlineObject (readonly)

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



8
9
10
# File 'lib/undies/io.rb', line 8

def newline
  @newline
end

#node_stackObject (readonly)

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



8
9
10
# File 'lib/undies/io.rb', line 8

def node_stack
  @node_stack
end

#streamObject (readonly)

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



8
9
10
# File 'lib/undies/io.rb', line 8

def stream
  @stream
end

Instance Method Details

#<<(markup) ⇒ Object



31
32
33
# File 'lib/undies/io.rb', line 31

def <<(markup)
  @stream << markup
end

#currentObject



38
# File 'lib/undies/io.rb', line 38

def current; @node_stack.last; end

#empty?Boolean

Returns:

  • (Boolean)


39
# File 'lib/undies/io.rb', line 39

def empty?; @node_stack.empty? end

#line_indent(relative_level = 0) ⇒ Object



27
28
29
# File 'lib/undies/io.rb', line 27

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

#options=(opts) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/undies/io.rb', line 17

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

#popObject



37
# File 'lib/undies/io.rb', line 37

def pop; @level -= 1; @node_stack.pop; end

#push(scope) ⇒ Object



35
# File 'lib/undies/io.rb', line 35

def push(scope); @level += 1; push!(scope); end

#push!(scope) ⇒ Object



36
# File 'lib/undies/io.rb', line 36

def push!(scope); @node_stack.push(scope); end