iostruct 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4a17b782202e0503788fd3b16dffdebddecd62becb30c52ba28161ebde1e300d
4
- data.tar.gz: d42484281a468a378902d4589b67201ad4f134660559766690e48849b9f2a46c
3
+ metadata.gz: 1f676ed52c2ea4738cc80dc0c6634ee50d9b4ef844107caad6f15937f70bca6f
4
+ data.tar.gz: b66779b78bc9abb15007089fb4cb4320324b6efa07e92d4df39b82fb16717bbb
5
5
  SHA512:
6
- metadata.gz: db34080f66b2815628e20a6e7b9331089d55a011dd35ae90b9e8f7758cb97332fad7b9caadfcc977a93e104cb0d9b9e3c932e82a2070e54205ba3bb7a52337d4
7
- data.tar.gz: d25f3804da77a52630bb4492548c3ab71124c06610558dde277ea2c769a826ce8a720a5df99b2ab3691791b090f6ec4774b65e8072117383ab389e4095451a0f
6
+ metadata.gz: ee0f456796c677b2d9bd9638b993d88ff828e84453ad8b89dc2b4f7d365cb8c6ab3b34156de415c47294ee88541f2b87157f04526c04930171ed211d51371709
7
+ data.tar.gz: 914f2243b76864fa1dbc476528512b1827a690fdff29b4981157860c2311e8f0caa31b4cfe5f102dff656fd2ccbd7f0ffa04b22617270726646cbd4a6eb4585a
data/CHANGELOG.md CHANGED
@@ -1,3 +1,22 @@
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
+
10
+ # 0.4.0
11
+
12
+ - added `size` class method that returns SIZE constant
13
+
14
+ ```ruby
15
+ X = IOStruct.new('LL')
16
+ X::SIZE # 8
17
+ X.size # 8
18
+ ```
19
+
1
20
  # 0.3.0
2
21
 
3
22
  - added `__offset` field:
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module IOStruct
4
- VERSION = "0.3.0"
4
+ VERSION = "0.5.0"
5
5
  end
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
 
@@ -110,6 +111,14 @@ module IOStruct
110
111
  # end
111
112
  new(*data.unpack(const_get('FORMAT'))).tap{ |x| x.__offset = pos }
112
113
  end
114
+
115
+ def size
116
+ self::SIZE
117
+ end
118
+
119
+ def name
120
+ self.to_s
121
+ end
113
122
  end # ClassMethods
114
123
 
115
124
  module InstanceMethods
@@ -142,7 +151,7 @@ module IOStruct
142
151
 
143
152
  module HexInspect
144
153
  def to_s
145
- "<#{self.class.to_s} " + to_h.map do |k, v|
154
+ "<#{self.class.name} " + to_h.map do |k, v|
146
155
  if v.is_a?(Integer) && v > 9
147
156
  "#{k}=0x%x" % v
148
157
  else
@@ -152,7 +161,7 @@ module IOStruct
152
161
  end
153
162
 
154
163
  def to_table
155
- @fmtstr_tbl = "<#{self.class.to_s} " + self.class.const_get('FIELDS').map do |name, f|
164
+ @fmtstr_tbl = "<#{self.class.name} " + self.class.const_get('FIELDS').map do |name, f|
156
165
  fmt =
157
166
  case
158
167
  when f.type == Integer
@@ -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') }
@@ -36,10 +52,14 @@ describe IOStruct do
36
52
  expect(x.c).to eq 2
37
53
  end
38
54
 
39
- it "has correct size" do
55
+ it "has correct SIZE" do
40
56
  expect(struct::SIZE).to eq 2
41
57
  end
42
58
 
59
+ it "has correct size" do
60
+ expect(struct.size).to eq 2
61
+ end
62
+
43
63
  it "reads correct number of bytes from IO" do
44
64
  io = StringIO.new(data*2)
45
65
  x = struct.read(io)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: iostruct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrey "Zed" Zaikin