Skip to content

Instantly share code, notes, and snippets.

@practicingruby
Created May 21, 2013 17:12
Show Gist options
  • Select an option

  • Save practicingruby/5621477 to your computer and use it in GitHub Desktop.

Select an option

Save practicingruby/5621477 to your computer and use it in GitHub Desktop.
class BufferedHash
def initialize(head={})
@head = head
@buffer = {}
end
attr_reader :head
def [](key)
@buffer[key] || @head[key]
end
def []=(key, value)
@buffer[key] = value
end
def changed?
not @buffer.empty?
end
def commit
return unless changed?
@head.update(@buffer)
@buffer.clear
end
def rollback
return unless changed?
@buffer.clear
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment