Class: Rubirai::Event Abstract
- Inherits:
-
Object
- Object
- Rubirai::Event
- Defined in:
- lib/rubirai/events/event.rb
Overview
This class is abstract.
Direct Known Subclasses
BotEvent, MessageEvent, RecallEvent, RequestEvent, RubiraiErrorEvent
Instance Attribute Summary collapse
- #bot ⇒ Bot readonly
-
#raw ⇒ Object
readonly
Returns the value of attribute raw.
Class Method Summary collapse
- .parse(hash, bot = nil) ⇒ Object
- .set_event(type, *attr_keys) ⇒ Object
- .type_to_klass(type) ⇒ Object
- .valid_type?(type) ⇒ Boolean
Instance Method Summary collapse
-
#initialize(hash, bot = nil) ⇒ Event
constructor
A new instance of Event.
Constructor Details
#initialize(hash, bot = nil) ⇒ Event
Returns a new instance of Event.
105 106 107 108 |
# File 'lib/rubirai/events/event.rb', line 105 def initialize(hash, bot = nil) @raw = hash @bot = bot end |
Instance Attribute Details
#raw ⇒ Object (readonly)
Returns the value of attribute raw.
103 |
# File 'lib/rubirai/events/event.rb', line 103 attr_reader :bot, :raw |
Class Method Details
.parse(hash, bot = nil) ⇒ Object
93 94 95 96 |
# File 'lib/rubirai/events/event.rb', line 93 def self.parse(hash, bot = nil) hash = hash.stringify_keys type_to_klass(hash['type']).new hash, bot end |
.set_event(type, *attr_keys) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/rubirai/events/event.rb', line 43 def self.set_event(type, *attr_keys) attr_reader(*attr_keys) .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 = case k2 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 instance_variable_set("@#{k}", val) end end end end |
.type_to_klass(type) ⇒ Object
33 34 35 36 |
# 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) ⇒ Boolean
38 39 40 41 |
# File 'lib/rubirai/events/event.rb', line 38 def self.valid_type?(type) # noinspection RubyResolve all_types.include? type end |