Class: Changebase::Inline::Transaction
- Inherits:
-
Object
- Object
- Changebase::Inline::Transaction
- Defined in:
- lib/changebase/inline/transaction.rb
Instance Attribute Summary collapse
-
#events ⇒ Object
Returns the value of attribute events.
-
#id ⇒ Object
Returns the value of attribute id.
-
#metadata ⇒ Object
Returns the value of attribute metadata.
-
#timestamp ⇒ Object
Returns the value of attribute timestamp.
Class Method Summary collapse
Instance Method Summary collapse
- #_create ⇒ Object
- #_update ⇒ Object
- #as_json ⇒ Object
- #event!(event_attributes) ⇒ Object
-
#initialize(attrs = {}) ⇒ Transaction
constructor
A new instance of Transaction.
- #persisted? ⇒ Boolean
- #save! ⇒ Object
Constructor Details
#initialize(attrs = {}) ⇒ Transaction
Returns a new instance of Transaction.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/changebase/inline/transaction.rb', line 7 def initialize(attrs={}) attrs.each { |k,v| self.send("#{k}=", v) } if id @persisted = true else @persisted = false @id ||= SecureRandom.uuid end @events ||= [] @timestamp ||= Time.now @metadata ||= {} end |
Instance Attribute Details
#events ⇒ Object
Returns the value of attribute events.
5 6 7 |
# File 'lib/changebase/inline/transaction.rb', line 5 def events @events end |
#id ⇒ Object
Returns the value of attribute id.
5 6 7 |
# File 'lib/changebase/inline/transaction.rb', line 5 def id @id end |
#metadata ⇒ Object
Returns the value of attribute metadata.
5 6 7 |
# File 'lib/changebase/inline/transaction.rb', line 5 def @metadata end |
#timestamp ⇒ Object
Returns the value of attribute timestamp.
5 6 7 |
# File 'lib/changebase/inline/transaction.rb', line 5 def @timestamp end |
Class Method Details
.create!(attrs = {}) ⇒ Object
32 33 34 35 36 |
# File 'lib/changebase/inline/transaction.rb', line 32 def self.create!(attrs={}) transaction = self.new(attrs) transaction.save! transaction end |
Instance Method Details
#_create ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'lib/changebase/inline/transaction.rb', line 51 def _create events.delete_if { |a| a.columns.empty? } payload = JSON.generate({transaction: self.as_json}) Changebase.logger.debug("[Changebase] POST /transactions WITH #{payload}") Changebase.connection.post('/transactions', payload) @events = [] @persisted = true end |
#_update ⇒ Object
42 43 44 45 46 47 48 49 |
# File 'lib/changebase/inline/transaction.rb', line 42 def _update return if events.empty? events.delete_if { |a| a.diff.empty? } payload = JSON.generate({events: events.as_json.map{ |json| json[:transaction_id] = id; json }}) Changebase.logger.debug("[Changebase] POST /events WITH #{payload}") Changebase.connection.post('/events', payload) @events = [] end |
#as_json ⇒ Object
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/changebase/inline/transaction.rb', line 60 def as_json result = { id: id, lsn: .utc.iso8601(3), timestamp: .utc.iso8601(3), events: events.as_json } result[:metadata] = .as_json if !.empty? result end |
#event!(event_attributes) ⇒ Object
26 27 28 29 30 |
# File 'lib/changebase/inline/transaction.rb', line 26 def event!(event_attributes) event = Changebase::Inline::Event.new(event_attributes) @events << event event end |
#persisted? ⇒ Boolean
22 23 24 |
# File 'lib/changebase/inline/transaction.rb', line 22 def persisted? @persisted end |
#save! ⇒ Object
38 39 40 |
# File 'lib/changebase/inline/transaction.rb', line 38 def save! persisted? ? _update : _create end |