Module: AMQP::Events

Included in:
EventManager
Defined in:
lib/version.rb,
lib/amqp-events.rb,
lib/amqp-events/event.rb,
lib/amqp-events/events.rb,
lib/amqp-events/event_manager.rb

Defined Under Namespace

Classes: Event, EventError, EventManager, ExternalEvent, HandlerError

Constant Summary collapse

VERSION_FILE =

:nodoc:

Pathname.new(__FILE__).dirname + '../VERSION'
VERSION =
VERSION_FILE.exist? ? VERSION_FILE.read.strip : nil
UUID =
EventMachine::UuidGenerator

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(host) ⇒ Object

Once included into a class/module, gives this module .event macros for declaring events



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/amqp-events/events.rb', line 32

def self.included(host)

  host.instance_exec do
    def instance_events
      @instance_events ||= {}
    end

    def event(name, opts = {})
      sym_name = name.to_sym

      if instance_events.has_key? sym_name
        raise EventError.new "Unable to redefine Event #{name} with options #{opts.inspect}" if instance_events[sym_name] != opts
      else
        instance_events[sym_name] = opts

        # Defines instance method that has the same name as the Event being declared.
        # Calling it without arguments returns Event object itself
        # Calling it with block adds unnamed subscriber for Event
        # Calling it with arguments fires the Event
        # Such a messy interface provides some compatibility with C# events behavior
        define_method name do |*args, &block|
          events[sym_name] ||= Event.create(self, sym_name, opts)
          if args.empty?
            if block
              events[sym_name].subscribe &block
            else
              events[sym_name]
            end
          else
            events[sym_name].fire(*args)
          end
        end

        # Only supports assignment of the Event to self
        # Needed to support C#-like syntax : my_event += subscriber
        define_method "#{name}=" do |event|
          raise EventError.new("Wrong assignment of #{event.inspect} to #{events[name.to_sym].inspect}"
                ) unless event.equal? events[name.to_sym]
        end
      end
      sym_name # .event macro returns defined sym name
    end
  end

end

.require_libs(libs, opts = {}) ⇒ Object

Requires ruby source file(s). Accepts either single filename/glob or Array of filenames/globs. Accepts following options:

:file

Lib(s) required relative to this file - defaults to __FILE__

:dir

Required lib(s) located under this dir name - defaults to gem name



13
14
15
16
17
18
19
# File 'lib/amqp-events.rb', line 13

def self.require_libs(libs, opts={})
  file = Pathname.new(opts[:file] || __FILE__)
  [libs].flatten.each do |lib|
    name = file.dirname + (opts[:dir] || file.basename('.*')) + lib.gsub(/(?<!.rb)$/, '.rb')
    Pathname.glob(name.to_s).sort.each { |rb| require rb }
  end
end

Instance Method Details

#event(name, opts = {}) ⇒ Object



11
12
13
14
15
# File 'lib/amqp-events/events.rb', line 11

def event(name, opts = {})
  sym_name = name.to_sym
  self.class.event(sym_name, opts)
  events[sym_name] ||= Event.create(self, sym_name, opts)
end

#eventsObject



4
5
6
7
8
9
# File 'lib/amqp-events/events.rb', line 4

def events
  @events ||= self.class.instance_events.inject({}) do |events, (name, opts)|
    events[name] = Event.create(self, name, opts)
    events
  end
end

#subscribe(event, *args, &block) ⇒ Object Also known as: listen

object#subscribe(:Event) is a sugar-coat for object.Event#subscribe



18
19
20
# File 'lib/amqp-events/events.rb', line 18

def subscribe(event, *args, &block)
  event(event).subscribe(*args, &block)
end

#unsubscribe(event, *args, &block) ⇒ Object Also known as: remove

object#unsubscribe(:Event) is a sugar-coat for object.Event#unsubscribe



23
24
25
26
# File 'lib/amqp-events/events.rb', line 23

def unsubscribe(event, *args, &block)
  raise HandlerError.new "Unable to unsubscribe, there is no event #{event}" unless events[event.to_sym]
  events[event.to_sym].unsubscribe(*args, &block)
end