class Rubirai::Event

@abstract

Attributes

bot[R]

@!attribute [r] bot

@return [Bot]

@!attribute [r] raw

The raw hash representation of the event
@return [Hash]
raw[R]

@!attribute [r] bot

@return [Bot]

@!attribute [r] raw

The raw hash representation of the event
@return [Hash]

Public Class Methods

gen_descendants() click to toggle source

@private

# File lib/rubirai/events/event.rb, line 10
def self.gen_descendants
  descs = ObjectSpace.each_object(Class).select do |klass|
    klass < self
  end

  metaclass.instance_eval do
    define_method(:descendants) do
      descs
    end
    leaf_descs = descs.filter { |d| d.respond_to? :type }
    all_types = leaf_descs.map(&:type)
    define_method(:all_types) do
      all_types
    end
    type_map = leaf_descs.to_h { |d| [d.type, d] }
    define_method(:type_map) do
      type_map
    end
  end

  private_class_method :descendants
end
new(hash, bot = nil) click to toggle source

@private

# File lib/rubirai/events/event.rb, line 91
def initialize(hash, bot = nil)
  @raw = hash
  @bot = bot
end
parse(hash, bot = nil) click to toggle source

@param hash [Hash] @param bot [Rubirai::Bot, nil]

# File lib/rubirai/events/event.rb, line 78
def self.parse(hash, bot = nil)
  hash = hash.stringify_keys
  type_to_klass(hash['type']).new hash, bot
end
set_event(type, *attr_keys) click to toggle source
Calls superclass method
# File lib/rubirai/events/event.rb, line 43
def self.set_event(type, *attr_keys)
  attr_reader(*attr_keys)

  metaclass.instance_eval do
    break if type.nil?
    define_method(:type) do
      type
    end
  end

  class_eval do
    define_method(:initialize) do |hash, bot = nil|
      # noinspection RubySuperCallWithoutSuperclassInspection
      super hash, bot
      hash = hash.stringify_keys
      attr_keys.each do |k|
        k2 = k.to_s.snake_to_camel(lower: true)
        val = hash[k2]
        val = parse_val_from_key k2, val, bot
        instance_variable_set("@#{k}", val)
      end
    end
  end
end
type_to_klass(type) click to toggle source
# File lib/rubirai/events/event.rb, line 33
def self.type_to_klass(type)
  # noinspection RubyResolve
  type_map[type.to_sym]
end
valid_type?(type) click to toggle source
# File lib/rubirai/events/event.rb, line 38
def self.valid_type?(type)
  # noinspection RubyResolve
  all_types.include? type
end

Private Class Methods

metaclass() click to toggle source
# File lib/rubirai/events/event.rb, line 68
def self.metaclass
  class << self
    self
  end
end

Protected Instance Methods

parse_val_from_key(key, val, bot) click to toggle source
# File lib/rubirai/events/event.rb, line 98
def parse_val_from_key(key, val, bot)
  case key
  when 'group'
    Group.new val, bot
  when 'operator', 'member'
    GroupUser.new val, bot
  when 'sender'
    if val.key? 'group'
      GroupUser.new val, bot
    else
      User.new val, bot
    end
  when 'messageChain'
    MessageChain.new bot, val
  else
    val
  end
end