class Relevium::Service

Public Class Methods

call(*args) { |obj| ... } click to toggle source
# File lib/relevium/service.rb, line 7
def self.call(*args)
  obj = new(*args)
  yield obj if block_given?
  obj.call
end
new(*args) click to toggle source
# File lib/relevium/service.rb, line 13
def initialize(*args); end

Private Class Methods

global_registrations() click to toggle source
# File lib/relevium/service.rb, line 59
def self.global_registrations
  @global_registrations ||= Set.new
end
set_listener(klass, message, options = {}) click to toggle source
# File lib/relevium/service.rb, line 55
def self.set_listener(klass, message, options = {})
  global_registrations << GlobalRegistration.new(klass, message, options, self)
end

Public Instance Methods

on(name, &block) click to toggle source
# File lib/relevium/service.rb, line 15
def on(name, &block)
  local_registrations << BlockRegistration.new(name, block)
  self
end

Private Instance Methods

broadcast(name, *args) click to toggle source
# File lib/relevium/service.rb, line 22
def broadcast(name, *args)
  set_off_local_listeners(name, *args)
  set_off_global_listeners(name, *args)
  self
end
call() click to toggle source
# File lib/relevium/service.rb, line 63
def call
  raise NoMethodError
end
get_args(*args) click to toggle source
# File lib/relevium/service.rb, line 43
def get_args(*args)
  args.map { |arg| instance_variable_get("@#{arg}") }
end
local_registrations() click to toggle source
# File lib/relevium/service.rb, line 51
def local_registrations
  @local_registrations ||= Set.new
end
set_off_global_listeners(name, *args) click to toggle source
# File lib/relevium/service.rb, line 32
def set_off_global_listeners(name, *args)
  select_proc = Proc.new { |registration| registration.message == name }
  registrations = self.class.global_registrations.select(&select_proc)
  registrations += self.class.superclass&.global_registrations&.select(&select_proc)
  registrations.each do |registration|
    next unless registration.condition.nil? || registration.condition.call(self)

    registration.args.nil? ? registration.broadcast(*args) : registration.broadcast(*get_args(*registration.args))
  end
end
set_off_local_listeners(name, *args) click to toggle source
# File lib/relevium/service.rb, line 28
def set_off_local_listeners(name, *args)
  local_registrations.select { |registration| registration.listener == name }.first&.broadcast(*args)
end
transaction(&block) click to toggle source
# File lib/relevium/service.rb, line 47
def transaction(&block)
  ActiveRecord::Base.transaction(&block) if block_given?
end