Class: Aws::Templates::Render::BasicView

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

Overview

Basic render view

Views are classes encapsulating functionality of transforming artifacts into domain-specific output. For instance, the same LDAP record can be transformed into JSON description or LDIF definition. Views can be attached to ancestors of an artifact and it expected that the library will look-up the closest ancestor having view attached if the render is invoked on a child.

Each view is attached to a registry object which stores correspondence between artifact classes and their views, and optionally to an artifact. A view is registered in a registry only when it is attached to an artifact. Views depend on artifacts but artifacts are not aware of views. As the extreme case, a sole view can be attached to Artifact if you have universal way to render your domain objects.

Views are regular Ruby classes and all assumptions made about polymorphism, inheritance and incapsulation are true for them.

View class itself is an abstract class which can’t be instantiated directly.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj, params = nil) ⇒ BasicView

Create view instance and link it to the artifact instance



74
75
76
77
# File 'lib/aws/templates/render/view.rb', line 74

def initialize(obj, params = nil)
  @instance = obj
  @parameters = params
end

Instance Attribute Details

#instanceObject (readonly)

Artifact instance view object is attached to



56
57
58
# File 'lib/aws/templates/render/view.rb', line 56

def instance
  @instance
end

#parametersObject (readonly)

Assigned view parameters



60
61
62
# File 'lib/aws/templates/render/view.rb', line 60

def parameters
  @parameters
end

Class Method Details

.artifact(artifact_class) ⇒ Object

Link the view class to the artifact class

Registers the link in the render object of the view class.



49
50
51
52
# File 'lib/aws/templates/render/view.rb', line 49

def self.artifact(artifact_class)
  render.register(artifact_class, self)
  self
end

.register_in(r) ⇒ Object

Register the view class in a render

Registers the view class in the render

  • r - render registrar



40
41
42
43
# File 'lib/aws/templates/render/view.rb', line 40

def self.register_in(r)
  @render = r
  self
end

.renderObject

Render accessor

Returns either render of this view class or render of any ancestor.



31
32
33
# File 'lib/aws/templates/render/view.rb', line 31

def self.render
  @render || superclass.render
end

Instance Method Details

#in_instance(*args, &blk) ⇒ Object

Execute in the instance context

Executed passed block in the context of the instance being rendered. It helps against putting too much instance method accesses in long blocks. Returns the value returned by the block.



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

def in_instance(*args, &blk)
  instance.instance_exec(*args, &blk)
end

#renderObject

Alias for class method render



81
82
83
# File 'lib/aws/templates/render/view.rb', line 81

def render
  self.class.render
end

#rendered_for(obj, parameters_override = nil) ⇒ Object

Render the object

Renders passed object with the view default render



89
90
91
92
# File 'lib/aws/templates/render/view.rb', line 89

def rendered_for(obj, parameters_override = nil)
  render.view_for(obj, parameters_override.nil? ? parameters : parameters_override)
        .to_rendered
end

#to_renderedObject

Render the instance of the artifact

The method should be overriden and return rendered form of the attached instance

Raises:

  • (NotImplementedError)


98
99
100
# File 'lib/aws/templates/render/view.rb', line 98

def to_rendered
  raise NotImplementedError.new('The method should be overriden')
end