Class: DripDrop::AutoMessageClass

Inherits:
Message
  • Object
show all
Defined in:
lib/dripdrop/message.rb

Overview

Use of this “metaclass” allows for the automatic recognition of the message’s base class

Class Attribute Summary collapse

Attributes inherited from Message

#body, #head, #name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Message

constantize, decode, decode_json, #encode_json, #encoded, from_hash, #json_encoded, parse, subclasses, #to_hash

Constructor Details

#initialize(*args) ⇒ AutoMessageClass

Returns a new instance of AutoMessageClass.



138
139
140
# File 'lib/dripdrop/message.rb', line 138

def initialize(*args)
  raise "Cannot create an instance of this class - please use create_message class method"
end

Class Attribute Details

.message_subclassesObject

Returns the value of attribute message_subclasses.



143
144
145
# File 'lib/dripdrop/message.rb', line 143

def message_subclasses
  @message_subclasses
end

Class Method Details

.create_message(*args) ⇒ Object



166
167
168
169
# File 'lib/dripdrop/message.rb', line 166

def create_message(*args)
  klass = verify_args(*args)
  klass.create_message(*args)
end

.recreate_message(*args) ⇒ Object



171
172
173
174
# File 'lib/dripdrop/message.rb', line 171

def recreate_message(*args)
  klass = verify_args(*args)
  klass.recreate_message(*args)
end

.register_subclass(klass) ⇒ Object



176
177
178
# File 'lib/dripdrop/message.rb', line 176

def register_subclass(klass)
  DripDrop::AutoMessageClass.message_subclasses[klass.to_s] = klass
end

.verify_args(*args) ⇒ Object

Raises:

  • (ArgumentError)


147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/dripdrop/message.rb', line 147

def verify_args(*args)
  head =
    case args[0]
      when Hash
        az = args[0]
        az[:head] || az['head']
      else
        args[1]
    end
  raise ArgumentError, "Invalid head #{head.inspect}. Head must be a hash! (args: #{args.inspect})" unless head.is_a?(Hash)

  msg_class = head['message_class']
  unless DripDrop::AutoMessageClass.message_subclasses.has_key?(msg_class)
    raise ArgumentError, "Unknown AutoMessage message class #{msg_class}"
  end

  DripDrop::AutoMessageClass.message_subclasses[msg_class]
end