Class: Tincan::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/tincan/message.rb

Overview

Encapsulates a message published to (and received from) a Redis “tincan” queue.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Tincan::Message

Creates a new instance of a notification with an object (usually an ActiveModel instance).

Parameters:

  • Takes (Block)

    an optional block to configure this instance.

Yields:

  • (_self)

Yield Parameters:



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.message_id ||= SecureRandom.uuid
end

Instance Attribute Details

#change_typeObject

Returns the value of attribute change_type.



9
10
11
# File 'lib/tincan/message.rb', line 9

def change_type
  @change_type
end

#message_idObject

Returns the value of attribute message_id.



9
10
11
# File 'lib/tincan/message.rb', line 9

def message_id
  @message_id
end

#object_dataObject

Returns the value of attribute object_data.



9
10
11
# File 'lib/tincan/message.rb', line 9

def object_data
  @object_data
end

#object_nameObject

Returns the value of attribute object_name.



9
10
11
# File 'lib/tincan/message.rb', line 9

def object_name
  @object_name
end

#published_atObject

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.

Parameters:

  • hash (Hash)

    A hash of properties and their values.

Returns:



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.

Parameters:

  • json (String)

    A JSON string to be decoded.

Returns:



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.

Returns:

  • (String)

    A JSON-compliant marshalling of this instance’s data.



55
56
57
58
59
60
61
# File 'lib/tincan/message.rb', line 55

def to_json(options = {})
  # 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(options)
end

#to_sObject



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