Class: Cuca::Widget

Inherits:
Object
  • Object
show all
Defined in:
lib/cuca/widget.rb,
lib/cuca/session.rb

Overview

end

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}, &block) ⇒ Widget

initialize - don’t use widgets directly with .new.

params variables in form of hash(var=>val) to make available to the generator blocks if they require/need params will be passed to the output method block will also be passed to the output method



134
135
136
137
138
139
140
141
142
# File 'lib/cuca/widget.rb', line 134

def initialize(params = {}, &block)
 @_assigns = params[:assigns] || {} 
 @_args = params[:args] || {}
 @_profiler = params[:profiler] || nil
 @_block = block
 @_content = ""

 @@_hints ||= {}
end

Class Method Details

.clear_hintsObject

clear all hints



66
67
68
# File 'lib/cuca/widget.rb', line 66

def self.clear_hints
  @@_hints = {}
end

.define_attr_method(name, value = nil) ⇒ Object

this can be used by derrived classes



200
201
202
203
204
# File 'lib/cuca/widget.rb', line 200

def self.define_attr_method(name, value=nil)
  sing = class << self; self; end
  sing.class_eval "def #{name}; #{value.inspect}; end"
#  $stderr.puts "def #{name}; #{value.to_s.inspect}; end"
end

.run_attr_method(name) ⇒ Object

tries to run a class method if defined and return it



207
208
209
210
211
# File 'lib/cuca/widget.rb', line 207

def self.run_attr_method(name)
  return nil unless self.respond_to?(name.intern)
     
  self.send(name.intern)
end

Instance Method Details

#appObject

An accessor to the Cuca::app object



81
82
83
# File 'lib/cuca/widget.rb', line 81

def app
 $app
end

#cgiObject

An accessor to the global cgi variables



71
72
73
# File 'lib/cuca/widget.rb', line 71

def cgi
 $app.cgi
end

#clearObject

clear widgets generated content



160
161
162
# File 'lib/cuca/widget.rb', line 160

def clear
 @_content = ""
end

#contentObject

An accessor to @_content All ‘generators’ (like the mab -function) should append their generated clear text to @_content, latest with the before_to_s method



43
44
45
# File 'lib/cuca/widget.rb', line 43

def content
  @_content
end

#content=(newval) ⇒ Object

overwrite the content



48
49
50
# File 'lib/cuca/widget.rb', line 48

def content=(newval)
  @_content = newval
end

#controllerObject

an accessor to the current controller object - if available, otherwise nil



54
55
56
# File 'lib/cuca/widget.rb', line 54

def controller
  $controller_object || nil
end

#escape(text) ⇒ Object

Escape a string to use with URL etc..



108
109
110
# File 'lib/cuca/widget.rb', line 108

def escape(text)
  CGI::escape(text)
end

#escapeHTML(text) ⇒ Object

escape a string on HTML codes



118
119
120
# File 'lib/cuca/widget.rb', line 118

def escapeHTML(text)
  CGI::escapeHTML(text)
end

#get_assignsObject

will fetch a list of assigns to be passed to a code generator block this includes the :assigns from the constructor plus all instance variables from the widget



147
148
149
150
151
152
153
154
155
156
157
# File 'lib/cuca/widget.rb', line 147

def get_assigns
  a = @_assigns.clone

  self.instance_variables.each do |v|
    vs = v.to_s
    next if vs.match(/^\@\_/)
    next if vs.include?('cancel_execution')   # this is some internal key
    a[vs.gsub(/\@/,'')] = self.instance_variable_get(v)
  end
  a
end

#hintsObject

Hints is shared a shared container for all widgets. If you want to pass an information from one widget to another this can be useful. The last widget renered is the controller, then the Layout.



61
62
63
# File 'lib/cuca/widget.rb', line 61

def hints
  @@_hints
end

#logObject

An accessor to the global logger variables



76
77
78
# File 'lib/cuca/widget.rb', line 76

def log
 $app.logger
end

#output(*args, &block) ⇒ Object

Overwrite this method with a function that takes the arguments and optionally a block as you like.



167
168
169
# File 'lib/cuca/widget.rb', line 167

def output(*args, &block)
  @_content = "This widget doesnt have any content"
end

#paramsObject

an accessor to cgi.parameters variables. This is NOT params from the CGI class (see cgi_fix)



88
89
90
# File 'lib/cuca/widget.rb', line 88

def params
  $app.cgi.parameters
end

#query_parametersObject

accessor to cgi query parameters (http GET)



93
94
95
# File 'lib/cuca/widget.rb', line 93

def query_parameters
   $app.cgi.query_parameters
end

#request_methodObject

an accessor to request_method



103
104
105
# File 'lib/cuca/widget.rb', line 103

def request_method
  return $app.cgi.request_method
end

#request_parametersObject

accessor to the cgi request parameters (http POST)



98
99
100
# File 'lib/cuca/widget.rb', line 98

def request_parameters
  $app.cgi.request_parameters
end

#sessionObject



165
166
167
# File 'lib/cuca/session.rb', line 165

def session
   $session
end

#to_sObject

get cleartext for the widget



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/cuca/widget.rb', line 179

def to_s
  if @_profiler then 
     require 'profiler'
     Profiler__::start_profile
  end
     
  output(*@_args, &@_block)
  before_to_s if self.respond_to?(:before_to_s)
  out = @_content.to_s

  if @_profiler then
      Profiler__::stop_profile
      @_profiler.puts "____________________PROFILER #{self.class.inspect} ______________________"
      Profiler__::print_profile(@_profiler)
  end

  out
end

#unescape(text) ⇒ Object

Unescape an escaped string



113
114
115
# File 'lib/cuca/widget.rb', line 113

def unescape(text)
  CGI::unescape(text)
end

#unescapeHTML(text) ⇒ Object

unescape an html escaped string



123
124
125
# File 'lib/cuca/widget.rb', line 123

def unescapeHTML(text)
 CGI::unescapeHTML(text)
end