Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Addressing your first question. Here's some ideas you might like to consider</p> <p>First, structure you sub-classes so they all use the same initiation parameters. Also, you could put some of the other initiating code there as well (such as your encoded_body and server accessors. Here's a skeleton of what I mean:</p> <pre><code># SOChat Class skeleton structure class SOChatSubClass #&lt; inherit from whatever parent class is appropriate attr_accessor :encoded_body, :server, :from, :to, :body def initialize(event, room, server) @encoded_body = event['content'] @server = server SOChatEvent.events.push event #class specific code xmpp_message = event['message'] @from = xmpp_message.from @to = xmpp_message.to @body = xmpp_message.body #use super to call parent class initialization methods and to DRY up your code end end </code></pre> <p>Note that in my example you'll still have duplicated code in the sub-classes. Ideally you'd pull out the duplication by putting it in the appropriate parent class.</p> <p>If you have problems creating a common list of initiation parameters, then rather than pass in a list of arguments (event, room, server), change the classes to accept an argument list as a hash {:event => event, :room => room, :server => server, etc}.</p> <p>Regardless, once you have a common parameter structure for initializing the classes, you can initialize them a bit more dynamically, eliminating the need for the case statement.</p> <pre><code>class SOChatEvent class &lt;&lt; self; attr_accessor :events; end @events = [] @@event_parser = { 0 =&gt; SOChatSubClass, #hypothetical example for testing 1 =&gt; SOChatMessage, 2 =&gt; SOChatMessageEdit, #etc } def self.create_from_evt( json_event_data, room=nil, server=nil) event_type = json_event_data["event_type"] event_class = @@event_parser[event_type] #this creates the class defined by class returned in the @@event_parser hash event_obj = event_class.new(json_event_data, room, server) end #rest of class end </code></pre> <p><code>@@event_parser</code> contains the mapping between event type and the class to implement that event type. You just assign the appropriate class to a variable and treat it just like the actual class.</p> <p>Code like the following would create an object of the appropriate class:</p> <pre><code>event_obj = SOChatEvent.create_from_evt( json_event_data, "some room", "some server") </code></pre> <p>Note: There are further optimizations that could be done to what I provided to be even cleaner and more concise, but hopefully this helps you get over the hump of the case statement.</p> <p>Edit: I forgot to mention the Class instance variable <code>SOChatEvent.events</code> created with this: <code>class &lt;&lt; self; attr_accessor :events; end @events = []</code></p> <p>You were pushing events to an event stack, but I wasn't clear where you wanted that stack to exist and whether it was a global events list, or specific to a particular class. The one I did is global, so feel free to change it if you wanted the event stack constrained to certain classes or instances.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload