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.
-
#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).
15 16 17 18 |
# File 'lib/tincan/message.rb', line 15 def initialize yield self if block_given? self.published_at ||= DateTime.now 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 |
#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.
31 32 33 34 35 36 37 38 39 |
# File 'lib/tincan/message.rb', line 31 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.
23 24 25 |
# File 'lib/tincan/message.rb', line 23 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
64 65 66 67 68 69 70 |
# File 'lib/tincan/message.rb', line 64 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.
53 54 55 56 57 58 59 |
# File 'lib/tincan/message.rb', line 53 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
72 73 74 75 76 77 |
# File 'lib/tincan/message.rb', line 72 def to_s vars = instance_variables.map do |v| "#{v}=#{instance_variable_get(v).inspect}" end.join(', ') "<#{self.class}: #{vars}>" end |