packed_struct 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -133,6 +133,34 @@ module PackedStruct
133
133
  dir
134
134
  end
135
135
 
136
+ # The number of bytes this takes up in the resulting packed string.
137
+ #
138
+ # @return [Numeric]
139
+ def bytesize
140
+ case @tags[:type]
141
+ when nil
142
+ size / 8
143
+ when :short
144
+ 2
145
+ when :int
146
+ 4
147
+ when :long
148
+ 4
149
+ when :float
150
+ if sub_names.include?(:double)
151
+ 8
152
+ else
153
+ 4
154
+ end
155
+ when :null
156
+ size
157
+ when :string
158
+ size
159
+ else
160
+ 0
161
+ end
162
+ end
163
+
136
164
  private
137
165
 
138
166
  # Returns all of the names of the subs, and caches it.
@@ -9,7 +9,7 @@ module PackedStruct
9
9
  #
10
10
  # @return [Array<Directive>]
11
11
  def directives
12
- @directives.select! { |x| x.parent.nil? }
12
+ @directives = @directives.select { |x| x.parent.nil? }
13
13
  @directives
14
14
  end
15
15
 
@@ -79,6 +79,31 @@ module PackedStruct
79
79
  parts[directive.name] = value
80
80
  end
81
81
 
82
+
83
+ directives.each { |x| x.value = nil }
84
+
85
+ parts.delete(:null) {}
86
+ parts
87
+ end
88
+ # Unpacks from a socket.
89
+ #
90
+ # @param sock [#read] the socket to unpack from.
91
+ # @return [Hash<Symbol, Object>] the unpacked data.
92
+ def unpack_from_socket(sock)
93
+ read = ""
94
+ total = ""
95
+ parts = {}
96
+
97
+ directives.each_with_index do |directive, i|
98
+ total << directive.to_s
99
+ read << sock.read(directive.bytesize)
100
+ value = read.unpack(total)[i]
101
+ directive.value = value
102
+ parts[directive.name] = value
103
+ end
104
+
105
+ p read
106
+
82
107
  directives.each { |x| x.value = nil }
83
108
 
84
109
  parts.delete(:null) {}
@@ -1,5 +1,5 @@
1
1
  module PackedStruct
2
2
 
3
3
  # The current version of PackedStruct.
4
- VERSION = "0.1.2".freeze
4
+ VERSION = "0.2.0".freeze
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: packed_struct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: