Module: Aws::Templates::Render

Includes:
Processor
Included in:
Utils::BaseTypeViews, Utils::Inspect, Utils::Stringify
Defined in:
lib/aws/templates/render.rb,
lib/aws/templates/render/view.rb,
lib/aws/templates/render/utils.rb,
lib/aws/templates/render/basic_view.rb,
lib/aws/templates/render/utils/inspect.rb,
lib/aws/templates/render/utils/stringify.rb,
lib/aws/templates/render/utils/base_type_views.rb

Overview

View layer of the MVC pattern

View layer provides means of defining “views” of your artifacts to define different ways of rendering the object hierarchies you create to end representation.

The module also contains a few mixin methods to simplify creation of “renders” - collections of views defining the same domain representation. For instance JSON, LDIF, Wiki could be such final destinations.

Renders could be classes or modules. Modules work best if no customization is needed and you want a singleton.

Example

class Wiki
  include Aws::Templates::Render
end

module JSON
  extend Aws::Templates::Render
end

Defined Under Namespace

Modules: Utils Classes: BasicView, View

Instance Method Summary collapse

Methods included from Processor

#handler?, #handler_for, #routing

Methods included from Processor::Routing

#register, #registry

Instance Method Details

#can_render?(instance) ⇒ Boolean

Can object be rendered

Returns true if the object passed can be rendered by one of the views in the registry

Returns:

  • (Boolean)


36
37
38
# File 'lib/aws/templates/render.rb', line 36

def can_render?(instance)
  instance.class.ancestors.any? { |ancestor| handler?(ancestor) }
end

#define_handler(artifact_class, view = nil, &blk) ⇒ Object Also known as: define_view

Define view for artifact

Another way to define views for artifacts. Creates anonymous class and attaches as the view to the specified artifact



67
68
69
# File 'lib/aws/templates/render.rb', line 67

def define_handler(artifact_class, view = nil, &blk)
  super(artifact_class, view || BasicView, &blk)
end

#process(entity, params = nil) ⇒ Object



58
59
60
# File 'lib/aws/templates/render.rb', line 58

def process(entity, params = nil)
  view_for(entity, params).to_rendered
end

#view_for(instance, params = nil) ⇒ Object

Lookup a view for the artifact

Searches registry for artifact’s class and all its ancestors in the registry and returns the closest matching view

  • instance - artifact instance to render

  • params - assigned parameters; it can be arbitrary value;

    it is propagated to selected render
    


48
49
50
51
52
53
54
55
56
# File 'lib/aws/templates/render.rb', line 48

def view_for(instance, params = nil)
  return instance if instance.respond_to?(:to_rendered)

  ancestor = instance.class.ancestors.find { |mod| handler?(mod) }

  raise Templates::Exception::ViewNotFound.new(instance) unless ancestor

  handler_for(ancestor).new(instance, params)
end