Class: DuperVisor::Renderer

Inherits:
Object
  • Object
show all
Includes:
Formats::Accessors
Defined in:
lib/dupervisor/renderer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Formats::Accessors

#format_classes, #formats

Constructor Details

#initialize(content_hash, output_stream = nil) ⇒ Renderer

Returns a new instance of Renderer.

Raises:

  • (ArgumentError)


9
10
11
12
13
# File 'lib/dupervisor/renderer.rb', line 9

def initialize(content_hash, output_stream = nil)
  raise ArgumentError.new('Invalid arguments - expecting a stream and a hash') unless content_hash.is_a?(Hash)
  self.output  = output_stream
  self.content_hash = content_hash
end

Instance Attribute Details

#content_hashObject

Returns the value of attribute content_hash.



7
8
9
# File 'lib/dupervisor/renderer.rb', line 7

def content_hash
  @content_hash
end

#outputObject

Returns the value of attribute output.



7
8
9
# File 'lib/dupervisor/renderer.rb', line 7

def output
  @output
end

#rendered_contentObject

Returns the value of attribute rendered_content.



7
8
9
# File 'lib/dupervisor/renderer.rb', line 7

def rendered_content
  @rendered_content
end

Instance Method Details

#render(format) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/dupervisor/renderer.rb', line 15

def render(format)
  self.rendered_content = self.formats[format].to.call(content_hash)
  if output.respond_to?(:puts)
    output.puts rendered_content
    output.close if output.respond_to?(:close) && output != STDOUT
  end
  rendered_content
end