Module: Occi::Core::Helpers::Renderable

Included in:
ActionInstance, Category, Entity, Locations, Model
Defined in:
lib/occi/core/helpers/renderable.rb

Overview

Introduces rendering capabilities to every receiver class. Short-hand `to_<format>` methods are automatically generated for every available rendering `format`. The renderability is evaluated in runtime, when calling `render` or one of the `to_<format>` methods.

Author:

Constant Summary collapse

RENDERER_FACTORY_CLASS =

Default renderer factory class

Occi::Core::RendererFactory

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object

Allows calling `.extend(Renderable)` on instances. Does not allow class-based extension, will raise a `RuntimeError` error.

Examples:

o = Object.new
o.extend Renderable
o.to_text  # => String

Parameters:

  • base (Class, Object)

    class or object being extended


72
73
74
# File 'lib/occi/core/helpers/renderable.rb', line 72

def self.extended(base)
  base.is_a?(Class) ? raise("#{self} cannot extend #{base}") : included(base.class)
end

.included(base) ⇒ Object

Adds available rendering formats as `to_<format>` methods on the receiver.

Examples:

class Test
  include Occi::Core::Helpers::Renderable
end

t = Test.new
t.to_text   # => String

Parameters:

  • base (Class)

    class receiving this module


56
57
58
59
60
61
# File 'lib/occi/core/helpers/renderable.rb', line 56

def self.included(base)
  renderer_factory.formats.each do |format|
    base.logger.debug "Adding support for format #{format} to #{base}" if base.respond_to?(:logger)
    base.send(:define_method, "to_#{format}", proc { render(format) })
  end
end

.renderer_factoryObject

Returns pre-constructed instance of the active renderer factory providing access to registered renderers.

Returns:

  • (Object)

    instance of the renderer factory


80
81
82
# File 'lib/occi/core/helpers/renderable.rb', line 80

def self.renderer_factory
  renderer_factory_class.instance
end

.renderer_factory_classClass

Provides access to the default renderer factory class.

Returns:

  • (Class)

    renderer factory class


87
88
89
# File 'lib/occi/core/helpers/renderable.rb', line 87

def self.renderer_factory_class
  RENDERER_FACTORY_CLASS
end

Instance Method Details

#render(format, options = {}) ⇒ Object

Renders the receiver into the specified over-the-wire `format`. `format` is automatically injected into `options` and these are then passed (unmodified) to the underlying renderer. See documentation of a specific renderer for details.

Examples:

render 'text' # => String
render 'json' # => String
render 'headers' # => Hash

Parameters:

  • format (String)

    over-the-wire format, mandatory

  • options (Hash) (defaults to: {})

    options passed to the underlying renderer

Returns:

  • (Object)

    output of the chosen renderer


28
29
30
31
32
# File 'lib/occi/core/helpers/renderable.rb', line 28

def render(format, options = {})
  options[:format] = format
  logger.debug "#{self.class} is being rendered to #{format} with #{options.inspect}" if respond_to?(:logger)
  renderer_for(format).render(self, options)
end

#renderer_factoryObject

Instance proxy to `renderer_factory` instance, see `Renderable::renderer_factory`.


40
41
42
# File 'lib/occi/core/helpers/renderable.rb', line 40

def renderer_factory
  Renderable.renderer_factory
end

#renderer_for(format) ⇒ Object

Instance delegate for `RendererFactory#renderer_for`, see `RendererFactory`.


35
36
37
# File 'lib/occi/core/helpers/renderable.rb', line 35

def renderer_for(format)
  renderer_factory.renderer_for(format)
end