Class: Messaging::Adapters::Postgres::SerializedMessage
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Messaging::Adapters::Postgres::SerializedMessage
- Defined in:
- lib/messaging/adapters/postgres/serialized_message.rb
Instance Attribute Summary collapse
-
#expected_version ⇒ Object
Returns the value of attribute expected_version.
Class Method Summary collapse
-
.instantiate(attributes, *_args) ⇒ Object
We override this AR method to make records retreived from scopes etc.
Instance Method Summary collapse
- #create_or_update(*args) ⇒ Object
-
#message=(message) ⇒ Object
Virtual setter for message so we can create a serialized message from a message.
-
#readonly? ⇒ Boolean
You should never update a message after creating it.
- #stream ⇒ Object
- #to_message ⇒ Object
Instance Attribute Details
#expected_version ⇒ Object
Returns the value of attribute expected_version.
7 8 9 |
# File 'lib/messaging/adapters/postgres/serialized_message.rb', line 7 def expected_version @expected_version end |
Class Method Details
.instantiate(attributes, *_args) ⇒ Object
We override this AR method to make records retreived from scopes etc. be message objects of the corresponding message class instead of AR objects
See api.rubyonrails.org/classes/ActiveRecord/Persistence/ClassMethods.html#method-i-instantiate
14 15 16 17 18 |
# File 'lib/messaging/adapters/postgres/serialized_message.rb', line 14 def self.instantiate(attributes, *_args) attributes['message_type'].constantize.new( JSON.parse(attributes['data']).merge(stream_position: attributes['stream_position']) ) end |
Instance Method Details
#create_or_update(*args) ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/messaging/adapters/postgres/serialized_message.rb', line 44 def create_or_update(*args) with_locked_stream do set_stream_position validate_expected_version! super end end |
#message=(message) ⇒ Object
Virtual setter for message so we can create a serialized message from a message
21 22 23 24 25 26 27 28 29 |
# File 'lib/messaging/adapters/postgres/serialized_message.rb', line 21 def () self.data = .attributes_as_json self.expected_version = .expected_version self. = . self.stream = .stream_name self.stream_category = .stream_category self.stream_id = .stream_id self.uuid = .uuid end |
#readonly? ⇒ Boolean
You should never update a message after creating it
36 37 38 |
# File 'lib/messaging/adapters/postgres/serialized_message.rb', line 36 def readonly? true unless new_record? end |
#stream ⇒ Object
40 41 42 |
# File 'lib/messaging/adapters/postgres/serialized_message.rb', line 40 def stream @stream ||= Stream.new(attributes['stream']) end |
#to_message ⇒ Object
31 32 33 |
# File 'lib/messaging/adapters/postgres/serialized_message.rb', line 31 def .constantize.new(data.merge(stream_position: stream_position)) end |