Class: Alf::Renderer

Inherits:
Object
  • Object
show all
Extended by:
Support::Registry
Includes:
Enumerable
Defined in:
lib/alf/renderer.rb,
lib/alf/renderer/csv.rb,
lib/alf/renderer/rash.rb,
lib/alf/renderer/html.rb,
lib/alf/renderer/text.rb,
lib/alf/renderer/json.rb,
lib/alf/renderer/yaml.rb

Overview

Renders a relation iterator in a specific format.

A renderer takes a tuple iterator as input and renders it on an output stream. Similarly to the Reader class, this one provides a registration mechanism for specific output formats. The common scenario is as follows:

# Register a new renderer for :foo format (automatically provides the
# '--foo   Render output as a foo stream' option of 'alf show') and with
# the FooRenderer class for handling rendering.
Renderer.register(:foo, "as a foo stream", FooRenderer)

# Later on, you can request a renderer instance for a specific format
# as follows (wiring input is optional)
r = Renderer.renderer(:foo, [an iterator])

# Also, a factory method is automatically installed on the Renderer class
# itself.
r = Renderer.foo([an iterator])

Direct Known Subclasses

CSV, Html, JSON, Rash, Text, YAML

Defined Under Namespace

Classes: CSV, Html, JSON, Rash, Text, YAML

Constant Summary collapse

DEFAULT_OPTIONS =

Default renderer options

{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Support::Registry

each, listen, listeners, register, registered

Constructor Details

#initialize(input, options = {}) ⇒ Renderer

Creates a reader instance.


90
91
92
93
# File 'lib/alf/renderer.rb', line 90

def initialize(input, options = {})
  @input   = input
  @options = self.class.const_get(:DEFAULT_OPTIONS).merge(options || {})
end

Instance Attribute Details

#inputObject

Renderer tuple iterator


81
82
83
# File 'lib/alf/renderer.rb', line 81

def input
  @input
end

#optionsHash


84
85
86
# File 'lib/alf/renderer.rb', line 84

def options
  @options
end

Class Method Details

.by_mime_type(mime_type, *args) ⇒ Renderer

Returns a Renderer instance for the given mime type.


67
68
69
70
71
72
73
# File 'lib/alf/renderer.rb', line 67

def by_mime_type(mime_type, *args)
  if r = registered.find{|_,_,c| c.mime_type == mime_type}
    r.last.new(*args)
  else
    raise UnsupportedMimeTypeError, "No renderer for `#{mime_type}`"
  end
end

.register(name, description, clazz) ⇒ Object

Register a renderering class with a given name and description.

Registered class must at least provide a constructor with an empty signature. The name must be a symbol which can safely be used as a ruby method name. A factory class method of that name and degelation signature is automatically installed on the Renderer class.


40
41
42
# File 'lib/alf/renderer.rb', line 40

def register(name, description, clazz)
  super([name, description, clazz], Renderer)
end

.renderer(name, *args) ⇒ Renderer

Returns a Renderer instance for the given output format name.


52
53
54
55
56
57
58
# File 'lib/alf/renderer.rb', line 52

def renderer(name, *args)
  if r = registered.find{|triple| triple.first == name}
    r.last.new(*args)
  else
    raise "No renderer registered for #{name}"
  end
end

Instance Method Details

#execute(output = $stdout) ⇒ Object

Executes the rendering, outputting the resulting tuples on the provided output buffer.

The default implementation delegates the call to #render.


99
100
101
102
103
104
# File 'lib/alf/renderer.rb', line 99

def execute(output = $stdout)
  each do |str|
    output << str
  end
  output
end