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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0376aab4653f3f1fb656d82f679346bd1d04c6d8f80e17540a4a7fbb3bc470d7
4
- data.tar.gz: 211894d68015bc52658e8e818296a42c115bdeda8cfa2cb6525cadb1e9554e9b
3
+ metadata.gz: 1f676ed52c2ea4738cc80dc0c6634ee50d9b4ef844107caad6f15937f70bca6f
4
+ data.tar.gz: b66779b78bc9abb15007089fb4cb4320324b6efa07e92d4df39b82fb16717bbb
5
5
  SHA512:
6
- metadata.gz: 79edc6088c3258506085cf22aee37e75386bab8309c478786ec3022f3895f34a4b05490640bca22db56b78242b500d7030d3fc6b141a24aa0f9cfc1681baef7d
7
- data.tar.gz: 36ddc86813c0c0fc92874025fbe37f755a8f1cef22533949b101428b50ca2a0369aae9beddfe99a4b00d71c0db5985ec4f89b5beddefcc262ea6c91dfd2787b7
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module IOStruct
4
- VERSION = "0.4.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
 
@@ -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.to_s} " + to_h.map do |k, v|
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.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|
160
165
  fmt =
161
166
  case
162
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') }
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.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrey "Zed" Zaikin