class Wisper::ValueObjects::Events

Describes allowed events

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

Public Class Methods

new(list) click to toggle source

@!scope class @!method new(on) Initializes a ‘list’ of events

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

@raise [ArgumentError]

if an argument if of unsupported type

@return [undefined]

# File lib/wisper/value_objects/events.rb, line 19
def initialize(list)
  @list = list
end

Public Instance Methods

include?(event) click to toggle source

Check if given event is ‘included’ to the ‘list’ of events

@param [#to_s] event

@return [Boolean]

# File lib/wisper/value_objects/events.rb, line 28
def include?(event)
  appropriate_method.call(event.to_s)
end

Private Instance Methods

appropriate_method() click to toggle source
# File lib/wisper/value_objects/events.rb, line 48
def appropriate_method
  @appropriate_method ||= methods[recognized_type]
end
list() click to toggle source
# File lib/wisper/value_objects/events.rb, line 44
def list
  @list
end
methods() click to toggle source
# File lib/wisper/value_objects/events.rb, line 34
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/wisper/value_objects/events.rb, line 52
def recognized_type
  methods.keys.detect(&list.method(:is_a?)) || type_not_recognized
end
type_not_recognized() click to toggle source
# File lib/wisper/value_objects/events.rb, line 56
def type_not_recognized
  fail(ArgumentError, "#{list.class} not supported for `on` argument")
end