Class: Changebase::Inline::Transaction

Inherits:
Object
  • Object
show all
Defined in:
lib/changebase/inline/transaction.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#eventsObject

Returns the value of attribute events.



5
6
7
# File 'lib/changebase/inline/transaction.rb', line 5

def events
  @events
end

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/changebase/inline/transaction.rb', line 5

def id
  @id
end

#metadataObject

Returns the value of attribute metadata.



5
6
7
# File 'lib/changebase/inline/transaction.rb', line 5

def 
  @metadata
end

#timestampObject

Returns the value of attribute timestamp.



5
6
7
# File 'lib/changebase/inline/transaction.rb', line 5

def timestamp
  @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

#_createObject



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

#_updateObject



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_jsonObject



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:                  timestamp.utc.iso8601(3),
    timestamp:            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

Returns:

  • (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