Module: RSence::Plugins::PluginBase
Overview
This module contains common functionality included in the Plugin, GUIPlugin and the Servlet base classes.
Extension hooks for server events
These methods are provided as the basic server event hooks:
-
#init
– Use instead ofinitialize
-
#open
– Extend to open objects -
#flush
– Extend to write the state and to flush buffers -
#close
– Extend to close objects
Utility methods
These are general utility methods not intended to be extended.
-
#file_read
Use to read files -
#yaml_read
Use to read yaml data -
#file_write
Use to write files -
#bundle_path
Use for plugin bundle -specific paths -
#httime
Use for HTTP date/time
See also
-
Plugin Bundles – General information about the plugin bundle system
-
Plugin – The Plugin base class
-
Servlet – The Servlet base class
-
GUIPlugin – The GUIPlugin base class
Instance Method Summary collapse
-
#bundle_path(path = false, prefix = false, suffix = false) ⇒ String
Path utility.
-
#close ⇒ nil
Extend to close objects like streams and database connections.
-
#file_read(path) ⇒ false, String
File reader utility.
-
#file_write(path, data) ⇒ true, false
(also: #file_save)
Flie writer utility.
-
#flush ⇒ nil
Extend to save your plugin state, write or flush any data that needs to be stored.
-
#httime(time = false) ⇒ String
Utility for returning the time in the HTTP RFC specification format, like: !!!text Sun, 04 Jul 2010 06:20:53 EEST.
-
#init ⇒ nil
Extend this method do any initial configuration instead of extending the
initialize
constructor, which should never be done in plugins. -
#method_missing(sym, *args, &block) ⇒ Object
When a method is missing, tries a call via pluginmanager.
-
#open ⇒ nil
Extend to open objects like streams and database connections.
-
#yaml_read(path) ⇒ false, Object
YAML reader utility.
-
#yaml_write(path, data) ⇒ Object
Yaml writer utility.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args, &block) ⇒ Object
When a method is missing, tries a call via pluginmanager. (Eliminates plugins.plugin_name.method_name calls -> plugin_name.method_name)
40 41 42 |
# File 'lib/rsence/plugins/plugin_base.rb', line 40 def method_missing( sym, *args, &block ) @plugins.method_missing( sym, *args, &block ) end |
Instance Method Details
#bundle_path(path = false, prefix = false, suffix = false) ⇒ String
Path utility
Makes a full, absolute path using the plugin bundle as the default path when a relative path is given. Returns just the bundle’s local path, if no parameters given.
160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/rsence/plugins/plugin_base.rb', line 160 def bundle_path( path=false, prefix=false, suffix=false ) return @path if not path if suffix path = "#{path}#{suffix}" unless path.end_with?(suffix) end if prefix path = File.join( prefix, path ) end path = File.( path, @path ) return path end |
#close ⇒ nil
Extend to close objects like streams and database connections.
It is called by the PluginManager when the plugin is about to be destructed, so don’t expect any events after it has been called.
77 78 |
# File 'lib/rsence/plugins/plugin_base.rb', line 77 def close end |
#file_read(path) ⇒ false, String
File reader utility
Reads the contents of the file given in the path
.
88 89 90 91 92 |
# File 'lib/rsence/plugins/plugin_base.rb', line 88 def file_read( path ) path = bundle_path( path ) return false unless File.exist?( path ) return File.read( path ) end |
#file_write(path, data) ⇒ true, false Also known as: file_save
Flie writer utility.
Writes the contents of the data
into the file given in the path
.
128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/rsence/plugins/plugin_base.rb', line 128 def file_write( path, data ) path = bundle_path( path ) begin datafile = File.open( path, 'wb' ) datafile.write( data.to_s ) datafile.close return true rescue => e warn "file_write error for path #{path} #{e}" return false end end |
#flush ⇒ nil
Extend to save your plugin state, write or flush any data that needs to be stored.
It is called by the PluginManager before #close, but doesn’t always mean a close
event is imminent.
69 70 |
# File 'lib/rsence/plugins/plugin_base.rb', line 69 def flush end |
#httime(time = false) ⇒ String
Utility for returning the time in the HTTP RFC specification format, like:
Sun, 04 Jul 2010 06:20:53 EEST
179 180 181 182 |
# File 'lib/rsence/plugins/plugin_base.rb', line 179 def httime(time=false) time = Time.new unless time return time.gmtime.strftime('%a, %d %b %Y %H:%M:%S %Z') end |
#init ⇒ nil
Extend this method do any initial configuration instead of extending the initialize
constructor, which should never be done in plugins.
It is called by the PluginManager when the plugin has been constructed and registered.
53 54 |
# File 'lib/rsence/plugins/plugin_base.rb', line 53 def init end |
#open ⇒ nil
Extend to open objects like streams and database connections.
It is called by the PluginManager after the #init method, when everything is constructed after all plugins are loaded.
61 62 |
# File 'lib/rsence/plugins/plugin_base.rb', line 61 def open end |
#yaml_read(path) ⇒ false, Object
YAML reader utility
Reads the contents of the YAML file given in the path
and returns as a parsed structure of the contents of the file.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/rsence/plugins/plugin_base.rb', line 102 def yaml_read( path ) file_data = file_read( path ) if not file_data return false else begin return YAML.load( file_data ) rescue Psych::SyntaxError => e warn "Syntax Error in YAML file: #{path} (#{e.message})" return false rescue => e warn "An exception occurred while parsing YAML file: #{path}" warn e. warn " #{e.backtrace.join("\n ")}" return false end end end |
#yaml_write(path, data) ⇒ Object
Yaml writer utility.
Wrapper for #file_write Converts data
to yaml and then calls file_write with the result.
146 147 148 |
# File 'lib/rsence/plugins/plugin_base.rb', line 146 def yaml_write( path, data ) file_write( path, data.to_yaml ) end |