Method: Natsy::Controller.subject

Defined in:
lib/natsy/controller.rb

.subject(subject_segment, queue: nil) ⇒ Object

Use the ::subject macro to create a block for listening to that subject segment. Nested calls to ::subject will append each subsequent subject/pattern string to the last (joined by a periods). There is no limit to the level of nesting.

NOTE: The following two examples do exactly the same thing.

Examples:

class FoobarNatsController < RubyNatsController
  # ...

  subject "hello.wassup" do
    response do |data, subject|
      # The subject at this point is "hello.wassup"
      # ...
    end
  end

  subject "hello.howdy" do
    response do |data, subject|
      # The subject at this point is "hello.howdy"
      # ...
    end
  end
end
class FoobarNatsController < RubyNatsController
  # ...

  subject "hello" do
    subject "wassup" do
      response do |data, subject|
        # The subject at this point is "hello.wassup"
        # ...
      end
    end

    subject "howdy" do
      response do |data, subject|
        # The subject at this point is "hello.howdy"
        # ...
      end
    end
  end
end


92
93
94
95
96
97
98
99
# File 'lib/natsy/controller.rb', line 92

def subject(subject_segment, queue: nil)
  subject_chain.push(subject_segment)
  old_queue = current_queue
  self.current_queue = queue if Utils.present?(queue)
  yield
  self.current_queue = old_queue
  subject_chain.pop
end