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 collapse

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

Class Method Summary collapse

Class Method Details

.add(klass) ⇒ Object



214
215
216
# File 'lib/adhearsion/plugin.rb', line 214

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

.config(name = nil, &block) ⇒ Object



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

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

.countObject



210
211
212
# File 'lib/adhearsion/plugin.rb', line 210

def count
  subclasses.length
end

.delete(plugin_name) ⇒ Object



218
219
220
221
# File 'lib/adhearsion/plugin.rb', line 218

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

.delete_allObject



223
224
225
# File 'lib/adhearsion/plugin.rb', line 223

def delete_all
  @subclasses = nil
end

.generators(mapping) ⇒ Object

Register generator classes

Examples:

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


120
121
122
123
124
# File 'lib/adhearsion/plugin.rb', line 120

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

.init(name = nil, opts = {}, &block) ⇒ Object

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



191
192
193
194
195
196
# File 'lib/adhearsion/plugin.rb', line 191

def init(name = nil, opts = {}, &block)
  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, self, opts, &block)
end

.init_plugins(*args) ⇒ Object

Recursively initialization of all the loaded plugins



166
167
168
169
170
# File 'lib/adhearsion/plugin.rb', line 166

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

.initializersObject



178
179
180
# File 'lib/adhearsion/plugin.rb', line 178

def initializers
  @initializers ||= Collection.new
end

.load_tasksObject



105
106
107
108
109
110
# File 'lib/adhearsion/plugin.rb', line 105

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

.plugin_name(name = nil) ⇒ Object



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

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

.plugin_name=(name) ⇒ Object



143
144
145
# File 'lib/adhearsion/plugin.rb', line 143

def plugin_name=(name)
  @plugin_name = name
end

.reset_rake_tasksObject



101
102
103
# File 'lib/adhearsion/plugin.rb', line 101

def reset_rake_tasks
  @@rake_tasks = []
end

.run(name = nil, opts = {}, &block) ⇒ Object

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



203
204
205
206
207
208
# File 'lib/adhearsion/plugin.rb', line 203

def run(name = nil, opts = {}, &block)
  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, self, opts, &block)
end

.run_plugins(*args) ⇒ Object



172
173
174
175
176
# File 'lib/adhearsion/plugin.rb', line 172

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

.runnersObject



182
183
184
# File 'lib/adhearsion/plugin.rb', line 182

def runners
  @runners ||= Collection.new
end

.show_descriptionObject



161
162
163
# File 'lib/adhearsion/plugin.rb', line 161

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

.subclassesObject



126
127
128
# File 'lib/adhearsion/plugin.rb', line 126

def subclasses
  @subclasses ||= []
end

.tasksObject

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
    


96
97
98
99
# File 'lib/adhearsion/plugin.rb', line 96

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