Class: Tengine::Core::Handler

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document, Mongoid::Timestamps, CollectionAccessible, SelectableAttr
Defined in:
lib/tengine/core/handler.rb

Overview

イベントハンドラ

Tengineコアは、イベントを受信するとそのイベント種別名にマッチするイベントハンドラを探して 見つかったイベントハンドラをすべて実行します。

Defined Under Namespace

Classes: Visitor

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#イベントが対象かどうかを判断するためのフィルタ定義Object



27
# File 'lib/tengine/core/handler.rb', line 27

field :filter, :type => Hash, :default => {}

#処理するイベントのイベント種別名の配列Object



23
# File 'lib/tengine/core/handler.rb', line 23

field :event_type_names, :type => Array

#実行するRubyのブロックが定義されているファイルでの行番号Object



20
# File 'lib/tengine/core/handler.rb', line 20

field :lineno  , :type => Integer

#実行するRubyのブロックが定義されているファイル名Object



17
# File 'lib/tengine/core/handler.rb', line 17

field :filepath, :type => String

#実行対象となるメソッドの名前Object



40
# File 'lib/tengine/core/handler.rb', line 40

field :target_method_name, :type => String

#実行対象の取得方法Object



31
# File 'lib/tengine/core/handler.rb', line 31

field :target_instantiation_cd, :type => String, :default => '01'

Instance Method Details

#fire(event_type_name) ⇒ Object



89
90
91
# File 'lib/tengine/core/handler.rb', line 89

def fire(event_type_name)
  @caller.fire(event_type_name)
end

#match?(event) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
96
97
# File 'lib/tengine/core/handler.rb', line 93

def match?(event)
  result = filter.blank? ? true : Visitor.new(filter, event, driver.session).visit
  Tengine.logger.debug("match?(#{event.event_type_name.inspect}) => #{result.inspect}")
  result
end

#process_event(event) ⇒ Object

def process_event(event, &block)

  @caller = eval("self", block.binding)
  matched = match?(event)
  if matched
    # ハンドラの実行
    @caller.__safety_driver__(self.driver) do
      @caller.__safety_event__(event) do
        @caller.instance_eval(&block)
      end
    end
  end
ensure
  @caller = nil
end


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/tengine/core/handler.rb', line 70

def process_event(event)
  case self.target_instantiation_key
  when :instance_method then
    klass = driver.target_class_name.constantize
    inst = klass.new
    inst.instance_variable_set(:@__event__, event)
    m = inst.method(target_method_name)
    m.arity == 0 ? m.call : m.call(event)
  when :static then
    klass = driver.target_class_name.constantize
    m = klass.method(target_method_name)
    m.arity == 0 ? m.call : m.call(event)
  when :binding then
    # do nothing
  else
    raise Tengine::Core::KernelError, "Unsupported target_instantiation_key: #{self.target_instantiation_key.inspect}"
  end
end

#update_handler_pathObject



48
49
50
51
52
53
# File 'lib/tengine/core/handler.rb', line 48

def update_handler_path
  event_type_names.each do |event_type_name|
    Tengine::Core::HandlerPath.create!(:event_type_name => event_type_name,
      :driver_id => self.driver.id, :handler_id => self.id)
  end
end