Class: Oplogjam::Delete

Inherits:
Object
  • Object
show all
Defined in:
lib/oplogjam/delete.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(h, ts, ns, o) ⇒ Delete

Returns a new instance of Delete.



18
19
20
21
22
23
# File 'lib/oplogjam/delete.rb', line 18

def initialize(h, ts, ns, o)
  @h = Integer(h)
  @ts = Oplogjam::Timestamp(ts)
  @ns = String(ns)
  @o = Oplogjam::Document(o)
end

Instance Attribute Details

#hObject (readonly) Also known as: id

Returns the value of attribute h.



5
6
7
# File 'lib/oplogjam/delete.rb', line 5

def h
  @h
end

#nsObject (readonly) Also known as: namespace

Returns the value of attribute ns.



5
6
7
# File 'lib/oplogjam/delete.rb', line 5

def ns
  @ns
end

#oObject (readonly) Also known as: query

Returns the value of attribute o.



5
6
7
# File 'lib/oplogjam/delete.rb', line 5

def o
  @o
end

#tsObject (readonly)

Returns the value of attribute ts.



5
6
7
# File 'lib/oplogjam/delete.rb', line 5

def ts
  @ts
end

Class Method Details

.from(bson) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'lib/oplogjam/delete.rb', line 7

def self.from(bson)
  h = bson.fetch(H)
  ts = bson.fetch(TS)
  ns = bson.fetch(NS)
  o = bson.fetch(O)

  new(h, ts, ns, o)
rescue KeyError => e
  raise InvalidDelete, "missing field: #{e}"
end

Instance Method Details

#==(other) ⇒ Object



33
34
35
36
37
# File 'lib/oplogjam/delete.rb', line 33

def ==(other)
  return false unless other.is_a?(Delete)

  id == other.id
end

#apply(mapping) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/oplogjam/delete.rb', line 39

def apply(mapping)
  table = mapping[namespace]
  return unless table

  row_id = query.fetch(ID).to_json

  table
    .where(id: row_id, deleted_at: nil)
    .update(updated_at: Time.now.utc, deleted_at: Time.now.utc)
end

#timestampObject



29
30
31
# File 'lib/oplogjam/delete.rb', line 29

def timestamp
  Time.at(ts.seconds, ts.increment)
end