Class: Wee::Presenter

Inherits:
Object show all
Defined in:
lib/wee/core/presenter.rb,
lib/wee/core_ext.rb,
lib/wee/template.rb,
lib/wee/adaptors/nitro.rb,
lib/wee/adaptors/rails.rb

Overview

Wee::Presenter is the superclass of all classes that want to participate in rendering and callback-processing. Wee::Component and Wee::Decoration are it’s two most important subclasses.

Direct Known Subclasses

Component, Decoration

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.template(method, hash = {}) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/wee/template.rb', line 2

def self.template(method, hash={}) 
  method = method.to_s

  file_code = 
    if hash.has_key?(:property)
      "lookup_property(#{ hash[:property].inspect })"
    elsif hash.has_key?(:file)
      hash[:file].inspect
    elsif hash.has_key?(:local)
      # like file, but use relative to current path

      caller_path = caller.first.sub(/:\d+$/, "")

      File.join(File.dirname(caller_path), hash[:local]).inspect
    else
      caller_path = caller.first.sub(/:\d+$/, "")
      basename = caller_path[0, caller_path.rindex(".rb")]
      ext = '.tpl'

      if method =~ /^render(.*)$/
        basename + ext + $1.tr('_', '-')
      else
        raise ArgumentError
      end.inspect
    end

  if hash[:r_as_param]
    class_eval %{
      def #{ method }(r)
        r.template(#{ file_code })
      end
    }
  else
    class_eval %{
      def #{ method }
        r.template(#{ file_code })
      end
    }
  end
end

.uses_property(*args) ⇒ Object

This is currently only used for describing which properties are required by the underlying component.



119
120
# File 'lib/wee/core_ext.rb', line 119

def self.uses_property(*args)
end

Instance Method Details

#backtrack_state(snapshot) ⇒ Object

Dummy implementation. See Component#backtrack_state for more information.

snapshot

An object of class Snapshot



97
98
# File 'lib/wee/core/presenter.rb', line 97

def backtrack_state(snapshot)
end

#do_render(rendering_context) ⇒ Object

Render the presenter in the given rendering context. DO NOT overwrite this method, unless you know exactly what you’re doing!

Creates a new renderer object of the class returned by method #renderer_class, makes this the current renderer, then invokes method #render.

rendering_context

An object of class RenderingContext



33
34
35
# File 'lib/wee/core/presenter.rb', line 33

def do_render(rendering_context)
  with_renderer_for(rendering_context) do render() end 
end

#get_property(prop) ⇒ Object

Returns an “owned” property.



62
63
64
65
66
67
68
# File 'lib/wee/core_ext.rb', line 62

def get_property(prop)
  if self.properties
    self.properties[prop]
  else
    nil
  end
end

#lookup_property(prop) ⇒ Object

Tries to lookup a property from different places. nil as property value is not allowed!

Search order:

  1. self.get_property(prop)

  2. session.get_property(prop, self.class)

  3. application.get_property(prop, self.class)

  4. session.get_property(prop, nil)

  5. application.get_property(prop, nil)

  6. @@properties



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/wee/core_ext.rb', line 88

def lookup_property(prop)
  val = get_property(prop)
  return val if val != nil

  sess = session()
  app = sess.application
  klass = self.class

  val = sess.get_property(prop, klass)
  return val if val != nil

  val = app.get_property(prop, klass)
  return val if val != nil

  val = sess.get_property(prop, nil)
  return val if val != nil

  val = app.get_property(prop, nil)
  return val if val != nil

  if defined?(@@properties)
    val = @@properties[prop]
    return val if val != nil
  end

  return nil
end

#process_callbacks(&block) ⇒ Object

Process all callbacks specified for this presenter.

block

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



82
83
84
# File 'lib/wee/core/presenter.rb', line 82

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

#propertiesObject



57
# File 'lib/wee/core_ext.rb', line 57

def properties() @__properties end

#properties=(props) ⇒ Object



58
# File 'lib/wee/core_ext.rb', line 58

def properties=(props) @__properties = props end

#renderObject

This method renders the content of the presenter.

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

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



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

def render
end

#sessionObject

Returns the current session. A presenter (or component) has always an associated session. The returned object is of class Wee::Session or a subclass thereof.



17
18
19
# File 'lib/wee/core_ext.rb', line 17

def session
  Wee::Session.current
end