Class: Meangirls::Counter

Inherits:
CRDT
  • Object
show all
Defined in:
lib/meangirls/counter.rb

Direct Known Subclasses

GCounter

Instance Method Summary collapse

Methods inherited from CRDT

merge, #to_json

Instance Method Details

#+(delta) ⇒ Object

Returns a copy of this counter, with the local Meangirls node’s value incremented by delta.



22
23
24
# File 'lib/meangirls/counter.rb', line 22

def +(delta)
  clone.increment(Meangirls.node, delta)
end

#-(delta) ⇒ Object



26
27
28
# File 'lib/meangirls/counter.rb', line 26

def -(delta)
  clone.increment(Meangirls.node, -1 * delta)
end

#===(other) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/meangirls/counter.rb', line 4

def ===(other)
  # Can only compare to numerics
  unless other.kind_of? Meangirls::Counter or
    other.kind_of? Numeric
    return false
  end

  # TODO: This gets awkward when we exceed FP integer precision in aggregate
  # over actors.
  if float?
    self.to_f == other.to_f
  else
    self.to_i == other.to_i
  end
end

#to_iObject



30
31
32
# File 'lib/meangirls/counter.rb', line 30

def to_i
  to_f.to_i
end