-
-
Save lenary/c054695204152972239e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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