class Pluggaloid::Plugin

Attributes

vm[W]
defined_time[R]

最初にプラグインがロードされた時刻(uninstallされるとリセットする)

name[R]

プラグインの名前

spec[RW]

spec

Public Class Methods

__clear_aF4e__()
Alias for: clear!
call(event_name, *args) click to toggle source

イベント event_name を発生させる

Args

event_name

イベント名

*args

イベントの引数

Return

Delayer

# File lib/pluggaloid/plugin.rb, line 56
def call(event_name, *args)
  vm.Event[event_name].call(*args) end
clear!() click to toggle source
# File lib/pluggaloid/plugin.rb, line 89
def clear!
  if defined?(@vm) and @vm
    @vm.Event.clear!
    @vm = nil end
  __clear_aF4e__() end
Also aliased as: __clear_aF4e__
collect(event_name, *specs) click to toggle source

フィルタ event_name を実行し、defeventでPluggaloid::COLLECTと 宣言されている引数の結果を列挙するEnumeratorを返す

Args

event_name

イベント名(String | Symbol)

*specs

Pluggaloid::COLLECT以外の引数

Return

Enumerator
# File lib/pluggaloid/plugin.rb, line 74
def collect(event_name, *specs)
  vm.Event[event_name].collect(*specs)
end
create(plugin_name, &body) click to toggle source

プラグインのインスタンスを返す。 ブロックが渡された場合、そのブロックをプラグインのインスタンスのスコープで実行する

Args

plugin_name

プラグイン名

Return

Plugin

# File lib/pluggaloid/plugin.rb, line 37
def create(plugin_name, &body)
  self[plugin_name].instance_eval(&body) if body
  self[plugin_name] end
defevent(event_name, options = {}) click to toggle source

イベントを宣言する。

Args

event_name

イベント名

options

以下のキーを持つHash

:prototype

引数の数と型。Arrayで、type_strictが解釈できる条件を設定する

:priority

Delayerの優先順位

# File lib/pluggaloid/plugin.rb, line 47
def defevent(event_name, options = {})
  vm.Event[event_name].options = options end
filter_cancel!() click to toggle source

互換性のため

# File lib/pluggaloid/plugin.rb, line 83
def filter_cancel!
  vm.Filter.cancel! end
filtering(event_name, *args) click to toggle source

引数 args をフィルタリングした結果を返す

Args

*args

引数

Return

フィルタされた引数の配列

# File lib/pluggaloid/plugin.rb, line 64
def filtering(event_name, *args)
  vm.Event[event_name].filtering(*args) end
new(*args) click to toggle source

Args

plugin_name

プラグイン名

Calls superclass method
# File lib/pluggaloid/plugin.rb, line 107
def initialize(*args)
  super
  @defined_time = Time.new.freeze
  @events = Set.new
  @filters = Set.new
end
uninstall(plugin_name) click to toggle source

互換性のため

# File lib/pluggaloid/plugin.rb, line 79
def uninstall(plugin_name)
  self[plugin_name].uninstall end
vm() click to toggle source
# File lib/pluggaloid/plugin.rb, line 16
def vm
  @vm ||= begin
            raise Pluggaloid::NoDefaultDelayerError, "Default Delayer was not set." unless Delayer.default
            vm = Pluggaloid::VM.new(
              Delayer: Delayer.default,
              Plugin: self,
              Event: Pluggaloid::Event,
              Listener: Pluggaloid::Listener,
              Filter: Pluggaloid::Filter,
              HandlerTag: Pluggaloid::HandlerTag,
              Subscriber: Pluggaloid::Subscriber,
              StreamGenerator: Pluggaloid::StreamGenerator
            )
            vm.Event.vm = vm end end

Public Instance Methods

add_event(event_name, **kwrest, &callback) click to toggle source

イベントリスナを新しく登録する

Args

event

監視するEventのインスタンス

name:

名前(String | nil)

slug:

イベントリスナスラッグ(Symbol | nil)

tags:

Pluggaloid::HandlerTag|Array リスナのタグ

&callback

コールバック

Return

Pluggaloid::Listener

# File lib/pluggaloid/plugin.rb, line 123
def add_event(event_name, **kwrest, &callback)
  result = vm.Listener.new(vm.Event[event_name], **kwrest, &callback)
  @events << result
  result end
add_event_filter(event_name, **kwrest, &callback) click to toggle source

イベントフィルタを新しく登録する

Args

event_name

イベント名(String | Symbol)

name:

名前(String | nil)

slug:

フィルタスラッグ(Symbol | nil)

tags:

Pluggaloid::HandlerTag|Array フィルタのタグ

&callback

コールバック

Return

Pluggaloid::Filter

# File lib/pluggaloid/plugin.rb, line 137
def add_event_filter(event_name, **kwrest, &callback)
  result = vm.Filter.new(vm.Event[event_name], **kwrest, &callback)
  @filters << result
  result end
collect(event_name, *specs) click to toggle source
# File lib/pluggaloid/plugin.rb, line 166
def collect(event_name, *specs)
  self.class.collect(event_name, *specs)
end
collection(event_name, *specs, &block) click to toggle source

追加・削除がフィルタに反映されるコレクションオブジェクトを作成する。 同時に event_name にフィルタが定義され、フィルタが呼ばれると その時点のコレクションオブジェクトの内容を全て列挙する。 フィルタと対になるコレクションオブジェクトは、 _&block_ の引数として渡される。

Args

event_name

イベント名(String | Symbol)

*specs

Pluggaloid::COLLECT以外の引数

&block

コレクションオブジェクトを受け取って一度だけ実行されるblock

Return

_&block_ の戻り値

# File lib/pluggaloid/plugin.rb, line 180
def collection(event_name, *specs, &block)
  event = vm.Event[event_name]
  mutation = Pluggaloid::Collection.new(event, *specs)
  add_event_filter(event_name, name: 'collection(%s line %i)' % block.source_location) do |*args|
    if mutation.argument_hash_same?(args)
      mutation.values.each(&args[event.collect_index].method(:<<))
    end
    args
  end
  block.call(mutation)
end
defdsl(dsl_name, &callback) click to toggle source

DSLメソッドを新しく追加する。 追加されたメソッドは呼ぶと &callback が呼ばれ、その戻り値が返される。引数も順番通り全て &callbackに渡される

Args

dsl_name

新しく追加するメソッド名

&callback

実行されるメソッド

Return

self

# File lib/pluggaloid/plugin.rb, line 292
def defdsl(dsl_name, &callback)
  self.class.instance_eval {
    define_method(dsl_name, &callback) }
  self end
defevent(event_name, options={}) click to toggle source

イベント event_name を宣言する

Args

event_name

イベント名

options

イベントの定義

# File lib/pluggaloid/plugin.rb, line 281
def defevent(event_name, options={})
  vm.Event[event_name].defevent({ plugin: self, **options })
end
detach(*args) click to toggle source

イベントを削除する。 引数は、Pluggaloid::ListenerかPluggaloid::Filterのみ(on_*やfilter_*の戻り値)。 互換性のため、二つ引数がある場合は第一引数は無視され、第二引数が使われる。

Args

*args

引数

Return

self

# File lib/pluggaloid/plugin.rb, line 249
def detach(*args)
  listener = args.last
  case listener
  when vm.Listener, vm.Subscriber
    @events.delete(listener)
    listener.detach
  when vm.Filter
    @filters.delete(listener)
    listener.detach
  when Enumerable
    listener.each(&method(:detach))
  else
    raise ArgumentError, "Argument must be Pluggaloid::Listener, Pluggaloid::Filter, Pluggaloid::HandlerTag or Enumerable. But given #{listener.class}."
  end
  self end
filters(&block) click to toggle source

フィルタを列挙する

Return

Set of Pluggaloid::Filter

# File lib/pluggaloid/plugin.rb, line 234
def filters(&block)
  if block
    @filters.each(&block)
  else
    @filters.dup
  end
