Class: Oplogjam::Update

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(h, ts, ns, o2, o) ⇒ Update

Returns a new instance of Update.



21
22
23
24
25
26
27
# File 'lib/oplogjam/update.rb', line 21

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

Instance Attribute Details

#hObject (readonly) Also known as: id

Returns the value of attribute h.



7
8
9
# File 'lib/oplogjam/update.rb', line 7

def h
  @h
end

#nsObject (readonly) Also known as: namespace

Returns the value of attribute ns.



7
8
9
# File 'lib/oplogjam/update.rb', line 7

def ns
  @ns
end

#oObject (readonly) Also known as: update

Returns the value of attribute o.



7
8
9
# File 'lib/oplogjam/update.rb', line 7

def o
  @o
end

#o2Object (readonly) Also known as: query

Returns the value of attribute o2.



7
8
9
# File 'lib/oplogjam/update.rb', line 7

def o2
  @o2
end

#tsObject (readonly)

Returns the value of attribute ts.



7
8
9
# File 'lib/oplogjam/update.rb', line 7

def ts
  @ts
end

Class Method Details

.from(bson) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/oplogjam/update.rb', line 9

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

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

Instance Method Details

#==(other) ⇒ Object



38
39
40
41
42
# File 'lib/oplogjam/update.rb', line 38

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

  id == other.id
end

#apply(mapping) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/oplogjam/update.rb', line 44

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(document: jsonb_update, updated_at: Time.now.utc)
end

#timestampObject



34
35
36
# File 'lib/oplogjam/update.rb', line 34

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