Class: Wee::Component

Inherits:
Presenter show all
Defined in:
lib/wee/core/component.rb,
lib/wee/continuation/core/component.rb

Overview

The base class of all components. You should at least overwrite method #render in your own subclasses.

Direct Known Subclasses

Main, OgScaffolder, OgScaffolder::Editor, MessageBox

Defined Under Namespace

Classes: OnAnswer

Instance Attribute Summary

Attributes inherited from Presenter

#properties

Instance Method Summary collapse

Methods inherited from Presenter

#do_render, #get_property, #lookup_property, #session, template, uses_property

Instance Method Details

#add_decoration(d) ⇒ Object

Adds decoration d to the decoration chain.

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: self



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/wee/core/component.rb', line 158

def add_decoration(d)
  if d.global?
    d.owner = self.decoration
    self.decoration = d
  else
    last_global = nil
    each_decoration {|i| 
      if i.global?
        last_global = i
      else
        break
      end
    }
    if last_global.nil?
      # no global decorations specified -> add in front
      d.owner = self.decoration
      self.decoration = d
    else
      # add after last_global
      d.owner = last_global.owner
      last_global.owner = d 
    end
  end

  return self
end

#backtrack_state(snapshot) ⇒ Object

Take snapshots of objects that should correctly be backtracked.

Backtracking means that you can go back in time of the components’ state. Therefore it is neccessary to take snapshots of those objects that want to participate in backtracking. Taking snapshots of the whole component tree would be too expensive and unflexible. Note that methods take_snapshot and restore_snapshot are called for those objects to take the snapshot (they behave like marshal_dump and marshal_load). Overwrite them if you want to define special behaviour.

By default only @decoration is backtracked (which actually is a ValueHolder, as only the pointer changes not the decoration-object itself!).

For example if you dynamically add children to your component, you might want to backtrack the children array. Therefore you simply pass it to the Snapshot#add method:

def backtrack_state(snapshot)
  super
  snapshot.add(self.children)
end

This will call Array#take_snapshot to take the snapshot for the children array. If at a later point in time a snapshot is restored, Array#restore_snapshot will be called with the return value of Array#take_snapshot as argument.

snapshot

An object of class Snapshot



272
273
274
275
# File 'lib/wee/core/component.rb', line 272

def backtrack_state(snapshot)
  snapshot.add(@decoration)
  children.each do |child| child.backtrack_state_chain(snapshot) end
end

#backtrack_state_chain(snapshot) ⇒ Object

Starts the backtrack-state phase for the decoration chain, by invoking method #backtrack_state of the first decoration or the component itself if no decorations were specified.

See #backtrack_state for details.

snapshot

An object of class Snapshot



236
237
238
# File 'lib/wee/core/component.rb', line 236

def backtrack_state_chain(snapshot)
  decoration.backtrack_state(snapshot)
end

#decorationObject

Returns the first decoration from the component’s decoration chain, or self if no decorations were specified for the component.



129
130
131
# File 'lib/wee/core/component.rb', line 129

def decoration
  @decoration.value
end

#decoration=(d) ⇒ Object

Set the pointer to the first decoration to d.



135
136
137
# File 'lib/wee/core/component.rb', line 135

def decoration=(d) 
  @decoration.value = d
end

#do_render_chain(rendering_context) ⇒ Object

Starts rendering the decoration chain by calling method Presenter#do_render for the first decoration of the component, or calling do_render for the component itself if no decorations were specified.

rendering_context

An object of class RenderingContext



19
20
21
# File 'lib/wee/core/component.rb', line 19

def do_render_chain(rendering_context)
  decoration.do_render(rendering_context)
end

#each_decorationObject

Iterates over all decorations (note that the component itself is excluded).



141
142
143
144
145
146
147
148
# File 'lib/wee/core/component.rb', line 141

def each_decoration # :yields: decoration
  d = self.decoration
  loop do
    break if d == self or d.nil?
    yield d
    d = d.owner
  end
end

#process_callbacks(callback_stream) ⇒ Object

Process and invoke all callbacks specified for this component and all of it’s child components.

All input callbacks of this component and it’s child components are processed/invoked before any of the action callbacks are processed/invoked.

callback_stream

An object of class CallbackStream



59
60
61
62
63
64
65
66
# File 'lib/wee/core/component.rb', line 59

def process_callbacks(callback_stream)
  super do
    # process callbacks of all children
    children.each do |child|
      child.process_callbacks_chain(callback_stream)
    end
  end
end

#process_callbacks_chain(callback_stream) ⇒ Object

Starts processing the callbacks for the decoration chain by invoking method #process_callbacks of the first decoration or the component itself if no decorations were specified.

callback_stream

An object of class CallbackStream



46
47
48
# File 'lib/wee/core/component.rb', line 46

def process_callbacks_chain(callback_stream)
  decoration.process_callbacks(callback_stream)
end

#remove_decoration(d) ⇒ Object

Remove decoration d from the decoration chain.

Returns the removed decoration or nil if it did not exist in the decoration chain.



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/wee/core/component.rb', line 190

def remove_decoration(d)
  if d == self.decoration  # 'd' is in front
    self.decoration = d.owner
  else
    last_decoration = self.decoration
    next_decoration = nil
    loop do
      return nil if last_decoration == self or last_decoration.nil?
      next_decoration = last_decoration.owner
      break if d == next_decoration
      last_decoration = next_decoration
    end
    last_decoration.owner = d.owner  
  end
  d.owner = nil  # decoration 'd' no longer is an owner of anything!
  return d
end

#remove_decoration_ifObject

Remove all decorations that match the block condition.

Example (removes all decorations of class HaloDecoration):

remove_decoration_if {|d| d.class == HaloDecoration}


215
216
217
218
219
# File 'lib/wee/core/component.rb', line 215

def remove_decoration_if # :yields: decoration
  to_remove = []
  each_decoration {|d| to_remove << d if yield d}
  to_remove.each {|d| remove_decoration(d)}
end

#renderObject

This method renders the content of this component.

OVERWRITE this method in your own component class to implement the view. By default this method does nothing!

Use the current renderer as returned by #renderer or it’s short-cut #r.



30
31
# File 'lib/wee/core/component.rb', line 30

def render
end