Class: ChatX::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/chatx/events.rb

Constant Summary collapse

EVENT_CLASSES =
{
  1 => {
    ChatX::Message => {
      time_stamp: 'time_stamp',
      content: 'content',
      room_id: 'room_id',
      user_id: 'user_id',
      message_id: 'message_id',
      parent_id: 'parent_id'
    }
  },
  2 => {
    ChatX::Message => {
      time_stamp: 'time_stamp',
      content: 'content',
      room_id: 'room_id',
      user_id: 'user_id',
      message_id: 'message_id'
    }
  },
  3 => {
    ChatX::User => {
      user_id: 'target_user_id'
    },
    ChatX::Room => {
      room_id: 'room_id'
    }
  },
  4 => {
    ChatX::User => {
      user_id: 'target_user_id'
    },
    ChatX::Room => {
      room_id: 'room_id'
    }
  },
  5 => {
    ChatX::Room => {
      room_id: 'room_id'
    }
  },
  # Note: A star/unstar/pin/unpin event DOES NOT have a user_id, which throws an error in ChatX::Message intialization.
  # 6 => {
  #   ChatX::Message => {
  #     time_stamp: 'time_stamp',
  #     content: 'content',
  #     room_id: 'room_id',
  #     user_id: 'user_id',
  #     message_id: 'message_id'
  #   }
  # },
  8 => {
    ChatX::Message => {
      time_stamp: 'time_stamp',
      content: 'content',
      room_id: 'room_id',
      user_id: 'user_id',
      message_id: 'message_id'
    },
    [ChatX::User, 'source_user'] => {
      user_id: 'user_id'
    },
    [ChatX::User, 'target_user'] => {
      user_id: 'target_user_id'
    }
  },
  18 => {
    ChatX::Message => {
      time_stamp: 'time_stamp',
      content: 'content',
      room_id: 'room_id',
      user_id: 'user_id',
      message_id: 'message_id'
    }
  }
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event_hash, server, bot) ⇒ Event

Returns a new instance of Event.



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, **opts, &block) ⇒ Object

rubocop:enable Style/GuardClause



176
177
178
179
180
181
182
183
184
185
186
187
188
189
# 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

Instance Attribute Details

#hashObject (readonly)

Returns the value of attribute hash.



138
139
140
# File 'lib/chatx/events.rb', line 138

def hash
  @hash
end

#typeObject (readonly)

Returns the value of attribute type.



138
139
140
# File 'lib/chatx/events.rb', line 138

def type
  @type
end

#type_longObject (readonly)

Returns the value of attribute type_long.



138
139
140
# File 'lib/chatx/events.rb', line 138

def type_long
  @type_long
end

#type_shortObject (readonly)

Returns the value of attribute type_short.



138
139
140
# File 'lib/chatx/events.rb', line 138

def type_short
  @type_short
end

Instance Method Details

#respond_to_missing?(m) ⇒ Boolean

Returns:

  • (Boolean)


191
192
193
# File 'lib/chatx/events.rb', line 191

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