Module: Bytemapper
- Defined in:
- lib/bytemapper.rb,
lib/bytemapper/type.rb,
lib/bytemapper/chunk.rb,
lib/bytemapper/shape.rb,
lib/bytemapper/table.rb,
lib/bytemapper/nameable.rb,
lib/bytemapper/registry.rb,
lib/bytemapper/typeable.rb,
lib/bytemapper/flattenable.rb
Overview
Bytemapper - Model arbitrary bytestrings as Ruby objects.
Copyright © 2020 Jefferson Hudson
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License along with this program. If not, see <www.gnu.org/licenses/>.
Defined Under Namespace
Modules: Flattenable, Nameable, Typeable
Classes: Chunk, Registry, Shape, Table, Type
Constant Summary
collapse
- @@registry =
Registry.new
Class Method Summary
collapse
Class Method Details
.get(obj) ⇒ Object
94
95
96
|
# File 'lib/bytemapper.rb', line 94
def get(obj)
registry.get(obj)
end
|
.is_a_name?(obj) ⇒ Boolean
69
70
71
|
# File 'lib/bytemapper.rb', line 69
def is_a_name?(obj)
obj.is_a?(String) || obj.is_a?(Symbol)
end
|
.is_a_shape?(obj) ⇒ Boolean
65
66
67
|
# File 'lib/bytemapper.rb', line 65
def is_a_shape?(obj)
obj.is_a?(Hash)
end
|
.is_a_type?(obj) ⇒ Boolean
57
58
59
60
61
62
63
|
# File 'lib/bytemapper.rb', line 57
def is_a_type?(obj)
obj.is_a?(Type) ||
obj.is_a?(Array) &&
obj.size == 2 &&
obj.first.is_a?(Integer) &&
obj.last.is_a?(String)
end
|
.map(bytes, shape, name = nil) ⇒ Object
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/bytemapper.rb', line 73
def map(bytes, shape, name = nil)
bytes.force_encoding(Encoding::ASCII_8BIT)
bytes = StringIO.new(bytes)
if shape.is_a?(Array)
chunks = []
shape.each { |s| chunks << Chunk.new(bytes.read(s.size), s, name) }
chunks
else
shape = wrap(shape, name)
Chunk.new(bytes, shape, name)
end
end
|
.names(filter_key = nil) ⇒ Object
102
103
104
|
# File 'lib/bytemapper.rb', line 102
def names(filter_key = nil)
registry.names.keys
end
|
.print ⇒ Object
106
107
108
|
# File 'lib/bytemapper.rb', line 106
def print
registry.print
end
|
.put(obj, name) ⇒ Object
98
99
100
|
# File 'lib/bytemapper.rb', line 98
def put(obj, name)
registry.put(obj, name)
end
|
.register(obj, name, fqname = []) ⇒ Object
Also known as:
wrap
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/bytemapper.rb', line 30
def register(obj, name, fqname = [])
return if obj.nil?
name = name.downcase.to_sym unless name.nil?
fqname << name unless name.nil?
if is_a_type?(obj)
name = fqname.size > 1 ? fqname.join('.') : fqname.first
obj = Type.new(obj)
put(obj, name)
elsif is_a_name?(obj)
register(get(obj), nil, fqname)
elsif is_a_shape?(obj)
if registered?(obj)
obj = get(obj)
put(obj, name)
else
shape = Shape.new
obj.each do |k,v|
shape[k] = register(v, k, [].concat(fqname))
end
put(shape, name)
end
else
put(obj, name)
end
end
|
.registered?(obj) ⇒ Boolean
90
91
92
|
# File 'lib/bytemapper.rb', line 90
def registered?(obj)
registry.registered?(obj)
end
|
.registry ⇒ Object
110
111
112
|
# File 'lib/bytemapper.rb', line 110
def registry
@@registry
end
|
.repeat(obj, times = nil) ⇒ Object
86
87
88
|
# File 'lib/bytemapper.rb', line 86
def repeat(obj, times = nil)
Table.new(obj, times)
end
|
.reset(with_basic_types = true) ⇒ Object
114
115
116
|
# File 'lib/bytemapper.rb', line 114
def reset(with_basic_types = true)
@@registry = Registry.new(with_basic_types)
end
|
.reverse_lookup(prefix, value = nil) ⇒ Object
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
# File 'lib/bytemapper.rb', line 118
def reverse_lookup(prefix, value = nil)
@@rl_cache ||= {}
prefix = "#{prefix.to_s}_"
lookup = @@rl_cache[prefix]
if lookup.nil?
labels = names.filter { |n| n.start_with?(prefix) }
values = labels.map { |l| get(l) }
lookup = Hash[values.zip(labels)]
@@rl_cache[prefix] = lookup
end
value.nil? ? lookup : lookup[value]
end
|