Created
August 27, 2019 09:57
-
-
Save OrelSokolov/94e2ff34e2c9cd8c6c0bb42835dba8d8 to your computer and use it in GitHub Desktop.
Abstract buffer
This file contains hidden or 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 Buffer | |
| attr_reader :buffer, :overflows | |
| def initialize(&block) | |
| @limitation_criteria = block | |
| @buffer = [] | |
| @overflows = 0 | |
| end | |
| def set_action(&block) | |
| @action = block | |
| end | |
| def clean_with_action! | |
| @action.call @buffer | |
| @buffer = [] | |
| end | |
| def push(data) | |
| @buffer.push data | |
| if @limitation_criteria.call(@buffer) | |
| @overflows += 1 | |
| clean_with_action! | |
| end | |
| end | |
| end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment