File: entity.rb

package info (click to toggle)
ruby-amqp 1.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 2,508 kB
  • sloc: ruby: 8,272; sh: 11; makefile: 10
file content (71 lines) | stat: -rw-r--r-- 1,747 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
require "amqp/openable"
require "amqp/callbacks"

module AMQP
  module RegisterEntityMixin
    # @example Registering Channel implementation
    #  Adapter.register_entity(:channel, Channel)
    #   # ... so then I can do:
    #  channel = client.channel(1)
    #  # instead of:
    #  channel = Channel.new(client, 1)
    def register_entity(name, klass)
      define_method(name) do |*args, &block|
        klass.new(self, *args, &block)
      end # define_method
    end # register_entity
  end # RegisterEntityMixin

  module ProtocolMethodHandlers
    def handle(klass, &block)
      HandlersRegistry.register(klass, &block)
    end

    def handlers
      HandlersRegistry.handlers
    end
  end # ProtocolMethodHandlers


  # AMQ entities, as implemented by AMQP, have callbacks and can run them
  # when necessary.
  #
  # @note Exchanges and queues implementation is based on this class.
  #
  # @abstract
  module Entity

    #
    # Behaviors
    #

    include Openable
    include Callbacks

    #
    # API
    #

    # @return [Array<#call>]
    attr_reader :callbacks


    def initialize(connection)
      @connection = connection
      # Be careful with default values for #ruby hashes: h = Hash.new(Array.new); h[:key] ||= 1
      # won't assign anything to :key. MK.
      @callbacks  = Hash.new
    end # initialize
  end # Entity

  # Common behavior of AMQ entities that can be either client or server-named, for example, exchanges and queues.
  module ServerNamedEntity

    # @return [Boolean] true if this entity is anonymous (server-named)
    def server_named?
      @server_named || @name.nil? || @name.empty?
    end
    # backwards compabitility. MK.
    alias anonymous? server_named?
  end # ServerNamedEntity
end