Module: Vigilem::Core::Buffer Abstract
- Included in:
- DefaultBuffer
- Defined in:
- lib/vigilem/core/buffer.rb
Overview
This module is abstract.
minimum compliance #concat, #slice!, #peek buffers are typically String or Array
Class Method Summary collapse
Instance Method Summary collapse
- #concat(other_buffer) ⇒ Object
- #deep_dup ⇒ Buffer
- #empty? ⇒ TrueClass || FalseClass
-
#offset(len) ⇒ Array<Object, Integer>
[obj #slice!ed, remainder of len].
-
#offset!(len) ⇒ Array<Object, Integer>
[obj #slice!ed, remainder of len].
- #peek ⇒ Object
- #pop(num = nil) ⇒ Object
- #shift(n = nil) ⇒ Object
- #slice(start_idx_range, len = nil) ⇒ Object
-
#slice!(start_idx_range, len = nil) ⇒ Object
used because it’s available on both String and Array maybe drop this in favor of shift…
Class Method Details
.new(type = []) ⇒ DefaultBuffer
113 114 115 116 |
# File 'lib/vigilem/core/buffer.rb', line 113 def self.new(type=[]) require 'vigilem/core/default_buffer' DefaultBuffer.new(type) end |
Instance Method Details
#concat(other_buffer) ⇒ Object
57 58 59 |
# File 'lib/vigilem/core/buffer.rb', line 57 def concat(other_buffer) _super_check_fail_on_error(__method__, nil, other_buffer) end |
#deep_dup ⇒ Buffer
102 103 104 105 106 107 108 |
# File 'lib/vigilem/core/buffer.rb', line 102 def deep_dup _super_check(__method__) do Support::Utils.deep_dup(self) end rescue TypeError raise "#{self} does not respond_to? :slice, :deep_dup, :_dump and no :_dump_data is defined for class #{self.class}" end |
#empty? ⇒ TrueClass || FalseClass
89 90 91 92 93 94 95 96 97 |
# File 'lib/vigilem/core/buffer.rb', line 89 def empty? _super_check(__method__) do begin !!peek rescue StopIteration false end end end |
#offset(len) ⇒ Array<Object, Integer>
Returns [obj #slice!ed, remainder of len].
71 72 73 |
# File 'lib/vigilem/core/buffer.rb', line 71 def offset(len) Support::Utils.offset(self, len) end |
#offset!(len) ⇒ Array<Object, Integer>
Returns [obj #slice!ed, remainder of len].
64 65 66 |
# File 'lib/vigilem/core/buffer.rb', line 64 def offset!(len) Support::Utils.offset!(self, len) end |
#peek ⇒ Object
TODO:
memoization
44 45 46 47 48 49 50 51 52 |
# File 'lib/vigilem/core/buffer.rb', line 44 def peek _super_check(__method__) do if (enum = respond(:each)).respond_to? :peek enum.peek else _super_check_fail_on_error(__method__) end end end |
#pop(num = nil) ⇒ Object
79 80 81 82 83 84 85 |
# File 'lib/vigilem/core/buffer.rb', line 79 def pop(num=nil) _super_check((method_name = __method__), nil, *num) do raise ArgumentError, 'negative array size' if num < 0 ret = slice!(-num, num) if num.nil? then slice!(-num, num) else Support::Utils.unwrap_ary(ret) end end end |
#shift(n = nil) ⇒ Object
35 36 37 38 39 |
# File 'lib/vigilem/core/buffer.rb', line 35 def shift(n=nil) _super_check(__method__, nil, *n) do slice!(0, *n) end end |
#slice(start_idx_range, len = nil) ⇒ Object
17 18 19 20 21 |
# File 'lib/vigilem/core/buffer.rb', line 17 def slice(start_idx_range, len=nil) _super_check((method_name = __method__), nil, *[start_idx_range, *len]) do self.deep_dup.slice!(start_idx_range, len) end end |
#slice!(start_idx_range, len = nil) ⇒ Object
used because it’s available on both String and Array maybe drop this in favor of shift…
29 30 31 |
# File 'lib/vigilem/core/buffer.rb', line 29 def slice!(start_idx_range, len=nil) _super_check_fail_on_error(__method__, nil, *[start_idx_range, *len]) end |