Class: Adhearsion::Plugin

Inherits:
Object show all
Extended by:
ActiveSupport::Autoload
Defined in:
lib/adhearsion/plugin.rb,
lib/adhearsion/plugin/collection.rb,
lib/adhearsion/plugin/initializer.rb

Overview

Plugin is the core of extension of Adhearsion framework and provides the easiest path to add new functionality, configuration or modify the initialization process.

Its behavior is based on Rails::Railtie, so if you are familiar with Rails this will be easier for you to start using Adhearsion::Plugin, but of course no previous knowledge is required.

With an Adhearsion Plugin you can:

  • create initializers

  • add rake tasks to Adhearsion

  • add/modify configuration files

How to create your Adhearsion Plugin

Create a class that inherits from Adhearsion::Plugin within your plugin namespace. This class shall be loaded during your awesome Adhearsion application boot process.

# lib/my_plugin/plugin.rb
module MyPlugin
  class Plugin < Adhearsion::Plugin
  end
end

Execute a specific code while initializing Adhearison

module MyPlugin
  class Plugin < Adhearsion::Plugin
    init :my_plugin do
      logger.warn "I want to ensure my plugin is being loaded!!!"
    end
  end
end

As Rails::Railtie does, you can define the exact point when you want to load your plugin during the initialization process.

module MyPlugin
  class Plugin < Adhearsion::Plugin
    init :my_plugin, :after => :my_other_plugin do
      logger.warn "My Plugin depends on My Other Plugin, so it must be loaded after"
    end
  end
end

Direct Known Subclasses

PunchblockPlugin

Defined Under Namespace

Classes: Collection, Initializer

Constant Summary

METHODS_OPTIONS =
{:load => true, :scope => false}

Class Method Summary (collapse)

Class Method Details

+ (Object) add(klass)



212
213
214
# File 'lib/adhearsion/plugin.rb', line 212

def add(klass)
  klass.ancestors.include?(self) and subclasses << klass
end

+ (Object) config(name = nil, &block)



145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/adhearsion/plugin.rb', line 145

def config(name = nil, &block)
  if block_given?
    if name.nil?
      name = self.plugin_name
    else
      self.plugin_name = name
    end
    ::Loquacious::Configuration.defaults_for name, &Proc.new
    ::Loquacious.configuration_for plugin_name, &block
  else
    ::Loquacious.configuration_for plugin_name
  end
end

+ (Object) count



208
209
210
# File 'lib/adhearsion/plugin.rb', line 208

def count
  subclasses.length
end

+ (Object) delete(plugin_name)



216
217
218
219
# File 'lib/adhearsion/plugin.rb', line 216

def delete(plugin_name)
  plugin_name.ancestors.include?(self) and plugin_name = plugin_name.plugin_name
  subclasses.delete_if { |plugin| plugin.plugin_name.eql? plugin_name }
end

+ (Object) delete_all



221
222
223
# File 'lib/adhearsion/plugin.rb', line 221

def delete_all
  @subclasses = nil
end

+ (Object) generators(mapping)

Register generator classes

Examples:

FooBar = Class.new Adhearsion::Plugin do
  generators :my_plugin:foo_generator' => FooGenerator
end


118
119
120
121
122
# File 'lib/adhearsion/plugin.rb', line 118

def generators(mapping)
  mapping.each_pair do |name, klass|
    Generators.add_generator name, klass
  end
end

+ (Object) init(name = nil, opts = {})

Class method that will be used by subclasses to initialize the plugin

Parameters:

  • name (defaults to: nil)

    Symbol plugin initializer name

  • opts (defaults to: {})

    Hash

    • :before specify the plugin to be loaded before another plugin

    • :after specify the plugin to be loaded after another plugin



189
190
191
192
193
194
# File 'lib/adhearsion/plugin.rb', line 189

def init(name = nil, opts = {})
  name = plugin_name unless name
  block_given? or raise ArgumentError, "A block must be passed while defining the Plugin initialization process"
  opts[:after] ||= initializers.last.name unless initializers.empty? || initializers.find { |i| i.name == opts[:before] }
  Adhearsion::Plugin.initializers << Initializer.new(name, nil, opts, &Proc.new)
end

+ (Object) init_plugins(*args)

Recursively initialization of all the loaded plugins



164
165
166
167
168
# File 'lib/adhearsion/plugin.rb', line 164

def init_plugins(*args)
  initializers.tsort.each do |initializer|
    initializer.run(*args)
  end
end

+ (Object) initializers



176
177
178
# File 'lib/adhearsion/plugin.rb', line 176

def initializers
  @initializers ||= Collection.new
end

+ (Object) load_tasks



103
104
105
106
107
108
# File 'lib/adhearsion/plugin.rb', line 103

def load_tasks
  container = Object.new.tap { |o| o.extend Rake::DSL if defined? Rake::DSL }
  tasks.each do |block|
    container.instance_eval(&block)
  end
end

+ (Object) plugin_name(name = nil)



133
134
135
136
137
138
139
# File 'lib/adhearsion/plugin.rb', line 133

def plugin_name(name = nil)
  if name.nil?
    @plugin_name ||= ActiveSupport::Inflector.underscore(self.name)
  else
    self.plugin_name = name
  end
end

+ (Object) plugin_name=(name)



141
142
143
# File 'lib/adhearsion/plugin.rb', line 141

def plugin_name=(name)
  @plugin_name = name
end

+ (Object) reset_rake_tasks



99
100
101
# File 'lib/adhearsion/plugin.rb', line 99

def reset_rake_tasks
  @@rake_tasks = []
end

+ (Object) run(name = nil, opts = {})

Class method that will be used by subclasses to run the plugin

Parameters:

  • name (defaults to: nil)

    Symbol plugin initializer name

  • opts (defaults to: {})

    Hash

    • :before specify the plugin to be loaded before another plugin

    • :after specify the plugin to be loaded after another plugin



201
202
203
204
205
206
# File 'lib/adhearsion/plugin.rb', line 201

def run(name = nil, opts = {})
  name = plugin_name unless name
  block_given? or raise ArgumentError, "A block must be passed while defining the Plugin run process"
  opts[:after] ||= runners.last.name unless runners.empty? || runners.find { |i| i.name == opts[:before] }
  Adhearsion::Plugin.runners << Initializer.new(name, nil, opts, &Proc.new)
end

+ (Object) run_plugins(*args)



170
171
172
173
174
# File 'lib/adhearsion/plugin.rb', line 170

def run_plugins(*args)
  runners.tsort.each do |runner|
    runner.run(*args)
  end
end

+ (Object) runners



180
181
182
# File 'lib/adhearsion/plugin.rb', line 180

def runners
  @runners ||= Collection.new
end

+ (Object) show_description



159
160
161
# File 'lib/adhearsion/plugin.rb', line 159

def show_description
  ::Loquacious::Configuration.help_for plugin_name
end

+ (Object) subclasses



124
125
126
# File 'lib/adhearsion/plugin.rb', line 124

def subclasses
  @subclasses ||= []
end

+ (Object) tasks

Class method that allows any subclass (any Adhearsion plugin) to register rake tasks.

  • Example 1:

    FooBar = Class.new Adhearsion::Plugin do
      tasks do
        namespace :foo_bar do
          desc "Prints the FooBar plugin version"
          task :version do
            STDOUT.puts "FooBar plugin v0.1"
          end
        end
      end
    end
  • Example 2:

    FooBar = Class.new Adhearsion::Plugin do
      tasks do
        load "tasks/foo_bar.rake"
      end
    end
    
    = tasks/foo_bar.rake
    
     namespace :foo_bar do
       desc "Prints the FooBar plugin version"
       task :version do
         STDOUT.puts "FooBar plugin v0.1"
       end
     end


94
95
96
97
# File 'lib/adhearsion/plugin.rb', line 94

def tasks
  @@rake_tasks << Proc.new if block_given?
  @@rake_tasks
end