| Module | ApplicationUtilities |
| In: |
lib/application_utilities.rb
|
Mixin module: Application helpers and tools.
Note that the web server must be restarted to recognize changes within this file.
Convert a variable into a lower case string.
# File lib/application_utilities.rb, line 8
8: def down
9: self.to_s.downcase
10: end
Extract the first data segment in a string that matches the regular expression defined by ‘pattern’.
# File lib/application_utilities.rb, line 27
27: def extract (
28: pattern
29: )
30: (/#{pattern}/.match(self)) ? ($1) : (FALSE)
31: end
Read and return the contents of a file.
# File lib/application_utilities.rb, line 37
37: def file_read (
38: file
39: )
40: contents = ''
41: File.open(file) { |f| contents = f.read } if File.readable?(file)
42:
43: contents
44: end
Return the internal Ruby data type of a variable.
# File lib/application_utilities.rb, line 50
50: def get_variable_class
51: self.class.down
52: end
Perform multiple gsub() operations on a string.
# File lib/application_utilities.rb, line 58
58: def gsubs (
59: substitutions
60: )
61: string = self.to_s
62: substitutions.each{|sub| string = string.gsub(/#{sub[0].to_s}/,sub[1].to_s)}
63: string
64: end
Search for ‘pattern’ in an array. The array may contain any depth of sub arrays.
# File lib/application_utilities.rb, line 71
71: def has (
72: *arguments
73: )
74: pattern = arguments[0]
75:
76: # Ignore case.
77: if (arguments[1])
78: self.flatten.each { |i| return TRUE if (i.down == pattern.down) }
79: else
80: self.flatten.each { |i| return TRUE if (i == pattern) }
81: end
82:
83: return FALSE
84: end
Load project specific libraries. Once a library has been initialized it will remain in memory until the web server is restarted.
# File lib/application_utilities.rb, line 111
111: def load_project_library (
112: project_host_id,
113: library
114: )
115: # Set the path for this project's library directory, and the full path to
116: # the requested library file.
117: path = MODULE_FILES.parse_data(project_host_id) + '/'
118: library_file = path + library
119:
120:
121: # Return in the affirmative if this library has already been loaded.
122: if ($:.has(path) && $".has(library_file))
123: return TRUE
124: # If the requested library is available then load it.
125: elsif (File.readable?(path) && File.readable?(library_file))
126: # If this project's library directory has not already been processed then
127: # add it to Rail's $LOAD_PATH, and note that it has been processed.
128: $:.push(path) if (!$:.has(path))
129:
130: # If this specific library file has not already been processed then
131: # load it, and note that it has been processed.
132: require library_file if (!$".has(library_file))
133:
134: return TRUE
135: else
136: return FALSE
137: end
138: end
Replace DATA references as found inside of a string with their appropriate values.
This method may be accessed in one of two ways:
1. Using one parameter:
2. Using any number of parameters:
Data parameters are mapped to DATA reference markers based on their parametric index. For example, if this method is called in the following way:
the result will be:
DATA references with non-existent values remain unparsed, and are thus present in this method‘s result.
# File lib/application_utilities.rb, line 164
164: def parse_data (
165: *arguments
166: )
167: if (arguments)
168: message = self
169:
170: # This method was accessed using a single array as it's argument.
171: if ((1 == arguments.size) && ('array' == arguments[0].get_variable_class))
172: data = arguments[0]
173: # This method was accessed using any number of parameters.
174: else
175: data = arguments
176: end
177:
178: # Replace all DATA markers in the message variable with their data.
179: if (message =~ /#{DATA_REFERENCE}/)
180: for id in 1..data.size
181: data_reference = '\{' + DATA + ':' + id.to_s + '\}'
182: message = message.gsub(/#{data_reference}/, data[id-1].to_s)
183: end
184: end
185:
186: return message
187: else
188: return FALSE
189: end
190: end
Return the time.
This method uses a variable length parameter list:
# File lib/application_utilities.rb, line 200
200: def t (
201: *arguments
202: )
203: time = Time.now
204: time + arguments[0] if (arguments[0])
205: time.strftime(DATETIME_FORMAT)
206: end
Convert a string from the URL safe format used by Code Blue into an SQL ‘LIKE’ pattern.
Steps and rules:
# File lib/application_utilities.rb, line 221
221: def url_decode (
222: *arguments
223: )
224: value = arguments[0]
225:
226: (arguments[1]) ? (for_sql = arguments[1]) : (for_sql = FALSE)
227:
228: subs = []
229:
230: if (for_sql)
231: subs << ['^', '%']
232: subs << ['-', '%']
233: subs << [URL_TITLE_ANTIREGEX, '%']
234: subs << ['$', '%']
235: else
236: subs << ['-', ' ']
237: subs << [URL_TITLE_ANTIREGEX, ' ']
238: end
239:
240: value.gsubs(subs)
241: end
Convert a string to the URL safe format used by Code Blue.
Steps and rules:
# File lib/application_utilities.rb, line 257
257: def url_encode (
258: *arguments
259: )
260: value = arguments[0]
261:
262: subs = []
263: subs << ['\s', '-']
264: subs << [INDEX_PROHIBITED_CHARS, '' ]
265: subs << [URL_TITLE_ANTIREGEX, '-']
266: subs << ['[\.\-]{2,}', '-']
267: subs << ['[\.]{2,}', '-']
268: subs << ['^-', '' ]
269: subs << ['\-$', '' ]
270:
271: url_encoded_value = value.gsubs(subs)
272:
273: # Construct a URL encoded value that includes a content item id:
274: # * if this method has been executed with a third parameter then
275: # append the content item id in all cases
276: # * if there is no third parameter, but there is a second parameter and
277: # the value contains special characters then include the content item id
278: if (arguments[2]||(arguments[1] && (url_decode(url_encoded_value)!=value)))
279: return "#{url_encoded_value}/#{arguments[1]}"
280: # Otherwise return the encoded value.
281: else
282: return url_encoded_value
283: end
284: end