Class: MemoryIO::Types::Type
- Inherits:
-
Object
- Object
- MemoryIO::Types::Type
- Defined in:
- lib/memory_io/types/type.rb
Overview
The base class, all descendants of this class would be consider as a valid ‘type’.
Direct Known Subclasses
Constant Summary collapse
- SIZE_T =
The size of
size_t
. i.e. sizeof(size_t). 8
Class Method Summary collapse
-
.find(symbol) ⇒ {Symbol => Object}
private
Find the subclass of Type by symbol.
-
.keep_pos(stream, pos: nil) {|stream| ... } ⇒ Object
Yield a block and resume the position of stream.
- .read(_stream) ⇒ Object abstract
-
.read_size_t(stream) ⇒ Integer
Read SIZE_T bytes and cast to a little endian unsigned integer.
-
.register(object, option = {}) ⇒ Array<Symbol>
Register a new type.
- .write(_stream, _obj) ⇒ Object abstract
-
.write_size_t(stream, val) ⇒ void
Pack
val
into SIZE_T bytes and write tostream
.
Class Method Details
.find(symbol) ⇒ {Symbol => Object}
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Find the subclass of MemoryIO::Types::Type by symbol.
90 91 92 |
# File 'lib/memory_io/types/type.rb', line 90 def find(symbol) @map[symbol] end |
.keep_pos(stream, pos: nil) {|stream| ... } ⇒ Object
Yield a block and resume the position of stream.
71 72 73 74 75 76 77 |
# File 'lib/memory_io/types/type.rb', line 71 def keep_pos(stream, pos: nil) org = stream.pos stream.pos = pos if pos ret = yield stream stream.pos = org ret end |
.read(_stream) ⇒ Object
142 143 |
# File 'lib/memory_io/types/type.rb', line 142 def read(_stream) raise NotImplementedError end |
.read_size_t(stream) ⇒ Integer
Read SIZE_T bytes and cast to a little endian unsigned integer.
28 29 30 |
# File 'lib/memory_io/types/type.rb', line 28 def read_size_t(stream) MemoryIO::Util.unpack(stream.read(SIZE_T)) end |
.register(object, option = {}) ⇒ Array<Symbol>
If all symbols in alias
have been registered, an ArgumentError will be raised. However, if at least one of aliases hasn’t been used, registration will success.
Register a new type.
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/memory_io/types/type.rb', line 125 def register(object, option = {}) @map ||= OpenStruct.new aliases = Array(option[:alias]) reg_fail = ArgumentError.new(<<-EOS.strip) Register '#{object.inspect}' fails because another object with same name has been registered. Specify an alias such as `register(MyClass, alias: :custom_alias_name)`. EOS raise reg_fail if aliases.any? && aliases.all? { |ali| @map[ali] } keys = get_keys(object).concat(aliases).uniq.reject { |k| @map[k] } raise reg_fail if keys.empty? rec = MemoryIO::Types::Record.new(object, keys, option) keys.each { |k| @map[k] = rec } end |
.write(_stream, _obj) ⇒ Object
146 147 |
# File 'lib/memory_io/types/type.rb', line 146 def write(_stream, _obj) raise NotImplementedError end |