Class: PinkShirt::Output

Inherits:
Object
  • Object
show all
Defined in:
lib/pink_shirt/output.rb

Overview

Nokogiri::Sax builds output as a long stream. Output collects all the writes as an array, and then joins them when required you can also lock it for writing.

Usage

stream = Output.new
stream << 'goods... '
stream.inspect #=> ['goods... ']
stream.lock('suspicious tag')
stream << 'bad stuff'
stream.inspect #=> ['goods... ']
stream.unlock
stream << 'good again'
stream.inspect #=> ['goods... ', 'good again']

Instance Method Summary collapse

Constructor Details

#initializeOutput

Returns a new instance of Output.



22
23
24
25
26
27
# File 'lib/pink_shirt/output.rb', line 22

def initialize
  @contents = []
  @silenced = false
  @locks = []
  @ouput = ""
end

Instance Method Details

#<<(more) ⇒ Object



39
40
41
# File 'lib/pink_shirt/output.rb', line 39

def << (more)
  @contents << more unless @silenced
end

#inspectObject



62
63
64
# File 'lib/pink_shirt/output.rb', line 62

def inspect
  @contents
end

#joinObject



43
44
45
# File 'lib/pink_shirt/output.rb', line 43

def join
  @output = @contents.join
end

#lock(key) ⇒ Object



29
30
31
32
# File 'lib/pink_shirt/output.rb', line 29

def lock(key)
  @locks << key
  @silenced = true
end

#sanitizeObject



47
48
49
# File 'lib/pink_shirt/output.rb', line 47

def sanitize
  @output = PinkShirt::Entities.sanitize(@output)
end

#to_sObject



55
56
57
58
59
60
# File 'lib/pink_shirt/output.rb', line 55

def to_s
  join
  sanitize
  trim
  @output
end

#trimObject



51
52
53
# File 'lib/pink_shirt/output.rb', line 51

def trim
  @output = @output.chomp.chomp.chomp
end

#unlockObject



34
35
36
37
# File 'lib/pink_shirt/output.rb', line 34

def unlock
  @locks.pop
  @silenced = false if @locks.empty?
end