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.

Defined Under Namespace

Classes: OnAnswer

Instance Method Summary collapse

Methods inherited from Presenter

#do_render, #get_property, #lookup_property, #properties, #properties=, #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



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

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



274
275
276
277
# File 'lib/wee/core/component.rb', line 274

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



238
239
240
# File 'lib/wee/core/component.rb', line 238

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.



131
132
133
# File 'lib/wee/core/component.rb', line 131

def decoration
  @__decoration.value
end

#decoration=(d) ⇒ Object

Set the pointer to the first decoration to d.



137
138
139
# File 'lib/wee/core/component.rb', line 137

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).



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

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(&block) ⇒ Object

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

block

Specifies the action to be taken (e.g. whether to invoke input or action callbacks).



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

def process_callbacks(&block)
  block.call(self)

  # process callbacks of all children
  children.each do |child|
    child.process_callbacks_chain(&block)
  end
end

#process_callbacks_chain(&block) ⇒ 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.

block

Specifies the action to be taken (e.g. whether to invoke input or action callbacks).



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

def process_callbacks_chain(&block)
  decoration.process_callbacks(&block)
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.



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

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}


217
218
219
220
221
# File 'lib/wee/core/component.rb', line 217

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