Class: TavernaPlayer::PortRenderer

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers, Concerns::Callback
Defined in:
lib/taverna_player/port_renderer.rb

Overview

This class manages the rendering of many different port types that could be associated with a workflow. It can be configured with new types and the example renderers for each type can also be changed. An example of how to set it up can be found in the taverna_player initializer.

Each renderer has all of the ActionView::Helpers (such as link_to, tag, etc) available to them.

Constant Summary collapse

TAVERNA_ERROR_TYPE =

:stopdoc: Taverna Workflow error MIME type

MIME::Type.new("application/x-error")
EMPTY_FILE_OLD =

Empty file MIME types as used in older and new versions of “file”.

MIME::Type.new("application/x-empty")
EMPTY_FILE_NEW =
MIME::Type.new("inode/x-empty")

Instance Method Summary collapse

Methods included from Concerns::Callback

#callback

Constructor Details

#initializePortRenderer

Returns a new instance of PortRenderer.



37
38
39
40
# File 'lib/taverna_player/port_renderer.rb', line 37

def initialize
  @hash = Hash.new
  MIME::Types.add(TAVERNA_ERROR_TYPE, EMPTY_FILE_OLD, EMPTY_FILE_NEW)
end

Instance Method Details

#add(mimetype, method, default = false) ⇒ Object

:call-seq:

add(mimetype, renderer, default = false)

Add a renderer method for the specified MIME type. If you would like the renderer to be the default for that particular media type then pass true in the final parameter - the media type is the part of a MIME type before the slash (/), e.g. “text” or “image”. The MIME type should be specified as a string.



51
52
53
54
55
56
57
# File 'lib/taverna_player/port_renderer.rb', line 51

def add(mimetype, method, default = false)
  type = MIME::Types[mimetype].first

  @hash[type.media_type] ||= {}
  @hash[type.media_type][type.sub_type] = method
  type_default(type.media_type, method) if default
end

#default(method) ⇒ Object

:call-seq:

default(method)

Set a default renderer for any MIME type not specifically set. This could be used to supply a piece of text and a download link for any type that cannot normally be shown in the browser inline.



75
76
77
# File 'lib/taverna_player/port_renderer.rb', line 75

def default(method)
  @hash[:default] = method
end

#list(method) ⇒ Object

:call-seq:

list(method)

Set a renderer to handle list ports. This will typically format the list somehow and render the list items with further calls to TavernaPlayer.port_renderer.render.



85
86
87
# File 'lib/taverna_player/port_renderer.rb', line 85

def list(method)
  @hash[:list] = method
end

#render(port, index = []) ⇒ Object

:call-seq:

render(port) -> markup

This is the method that calls the correct renderer for the given port and returns the resultant rendering.



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/taverna_player/port_renderer.rb', line 94

def render(port, index = [])
  if port.depth > 0 && index.empty?
    renderer = @hash[:list]
  else
    type = MIME::Types[port.value_type(index)].first
    renderer = @hash[type.media_type][type.sub_type] ||
      @hash[type.media_type][:default] || @hash[:default]
  end

  raw(callback(renderer, port, index))
end

#type_default(media_type, method) ⇒ Object

:call-seq:

type_default(media_type, renderer)

This is another way of setting the default renderer method for a whole media type (see the add method for more details).



64
65
66
67
# File 'lib/taverna_player/port_renderer.rb', line 64

def type_default(media_type, method)
  @hash[media_type] ||= {}
  @hash[media_type][:default] = method
end