end
generate(event_name, *specs, **kwrest, &block) click to toggle source
# File lib/pluggaloid/plugin.rb, line 142
def generate(event_name, *specs, **kwrest, &block)
  vm.StreamGenerator.new(vm.Event[event_name], *specs, plugin: self, **kwrest, &block)
end
handler_tag(slug=SecureRandom.uuid, name=slug, &block) click to toggle source

このプラグインのHandlerTagを作る。 ブロックが渡された場合は、ブロックの中を実行し、ブロックの中で定義された Handler全てにTagを付与する。

Args

slug

スラッグ

name

タグ名

Return

Pluggaloid::HandlerTag

# File lib/pluggaloid/plugin.rb, line 200
def handler_tag(slug=SecureRandom.uuid, name=slug, &block)
  tag = case slug
        when String, Symbol
          vm.HandlerTag.new(slug: slug.to_sym, name: name.to_s, plugin: self)
        when vm.HandlerTag
          slug
        else
          raise Pluggaloid::TypeError, "Argument `slug' must be instance of Symbol, String or Pluggaloid::HandlerTag, but given #{slug.class}."
        end
  if block
    handlers = @events + @filters
    block.(tag)
    (@events + @filters - handlers).each do |handler|
      handler.add_tag(tag)
    end
  else
    tag
  end
end
listeners(&block) click to toggle source

イベントリスナを列挙する

Return

Set of Pluggaloid::Listener

# File lib/pluggaloid/plugin.rb, line 223
def listeners(&block)
  if block
    @events.each(&block)
  else
    @events.dup
  end
end
method_missing(method, *args, **kwrest, &proc) click to toggle source

マジックメソッドを追加する。

on_?name

add_event(name)

filter_?name

add_event_filter(name)

Calls superclass method
# File lib/pluggaloid/plugin.rb, line 310
def method_missing(method, *args, **kwrest, &proc)
  method_name = method.to_s
  case
  when method_name.start_with?('on')
    event_name = method_name[(method_name[2] == '_' ? 3 : 2)..method_name.size]
    add_event(event_name.to_sym, *args, **kwrest, &proc)
  when method_name.start_with?('filter')
    event_name = method_name[(method_name[6] == '_' ? 7 : 6)..method_name.size]
    add_event_filter(event_name.to_sym, **kwrest, &proc)
  else
    super
  end
end
on_unload(&callback)
Alias for: onunload
onunload(&callback) click to toggle source

プラグインが Plugin.uninstall される時に呼ばれるブロックを登録する。

# File lib/pluggaloid/plugin.rb, line 298
def onunload(&callback)
  add_event(:unload) do |plugin_slug|
    if plugin_slug == self.name
      callback.call
    end
  end
end
Also aliased as: on_unload
subscribe(event_name, *specs, **kwrest, &block) click to toggle source
# File lib/pluggaloid/plugin.rb, line 146
def subscribe(event_name, *specs, **kwrest, &block)
  if block
    result = vm.Subscriber.new(vm.Event[event_name], *specs, **kwrest, &block)
    @events << result
    result
  else
    Stream.new(
      Enumerator.new do |yielder|
        @events << vm.Subscriber.new(vm.Event[event_name], *specs, **kwrest) do |stream|
          stream.each(&yielder.method(:<<))
        end
      end.lazy
    )
  end
end
subscribe?(event_name, *specs) click to toggle source
# File lib/pluggaloid/plugin.rb, line 162
def subscribe?(event_name, *specs)
  vm.Event[event_name].subscribe?(*specs)
end
uninstall() click to toggle source

このプラグインを破棄する

Return

self

# File lib/pluggaloid/plugin.rb, line 268
def uninstall
  vm.Event[:unload].call(self.name)
  vm.Delayer.new do
    @events.map(&:detach)
    @filters.map(&:detach)
    self.class.destroy name
  end
  self end

Private Instance Methods

vm() click to toggle source
# File lib/pluggaloid/plugin.rb, line 326
def vm
  self.class.vm end