iostruct 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +9 -0
- data/lib/iostruct/version.rb +1 -1
- data/lib/iostruct.rb +8 -3
- data/spec/iostruct_spec.rb +16 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1f676ed52c2ea4738cc80dc0c6634ee50d9b4ef844107caad6f15937f70bca6f
|
4
|
+
data.tar.gz: b66779b78bc9abb15007089fb4cb4320324b6efa07e92d4df39b82fb16717bbb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ee0f456796c677b2d9bd9638b993d88ff828e84453ad8b89dc2b4f7d365cb8c6ab3b34156de415c47294ee88541f2b87157f04526c04930171ed211d51371709
|
7
|
+
data.tar.gz: 914f2243b76864fa1dbc476528512b1827a690fdff29b4981157860c2311e8f0caa31b4cfe5f102dff656fd2ccbd7f0ffa04b22617270726646cbd4a6eb4585a
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,12 @@
|
|
1
|
+
# 0.5.0
|
2
|
+
|
3
|
+
- added `inspect_name_override` constructor param, useful for dynamic declarations:
|
4
|
+
|
5
|
+
```ruby
|
6
|
+
IOStruct.new("NN").new.inspect # "<#<Class:0x000000011c45fa20> f0=nil f4=nil>"
|
7
|
+
IOStruct.new("NN", inspect_name_override: "Point").new.inspect # "<Point f0=nil f4=nil>"
|
8
|
+
```
|
9
|
+
|
1
10
|
# 0.4.0
|
2
11
|
|
3
12
|
- added `size` class method that returns SIZE constant
|
data/lib/iostruct/version.rb
CHANGED
data/lib/iostruct.rb
CHANGED
@@ -41,7 +41,7 @@ module IOStruct
|
|
41
41
|
|
42
42
|
FieldInfo = Struct.new :type, :size, :offset
|
43
43
|
|
44
|
-
def self.new fmt, *names, inspect: :hex, **renames
|
44
|
+
def self.new fmt, *names, inspect: :hex, inspect_name_override: nil, **renames
|
45
45
|
fields, size = parse_format(fmt, names)
|
46
46
|
names = auto_names(fields, size) if names.empty?
|
47
47
|
names.map!{ |n| renames[n] || n } if renames.any?
|
@@ -53,6 +53,7 @@ module IOStruct
|
|
53
53
|
x.extend ClassMethods
|
54
54
|
x.include InstanceMethods
|
55
55
|
x.include HexInspect if inspect == :hex
|
56
|
+
x.define_singleton_method(:name) { inspect_name_override } if inspect_name_override
|
56
57
|
end
|
57
58
|
end # self.new
|
58
59
|
|
@@ -114,6 +115,10 @@ module IOStruct
|
|
114
115
|
def size
|
115
116
|
self::SIZE
|
116
117
|
end
|
118
|
+
|
119
|
+
def name
|
120
|
+
self.to_s
|
121
|
+
end
|
117
122
|
end # ClassMethods
|
118
123
|
|
119
124
|
module InstanceMethods
|
@@ -146,7 +151,7 @@ module IOStruct
|
|
146
151
|
|
147
152
|
module HexInspect
|
148
153
|
def to_s
|
149
|
-
"<#{self.class.
|
154
|
+
"<#{self.class.name} " + to_h.map do |k, v|
|
150
155
|
if v.is_a?(Integer) && v > 9
|
151
156
|
"#{k}=0x%x" % v
|
152
157
|
else
|
@@ -156,7 +161,7 @@ module IOStruct
|
|
156
161
|
end
|
157
162
|
|
158
163
|
def to_table
|
159
|
-
@fmtstr_tbl = "<#{self.class.
|
164
|
+
@fmtstr_tbl = "<#{self.class.name} " + self.class.const_get('FIELDS').map do |name, f|
|
160
165
|
fmt =
|
161
166
|
case
|
162
167
|
when f.type == Integer
|
data/spec/iostruct_spec.rb
CHANGED
@@ -2,6 +2,22 @@ require 'spec_helper'
|
|
2
2
|
require 'stringio'
|
3
3
|
|
4
4
|
describe IOStruct do
|
5
|
+
describe ":inspect_name_override" do
|
6
|
+
context "when set" do
|
7
|
+
it "uses the custom name" do
|
8
|
+
x = IOStruct.new('LL', :x, :y, inspect_name_override: 'Point')
|
9
|
+
expect(x.new.inspect).to match /<Point x=nil y=nil>/
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
context "when not set" do
|
14
|
+
it "has default name" do
|
15
|
+
x = IOStruct.new('LL', :x, :y)
|
16
|
+
expect(x.new.inspect).to match /<#<Class:0x\h+> x=nil y=nil>/
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
5
21
|
describe "#read" do
|
6
22
|
let(:a) { [12345, 56789] }
|
7
23
|
let(:data) { a.pack('L2') }
|