Class: Parse::LiveQuery::Event
- Inherits:
-
Object
- Object
- Parse::LiveQuery::Event
- Defined in:
- lib/parse/live_query/event.rb
Overview
Represents an event received from the LiveQuery server. Events are emitted when objects matching a subscription’s query are created, updated, deleted, or enter/leave the query results.
Instance Attribute Summary collapse
-
#class_name ⇒ String
readonly
The Parse class name.
-
#object ⇒ Parse::Object
readonly
The object affected by this event (current state).
-
#original ⇒ Parse::Object?
readonly
The original state of the object (for :update, :enter, :leave).
-
#raw ⇒ Hash
readonly
Raw payload from the server.
-
#received_at ⇒ Time
readonly
When the event was received.
-
#request_id ⇒ Integer
readonly
The subscription request ID this event belongs to.
-
#type ⇒ Symbol
readonly
The type of event (:create, :update, :delete, :enter, :leave).
Instance Method Summary collapse
-
#create? ⇒ Boolean
True if this is a create event.
-
#delete? ⇒ Boolean
True if this is a delete event.
-
#enter? ⇒ Boolean
True if this is an enter event (object now matches query).
-
#initialize(type:, class_name:, object_data:, original_data: nil, request_id:, raw: {}) ⇒ Event
constructor
Create a new Event from a LiveQuery server message.
-
#leave? ⇒ Boolean
True if this is a leave event (object no longer matches query).
-
#parse_object_id ⇒ String
The Parse object ID.
-
#to_h ⇒ Hash
Event as a hash.
-
#update? ⇒ Boolean
True if this is an update event.
Constructor Details
#initialize(type:, class_name:, object_data:, original_data: nil, request_id:, raw: {}) ⇒ Event
Create a new Event from a LiveQuery server message
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/parse/live_query/event.rb', line 46 def initialize(type:, class_name:, object_data:, original_data: nil, request_id:, raw: {}) @type = type.to_sym @class_name = class_name @request_id = request_id @received_at = Time.now @raw = raw # Convert object data to Parse::Object instances @object = build_object(class_name, object_data) if object_data @original = build_object(class_name, original_data) if original_data end |
Instance Attribute Details
#class_name ⇒ String (readonly)
Returns the Parse class name.
31 32 33 |
# File 'lib/parse/live_query/event.rb', line 31 def class_name @class_name end |
#object ⇒ Parse::Object (readonly)
Returns the object affected by this event (current state).
22 23 24 |
# File 'lib/parse/live_query/event.rb', line 22 def object @object end |
#original ⇒ Parse::Object? (readonly)
Returns the original state of the object (for :update, :enter, :leave).
25 26 27 |
# File 'lib/parse/live_query/event.rb', line 25 def original @original end |
#raw ⇒ Hash (readonly)
Returns raw payload from the server.
37 38 39 |
# File 'lib/parse/live_query/event.rb', line 37 def raw @raw end |
#received_at ⇒ Time (readonly)
Returns when the event was received.
34 35 36 |
# File 'lib/parse/live_query/event.rb', line 34 def received_at @received_at end |
#request_id ⇒ Integer (readonly)
Returns the subscription request ID this event belongs to.
28 29 30 |
# File 'lib/parse/live_query/event.rb', line 28 def request_id @request_id end |
#type ⇒ Symbol (readonly)
Returns the type of event (:create, :update, :delete, :enter, :leave).
19 20 21 |
# File 'lib/parse/live_query/event.rb', line 19 def type @type end |
Instance Method Details
#create? ⇒ Boolean
Returns true if this is a create event.
59 60 61 |
# File 'lib/parse/live_query/event.rb', line 59 def create? type == :create end |
#delete? ⇒ Boolean
Returns true if this is a delete event.
69 70 71 |
# File 'lib/parse/live_query/event.rb', line 69 def delete? type == :delete end |
#enter? ⇒ Boolean
Returns true if this is an enter event (object now matches query).
74 75 76 |
# File 'lib/parse/live_query/event.rb', line 74 def enter? type == :enter end |
#leave? ⇒ Boolean
Returns true if this is a leave event (object no longer matches query).
79 80 81 |
# File 'lib/parse/live_query/event.rb', line 79 def leave? type == :leave end |
#parse_object_id ⇒ String
Returns the Parse object ID.
84 85 86 |
# File 'lib/parse/live_query/event.rb', line 84 def parse_object_id object&.id end |
#to_h ⇒ Hash
Returns event as a hash.
89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/parse/live_query/event.rb', line 89 def to_h { type: type, class_name: class_name, object_id: parse_object_id, request_id: request_id, received_at: received_at, object: object&.as_json, original: original&.as_json, } end |
#update? ⇒ Boolean
Returns true if this is an update event.
64 65 66 |
# File 'lib/parse/live_query/event.rb', line 64 def update? type == :update end |