Class: Aws::Templates::Render::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/aws/templates/render/registry.rb

Overview

View registry

View registries encapsulate differerent ways of transforming your artifacts into a domain-specific output. In nutshell, they are registries of View classes which are able to lookup proper View for object instance passed to it.

Instance Method Summary collapse

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)


34
35
36
# File 'lib/aws/templates/render/registry.rb', line 34

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

#register(artifact, view) ⇒ Object

Register pair artifact-view

Invoked from inside of a View class at definition of the link between the view class and an artifact

  • artifact - artifact class the view claims to be able to render

  • render - view class



26
27
28
# File 'lib/aws/templates/render/registry.rb', line 26

def register(artifact, view)
  registry[artifact] = view
end

#registryObject

View registry accessor



15
16
17
# File 'lib/aws/templates/render/registry.rb', line 15

def registry
  @registry ||= {}
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
    

Raises:



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

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

  mod = instance.class.ancestors.find do |ancestor|
    registry.include?(ancestor)
  end

  raise ViewNotFound.new(instance) unless mod

  registry[mod].new(instance, params)
end