Class: Wee::Decoration

Inherits:
Presenter show all
Defined in:
lib/wee/core/decoration.rb

Overview

Abstract base class of all decorations. Forwards the methods #process_callbacks, #do_render and #backtrack_state to the next decoration in the chain. Subclasses should provide special behaviour in these methods, otherwise the decoration does not make sense.

For example, a HeaderFooterDecoration class could draw a header and footer around the decorations or components below itself:

class HeaderFooterDecoration < Wee::Decoration
  def do_render(rendering_context)
    with_renderer_for(rendering_context) do
      render_header
      super(rendering_context)
      render_footer
    end
  end

  def render_header
    r.text "header
  end

  def render_footer
    ...
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Presenter

#get_property, #lookup_property, #properties, #properties=, #render, #session, template, uses_property

Instance Attribute Details

#ownerObject

Points to the next decoration in the chain. A decoration is responsible for all decorations or components “below” it (everything that follows this decoration in the chain). In other words, it’s the owner of everything “below” itself.



34
35
36
# File 'lib/wee/core/decoration.rb', line 34

def owner
  @owner
end

Instance Method Details

#backtrack_state(snapshot) ⇒ Object

Forwards method call to the next decoration in the chain.



59
60
61
62
# File 'lib/wee/core/decoration.rb', line 59

def backtrack_state(snapshot)
  @owner.backtrack_state(snapshot)
  snapshot.add(self)
end

#do_render(rendering_context) ⇒ Object

Forwards method call to the next decoration in the chain.



53
54
55
# File 'lib/wee/core/decoration.rb', line 53

def do_render(rendering_context)
  @owner.do_render(rendering_context)
end

#global?Boolean

Is this decoration a global or a local one? By default all decorations are local unless this method is overwritten.

A global decoration is added in front of the decoration chain, a local decoration is added in front of all other local decorations but after all global decorations.

Returns:

  • (Boolean)


43
# File 'lib/wee/core/decoration.rb', line 43

def global?() false end

#process_callbacks(&block) ⇒ Object

Forwards method call to the next decoration in the chain.



47
48
49
# File 'lib/wee/core/decoration.rb', line 47

def process_callbacks(&block)
  @owner.process_callbacks(&block)
end

#restore_snapshot(snap) ⇒ Object



78
79
80
# File 'lib/wee/core/decoration.rb', line 78

def restore_snapshot(snap)
  @owner = snap
end

#take_snapshotObject

We have to save the @owner attribute to be able to correctly backtrack calls, as method Wee::Component#call modifies it in the call to component.remove_decoration(answer). Removing the answer-decoration has the advantage to be able to call a component more than once!



74
75
76
# File 'lib/wee/core/decoration.rb', line 74

def take_snapshot
  @owner
end