class Operate::Pubsub::Events

Describes allowed events

Duck-types the argument to quack like array of strings when responding to the {#include?} method call.

Attributes

list[R]

Public Class Methods

new(list) click to toggle source

Initialize with a list of events.

@param [NilClass, String, Symbol, Array, Regexp] list

# File lib/operate/pubsub/events.rb, line 11
def initialize(list)
  @list = list
end

Public Instance Methods

include?(event) click to toggle source

Check if given event is included in the 'list' of events.

@param [#to_s] event

@return [Boolean]

# File lib/operate/pubsub/events.rb, line 20
def include?(event)
  appropriate_method.call(event.to_s)
end

Private Instance Methods

appropriate_method() click to toggle source
# File lib/operate/pubsub/events.rb, line 39
def appropriate_method
  @appropriate_method ||= methods[recognized_type]
end
methods() click to toggle source

Different event types and their corresponding matching method.

# File lib/operate/pubsub/events.rb, line 27
def methods
  {
    NilClass   => ->(_event) { true },
    String     => ->(event)  { list == event },
    Symbol     => ->(event)  { list.to_s == event },
    Enumerable => ->(event)  { list.map(&:to_s).include? event },
    Regexp     => ->(event)  { list.match(event) || false }
  }
end
recognized_type() click to toggle source
# File lib/operate/pubsub/events.rb, line 43
def recognized_type
  methods.keys.detect(&list.method(:is_a?)) || type_not_recognized
end
type_not_recognized() click to toggle source
# File lib/operate/pubsub/events.rb, line 47
def type_not_recognized
  raise(ArgumentError, "#{list.class} not supported for `on` argument")
end