Module: RSence::Plugins::PluginBase

Included in:
Plugin__, Servlet__
Defined in:
lib/rsence/plugins/plugin_base.rb

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 of initialize

  • #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.

See also

Instance Method Summary collapse

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)



32
33
34
# File 'lib/rsence/plugins/plugin_base.rb', line 32

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.

Parameters:

  • path (String, false) (defaults to: false)

    The path is relative to the bundle path by default, unless it starts with ‘/’ or ‘..’.

  • prefix (String, false) (defaults to: false)

    Alternative root path if path is specified as a relative path.

  • suffix (String, false) (defaults to: false)

    The file suffix, like the the extension.

Returns:

  • (String)

    Full absolute path.



187
188
189
190
191
192
193
194
195
196
197
# File 'lib/rsence/plugins/plugin_base.rb', line 187

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.expand_path( path, @path )
  return path
end

#closenil

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.

Returns:

  • (nil)


69
70
# File 'lib/rsence/plugins/plugin_base.rb', line 69

def close
end

#file_exist?(path) ⇒ false, true

File existence checker

Just a wrapper for File.exist, except it handles the path via #bundle_path

Returns:

  • (false)

    if the file does not exist.

  • (true)

    if the file exists.



78
79
80
81
# File 'lib/rsence/plugins/plugin_base.rb', line 78

def file_exist?( path )
  path = bundle_path( path )
  File.exist?( path )
end

#file_read(path) ⇒ false, String

File reader utility

Reads the contents of the file given in the path.

Parameters:

  • path (String)

    The path is relative to the bundle path by default, unless it starts with ‘/’ or ‘..’; it’s simply processed by #bundle_path before reading the file.

Returns:

  • (false)

    If there is no file, returns false

  • (String)

    The contents of the file.



91
92
93
94
95
# File 'lib/rsence/plugins/plugin_base.rb', line 91

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

File writer utility.

Writes the contents of the data into the file given in the path.

Parameters:

  • path (String)

    The path is relative to the bundle path by default, unless it starts with ‘/’ or ‘..’; it’s simply processed by #bundle_path before writing the file.

  • data (#to_s)

    The data to write.

Returns:

  • (true, false)

    A success code of the operation (false for failure and true for success).



155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/rsence/plugins/plugin_base.rb', line 155

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

#flushnil

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.

Returns:

  • (nil)


61
62
# File 'lib/rsence/plugins/plugin_base.rb', line 61

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

Parameters:

  • time (Time, false) (defaults to: false)

    An Time object to format. Uses the current date/time by default.

Returns:

  • (String)

    The date/time formatted according to the HTTP RFC specification.



206
207
208
209
# File 'lib/rsence/plugins/plugin_base.rb', line 206

def httime(time=false)
  time = Time.new unless time 
  return time.gmtime.strftime('%a, %d %b %Y %H:%M:%S %Z')
end

#initnil

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.

Returns:

  • (nil)


45
46
# File 'lib/rsence/plugins/plugin_base.rb', line 45

def init
end

#json_read(path) ⇒ false, Object

JSON reader utility

Reads the contents of the JSON file given in the path and returns as a parsed structure of the contents of the file.

Parameters:

  • path (String)

    The path is relative to the bundle path by default, unless it starts with ‘/’ or ‘..’; it’s simply processed by #bundle_path before reading the file.

Returns:

  • (false)

    If the is no file, returns false

  • (Object)

    Any valid structure defined by the YAML file, parsed to a Ruby object.



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/rsence/plugins/plugin_base.rb', line 132

def json_read( path )
  file_data = file_read( path )
  if not file_data
    return false
  else
    begin
      return JSON.parse( file_data )
    rescue => e
      warn "An exception occurred while parsing JSON file: #{path}"
      warn e.message
      warn "  #{e.backtrace.join("\n  ")}"
      return false
    end
  end
end

#opennil

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.

Returns:

  • (nil)


53
54
# File 'lib/rsence/plugins/plugin_base.rb', line 53

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.

Parameters:

  • path (String)

    The path is relative to the bundle path by default, unless it starts with ‘/’ or ‘..’; it’s simply processed by #bundle_path before reading the file.

Returns:

  • (false)

    If the is no file, returns false

  • (Object)

    Any valid structure defined by the YAML file, parsed to a Ruby object.



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/rsence/plugins/plugin_base.rb', line 105

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.message
      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.



173
174
175
# File 'lib/rsence/plugins/plugin_base.rb', line 173

def yaml_write( path, data )
  file_write( path, data.to_yaml )
end