Class: DripDrop::AutoMessageClass
- 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
-
.message_subclasses ⇒ Object
Returns the value of attribute message_subclasses.
Attributes inherited from Message
Class Method Summary collapse
- .create_message(*args) ⇒ Object
- .recreate_message(*args) ⇒ Object
- .register_subclass(klass) ⇒ Object
- .verify_args(*args) ⇒ Object
Instance Method Summary collapse
-
#initialize(*args) ⇒ AutoMessageClass
constructor
A new instance of AutoMessageClass.
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_subclasses ⇒ Object
Returns the value of attribute message_subclasses.
143 144 145 |
# File 'lib/dripdrop/message.rb', line 143 def @message_subclasses end |
Class Method Details
.create_message(*args) ⇒ Object
166 167 168 169 |
# File 'lib/dripdrop/message.rb', line 166 def (*args) klass = verify_args(*args) klass.(*args) end |
.recreate_message(*args) ⇒ Object
171 172 173 174 |
# File 'lib/dripdrop/message.rb', line 171 def (*args) klass = verify_args(*args) klass.(*args) end |
.register_subclass(klass) ⇒ Object
176 177 178 |
# File 'lib/dripdrop/message.rb', line 176 def register_subclass(klass) DripDrop::AutoMessageClass.[klass.to_s] = klass end |
.verify_args(*args) ⇒ Object
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..has_key?(msg_class) raise ArgumentError, "Unknown AutoMessage message class #{msg_class}" end DripDrop::AutoMessageClass.[msg_class] end |