Module TemplateUtilities
In: lib/template_utilities.rb

Mixin module: Template helpers and tools.

Note that the web server must be restarted to recognize changes within this file.

Methods

Public Instance methods

Convert the first letter of all words in a string to upper case, all other characters are ignored. Words are delimited by the following characters:

  • white space
  • _
  • /
  • (
  • [

[Source]

    # File lib/template_utilities.rb, line 14
14:   def capitalize_words
15:     result = ''
16: 
17:     for w in self.to_s.split(/(_|\s|\/|\(|\[)/)
18:       result += $1.to_s if ('' != result)
19:       result += w.slice(0..0).upcase.to_s + w.slice(1..w.size).to_s
20:     end
21: 
22:     result
23:   end

Generate a string title from a URL path.

Steps and rules:

  • remove the leading ’/’ character
  • convert underscores into spaces
  • convert all non-word characters into spaces
  • capitalize all words
  • combine each segment using the ’:’ character into a single string

[Source]

    # File lib/template_utilities.rb, line 55
55:   def convert_path_to_title (
56:     path
57:   )
58:     # Result value.
59:     segments = []
60: 
61:     # Remove the leading slash character.
62:     path = path.to_s.gsub(/^\//, '')
63: 
64:     # Process each segment of this URL path, applying the appropriate 
65:     # transformations.
66:     path.split('/').each{ |e| segments << e.gsub(/[_\W]/,' ').capitalize_words }
67: 
68:     # If the URL contains an ID at the end, but this URL mapping is not for
69:     # the ID field, then remove the ID from the result.
70:     if ((segments.last =~ /#{ID}/) && (segments.size > 3) && 
71:         (segments[1] !~ /^date$/i))
72:       segments.pop
73:     end
74: 
75:     # Convert from numeric month representation to month name.
76:     if ((segments[1] =~ /^date$/i) && segments[3] && (segments[3] =~ /#{ID}/))
77:       segments[3] = MONTH_NAMES[segments[3].to_i]
78:     end
79: 
80:     segments.join(' : ')
81:   end

Generate a string title from an input that is formatted as a database field name, although the input need not be an actual database field name.

Steps and rules:

  • convert underscores into spaces
  • capitalize all words

[Source]

    # File lib/template_utilities.rb, line 34
34:   def convert_to_title (
35:     string
36:   )
37:     # Result value.
38:     segments = []
39: 
40:     # Apply the appropriate transformations.
41:     string.to_s.gsub(/[_]/,' ').capitalize_words
42:   end

Remove tags from a passage that contains HTML, and limit the result to #{length} characters.

[Source]

     # File lib/template_utilities.rb, line 88
 88:   def create_summary (
 89:     *arguments
 90:   )
 91:     string = arguments[0]
 92:     length = arguments[1] if (arguments[1])
 93: 
 94:     # Remove:
 95:     # * newlines and carriage returns
 96:     # * HTML tags 
 97:     # * multiple occurences of whitespace
 98:     # * all opening whitespace
 99:     result = string.gsub(/[\n\r]/,            ' ')
100:     result = result.gsub(/<[a-zA-Z\/][^>]*>/, '' )
101:     result = result.gsub(/[\s]{2,}/,          ' ')
102:     result = result.gsub(/^[\s]{1,}/,         '' )
103: 
104:     if (length && result.length > length)
105:       result = result[0..length].gsub(/ [^>]{1,30}$/, ' ...')
106:     end
107: 
108:     result
109:   end

Escape regular expression special characters.

[Source]

     # File lib/template_utilities.rb, line 115
115:   def escape_regex (
116:     string
117:   )
118:     REGEX_SPECIAL_CHARS.each { |c| string = string.gsub("#{c}", "\\#{c}") }
119: 
120:     string
121:   end

Generate a string value that is suitable for use as an HTML <table summary>.

[Source]

     # File lib/template_utilities.rb, line 290
290:   def html_table_summary (
291:     string
292:   )
293:     url_encode(string).gsub(SINGLE_QUOTE_CHARACTER, '').gsub('-', ' ')
294:   end

Return an HTML <img> src tag, including height and width attributes.

[Source]

     # File lib/template_utilities.rb, line 127
127:   def image (
128:     file
129:   )
130:     digest = Digest::MD5.hexdigest(file)
131: 
132:     # If the HTML for this image is cached, return it.
133:     if g('image', digest)
134:       return g('image', digest)
135:     # The HTML is not cached, so generate it.
136:     else
137:       src    = "src='#{IMAGES}#{file}'"
138:       file   = PROJECT_WWW.parse_data(@page.host_id) + IMAGES + file
139:       result = ''
140: 
141:       # Populate src, height, and width attributes iff the file is accessible.
142:       if File.readable?(file)
143:         if ((h = image_height(file)) && (w = image_width(file)))
144:           result = "#{src} height='#{h}' width='#{w}'"
145:         else
146:           result = src
147:         end
148: 
149:         # Cache the result.
150:         g('image', digest, result)
151:       end
152: 
153:       return result
154:     end
155:   end

Determine an image‘s height.

[Source]

     # File lib/template_utilities.rb, line 161
161:   def image_height (
162:     file
163:   )
164:     if File.readable?(file)
165:       image = MiniMagick::Image.from_file(file)
166:       image[:height]
167:     else
168:       return FALSE
169:     end
170:   end

Determine an image‘s width.

[Source]

     # File lib/template_utilities.rb, line 176
176:   def image_width (
177:     file
178:   )
179:     if File.readable?(file)
180:       image = MiniMagick::Image.from_file(file)
181:       image[:width]
182:     else
183:       return FALSE
184:     end
185:   end

Convert a Rails’ DateTime value into a grammatically correct, human readable date.

[Source]

     # File lib/template_utilities.rb, line 192
192:   def readable_date (
193:     string
194:   )
195:     DateTime::parse(string).strftime('%B %d, %Y')
196:   end

Convert a numeric value into a human readable size, in kilobytes or megabytes.

NOTE: This method is based on the Rails v1.2.2 method ActionView::Helpers::NumberHelper::number_to_human_size()

[Source]

     # File lib/template_utilities.rb, line 207
207:   def readable_size
208:     if (self.to_s =~ /#{ID}/)
209:       size = self.to_f
210:       case
211:         when size == 1        : "1 Byte"
212:         when size < 1.kilobyte: "%d Bytes" % size
213:         when size < 1.megabyte: "%.d KB" %(size / 1.0.kilobyte)
214:         when size < 1.gigabyte: "%.d MB" %(size / 1.0.megabyte)
215:         when size < 1.terabyte: "%.d GB" %(size / 1.0.gigabyte)
216:         else                    "%.d TB" %(size / 1.0.terabyte)
217:       end.sub('.0', '')
218:     end
219:   end

Construct a template element reference.

[Source]

     # File lib/template_utilities.rb, line 225
225:   def reference_assemble (
226:     arguments
227:   )
228:     # Initialize the template reference with it's reference type.
229:     reference = arguments[0]
230: 
231:     # Append the remaining reference data.
232:     (1..arguments.size-1).each { |i| reference += ':' + arguments[i].to_s }
233: 
234:     reference
235:   end

Interface to: reference_assemble_tag()

All template references other than link references are processed in the same manner, however link references require specialized logic, hence this method must be used instead of direct calls to reference_assemble_tag() such as reference_assemble_tag(LINK, …).

[Source]

     # File lib/template_utilities.rb, line 246
246:   def reference_assemble_link (
247:     *arguments
248:   )
249:     # Escape the ':' character, in all string segments.
250:     for i in 2..arguments.size-1
251:       # Convert all arguments to data type String, for use with gsub() and 
252:       # match() below.
253:       arguments[i] = arguments[i].to_s
254: 
255:       # Ignore ':' characters that are already escaped (i.e. do not double
256:       # escape them).
257:       while matches = arguments[i].match(/#{ESCAPED_COLON_REGEX}/)
258:         arguments[i].gsub!(/#{matches[0]}/,matches[0].gsub(/:/,'ESCAPED_COLON'))
259:       end
260:     end
261: 
262: 
263:     reference_assemble_tag(LINK, *arguments)
264:   end

Construct a template element reference.

[Source]

     # File lib/template_utilities.rb, line 270
270:   def reference_assemble_tag (
271:     *arguments
272:   )
273:     REFERENCE_TAG_TEMPLATE.parse_data(reference_assemble(arguments))
274:   end

Display the number of results, only for pages with more than a single item.

[Source]

     # File lib/template_utilities.rb, line 280
280:   def result_message (
281:     count
282:   )
283:     (count.to_i > 1) ? ("#{count} results") : ('')
284:   end

Interface to: reference_assemble_tag()

Simplify calls of the format:

[Source]

     # File lib/template_utilities.rb, line 303
303:   def variable_html (
304:     variable
305:   )
306:     reference_assemble_tag(VARIABLE, 'html', variable)
307:   end

Interface to: reference_assemble_tag()

Simplify calls of the format:

[Source]

     # File lib/template_utilities.rb, line 316
316:   def variable_js (
317:     variable
318:   )
319:     reference_assemble_tag(VARIABLE, 'js', variable)
320:   end

Interface to: reference_assemble_tag()

Simplify calls of the format:

[Source]

     # File lib/template_utilities.rb, line 329
329:   def variable_text (
330:     variable
331:   )
332:     reference_assemble_tag(VARIABLE, 'text', variable)
333:   end

[Validate]