Class: Tincan::Message
- Inherits:
-
Object
- Object
- Tincan::Message
- Defined in:
- lib/tincan/message.rb
Overview
Encapsulates a message published to (and received from) a Redis “tincan” queue.
Instance Attribute Summary collapse
-
#change_type ⇒ Object
Returns the value of attribute change_type.
-
#message_id ⇒ Object
Returns the value of attribute message_id.
-
#object_data ⇒ Object
Returns the value of attribute object_data.
-
#object_name ⇒ Object
Returns the value of attribute object_name.
-
#published_at ⇒ Object
Returns the value of attribute published_at.
Class Method Summary collapse
-
.from_hash(hash) ⇒ Tincan::Message
Assigns keys and values to this object based on an already-deserialized JSON object.
-
.from_json(json) ⇒ Tincan::Message
Deserializes an object from a JSON string.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Overrides equality operator to determine if all ivars are equal.
-
#initialize {|_self| ... } ⇒ Tincan::Message
constructor
Creates a new instance of a notification with an object (usually an ActiveModel instance).
-
#to_json(options = {}) ⇒ String
Generates a version of this notification as a JSON string.
- #to_s ⇒ Object
Constructor Details
#initialize {|_self| ... } ⇒ Tincan::Message
Creates a new instance of a notification with an object (usually an ActiveModel instance).
16 17 18 19 20 |
# File 'lib/tincan/message.rb', line 16 def initialize yield self if block_given? self.published_at ||= DateTime.now self. ||= SecureRandom.uuid end |
Instance Attribute Details
#change_type ⇒ Object
Returns the value of attribute change_type.
9 10 11 |
# File 'lib/tincan/message.rb', line 9 def change_type @change_type end |
#message_id ⇒ Object
Returns the value of attribute message_id.
9 10 11 |
# File 'lib/tincan/message.rb', line 9 def @message_id end |
#object_data ⇒ Object
Returns the value of attribute object_data.
9 10 11 |
# File 'lib/tincan/message.rb', line 9 def object_data @object_data end |
#object_name ⇒ Object
Returns the value of attribute object_name.
9 10 11 |
# File 'lib/tincan/message.rb', line 9 def object_name @object_name end |
#published_at ⇒ Object
Returns the value of attribute published_at.
9 10 11 |
# File 'lib/tincan/message.rb', line 9 def published_at @published_at end |
Class Method Details
.from_hash(hash) ⇒ Tincan::Message
Assigns keys and values to this object based on an already-deserialized JSON object.
33 34 35 36 37 38 39 40 41 |
# File 'lib/tincan/message.rb', line 33 def self.from_hash(hash) instance = new do |i| hash.each do |key, val| val = DateTime.iso8601(val) if key == 'published_at' i.send("#{key}=".to_sym, val) end end instance end |
.from_json(json) ⇒ Tincan::Message
Deserializes an object from a JSON string.
25 26 27 |
# File 'lib/tincan/message.rb', line 25 def self.from_json(json) from_hash(JSON.parse(json)) end |
Instance Method Details
#==(other) ⇒ Object
Overrides equality operator to determine if all ivars are equal
66 67 68 69 70 71 72 |
# File 'lib/tincan/message.rb', line 66 def ==(other) false unless other checks = %i(object_name change_type object_data published_at).map do |p| other.send(p) == send(p) end checks.all? end |
#to_json(options = {}) ⇒ String
Generates a version of this notification as a JSON string.
55 56 57 58 59 60 61 |
# File 'lib/tincan/message.rb', line 55 def to_json( = {}) # Note: at some point I may want to override how this is done. I think # Rabl could definitely be of some use here. Hash[%i(object_name change_type object_data published_at).map do |name| [name, send(name)] end].to_json() end |
#to_s ⇒ Object
74 75 76 77 78 79 |
# File 'lib/tincan/message.rb', line 74 def to_s vars = instance_variables.map do |v| "#{v}=#{instance_variable_get(v).inspect}" end.join(', ') "<#{self.class}: #{vars}>" end |