Skip to content

Instantly share code, notes, and snippets.

@lenary
Last active December 25, 2015 17:29
Show Gist options
  • Save lenary/c054695204152972239e to your computer and use it in GitHub Desktop.
Save lenary/c054695204152972239e to your computer and use it in GitHub Desktop.
class Counter
# class method:
def self.increment(bucket, key, by=1)
# do something here, *without* fetching
end
attr_accessor :bucket
attr_accessor :key
def initialize(bucket, key)
@bucket = bucket
@key = key
@ops = []
@value = request_value
end
attr_reader :value
def increment(by)
@ops.push([:increment, by])
value += by
end
def send_operations!
encode_and_send(@ops)
@ops = []
@value = request_value
end
def recache_value
@value = request_value
apply_unsent_ops_to_value
end
def dirty?
@ops.present?
end
def request_value
# lol todo
end
def apply_unsent_ops_to_value
# lol todo
end
def encode_and_send
# lol tod
end
end
# counter
counter = Counter.new("test", "test")
counter.value # => 5
# do some stuff client side
counter.increment(5)
counter.value # => 10
counter.dirty? # => true
# actually send the changes to the client side.
counter.send_operations!
counter.value # => 10
counter.dirty? # => false
# But without a fetch:
Counter.increment("test", "test")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment