class ChatX::Event

Constants

EVENT_CLASSES

Attributes

hash[R]
type[R]
type_long[R]
type_short[R]

Public Class Methods

new(event_hash, server, bot) click to toggle source
# File lib/chatx/events.rb, line 140
def initialize(event_hash, server, bot)
  @type = event_hash['event_type'].to_i
  @type_short = EVENT_SHORTHAND[@type - 1]
  @type_long = EVENT[@type - 1]
  @bot = bot

  @etypes = []

  @hash = event_hash # .delete("event_type")

  # rubocop:disable Style/GuardClause
  if EVENT_CLASSES.include? @type
    classes = EVENT_CLASSES[@type]
    classes.each do |c, i|
      if c.class == Array
        clazz = c[0]
        method_name = c[1]
      else
        clazz = c
        method_name = clazz.to_s.split('::').last.downcase
      end
      init_properties = i.map { |k, v| [k, event_hash[v]] }.to_h
      # This bit is fun. It creates a ChatX::<event type> and uses the method_missing
      # to make its methods all aliased to this ChatX::Event, and also defines an
      # <event type> method on this ChatX::Event which returns the ChatX::<event type>
      # object.
      event_type_obj = clazz.new(server, **init_properties)
      @etypes.push event_type_obj
      instance_variable_set "@#{method_name}", event_type_obj
      self.class.send(:define_method, method_name) do
        instance_variable_get "@#{method_name}"
      end
    end
  end
  # rubocop:enable Style/GuardClause

  def method_missing(m, *args, **opts, &block)
    etypes = @etypes.select { |e| e.respond_to? m }
    super if etypes.empty?
    etype = etypes.first
    if etype.respond_to? m
      meth = etype.method(m)
      # Because it treats **opts as 1 argument
      if opts.empty?
        meth.call(*args, &block)
      else
        meth.call(*args, **opts, &block)
      end
    end
  end

  def respond_to_missing?(m, *)
    !@etypes.select { |e| e.respond_to? m }.empty? || super
  end
end

Public Instance Methods

method_missing(m, *args, **opts, &block) click to toggle source

rubocop:enable Style/GuardClause

Calls superclass method
# File lib/chatx/events.rb, line 176
def method_missing(m, *args, **opts, &block)
  etypes = @etypes.select { |e| e.respond_to? m }
  super if etypes.empty?
  etype = etypes.first
  if etype.respond_to? m
    meth = etype.method(m)
    # Because it treats **opts as 1 argument
    if opts.empty?
      meth.call(*args, &block)
    else
      meth.call(*args, **opts, &block)
    end
  end
end
respond_to_missing?(m, *) click to toggle source
Calls superclass method
# File lib/chatx/events.rb, line 191
def respond_to_missing?(m, *)
  !@etypes.select { |e| e.respond_to? m }.empty? || super
end