bindata 1.8.1 → 1.8.2

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of bindata might be problematic. Click here for more details.

@@ -1,5 +1,10 @@
1
1
  = BinData Changelog
2
2
 
3
+ == Version 1.8.2 (2014-02-02)
4
+
5
+ * Virtual fields can now have values.
6
+ * Bug fixes.
7
+
3
8
  == Version 1.8.1 (2014-01-15)
4
9
 
5
10
  * Added signed bitfields. Requested by redood.
@@ -282,7 +282,7 @@ module BinData
282
282
 
283
283
  def binary_string(str)
284
284
  if str.respond_to?(:force_encoding)
285
- str.dup.force_encoding(Encoding::BINARY)
285
+ str.to_s.dup.force_encoding(Encoding::BINARY)
286
286
  else
287
287
  str.dup
288
288
  end
@@ -97,7 +97,10 @@ module BinData
97
97
  def value
98
98
  snapshot
99
99
  end
100
- alias_method :value=, :assign
100
+
101
+ def value=(val)
102
+ assign(val)
103
+ end
101
104
 
102
105
  def respond_to?(symbol, include_private = false) #:nodoc:
103
106
  child = snapshot
@@ -201,14 +204,18 @@ module BinData
201
204
  def assert!
202
205
  current_value = snapshot
203
206
  expected = eval_parameter(:assert, :value => current_value)
204
- if not expected
205
- raise ValidityError,
206
- "value '#{current_value}' not as expected for #{debug_name}"
207
- elsif current_value != expected and expected != true
208
- raise ValidityError,
209
- "value is '#{current_value}' but " +
210
- "expected '#{expected}' for #{debug_name}"
207
+
208
+ msg = if not expected and current_value.nil?
209
+ "assertion failed"
210
+ elsif not expected
211
+ "value '#{current_value}' not as expected"
212
+ elsif expected != true and current_value != expected
213
+ "value is '#{current_value}' but expected '#{expected}'"
214
+ else
215
+ nil
211
216
  end
217
+
218
+ raise ValidityError, "#{msg} for #{debug_name}" if msg
212
219
  end
213
220
  end
214
221
 
@@ -1,3 +1,3 @@
1
1
  module BinData
2
- VERSION = "1.8.1"
2
+ VERSION = "1.8.2"
3
3
  end
@@ -1,9 +1,9 @@
1
1
  require "bindata/base"
2
2
 
3
3
  module BinData
4
- # A virtual field is one that is neither read, written nor occupies space.
5
- # It is used to make assertions or as a convenient label for determining
6
- # offsets.
4
+ # A virtual field is one that is neither read, written nor occupies space in
5
+ # the data stream. It is used to make assertions or as a convenient label
6
+ # for determining offsets or storing values.
7
7
  #
8
8
  # require 'bindata'
9
9
  #
@@ -26,28 +26,20 @@ module BinData
26
26
  #
27
27
  # [<tt>:assert</tt>] Raise an error when reading or assigning if the value
28
28
  # of this evaluated parameter is false.
29
+ # [<tt>:value</tt>] The virtual object will always have this value.
29
30
  #
30
- class Virtual < BinData::Base
31
+ class Virtual < BinData::BasePrimitive
31
32
 
32
- optional_parameter :assert
33
-
34
- def clear?; true; end
35
- def snapshot; nil; end
36
- def do_num_bytes; 0; end
37
- def do_write(io); end
38
-
39
- def assign(val)
40
- assert!
33
+ def value_to_binary_string(val)
34
+ ""
41
35
  end
42
36
 
43
- def do_read(io)
44
- assert!
37
+ def read_and_return_value(io)
38
+ nil
45
39
  end
46
40
 
47
- def assert!
48
- if has_parameter?(:assert) and not eval_parameter(:assert)
49
- raise ValidityError, "assertion failed for #{debug_name}"
50
- end
41
+ def sensible_default
42
+ nil
51
43
  end
52
44
  end
53
45
  end
@@ -22,7 +22,7 @@ describe BinData::Virtual do
22
22
 
23
23
  it "asserts on #read" do
24
24
  data = []
25
- obj = BinData::Virtual.new(:assert => lambda { data << 1 })
25
+ obj = BinData::Virtual.new(:assert => lambda { data << 1; true })
26
26
 
27
27
  obj.read ""
28
28
  data.must_equal [1]
@@ -30,9 +30,19 @@ describe BinData::Virtual do
30
30
 
31
31
  it "asserts on #assign" do
32
32
  data = []
33
- obj = BinData::Virtual.new(:assert => lambda { data << 1 })
33
+ obj = BinData::Virtual.new(:assert => lambda { data << 1; true })
34
34
 
35
35
  obj.assign("foo")
36
36
  data.must_equal [1]
37
37
  end
38
+
39
+ it "assigns a value" do
40
+ obj = BinData::Virtual.new(3)
41
+ obj.must_equal 3
42
+ end
43
+
44
+ it "accepts the :value parameter" do
45
+ obj = BinData::Virtual.new(:value => 3)
46
+ obj.must_equal 3
47
+ end
38
48
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bindata
3
3
  version: !ruby/object:Gem::Version
4
- hash: 53
4
+ hash: 51
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 8
9
- - 1
10
- version: 1.8.1
9
+ - 2
10
+ version: 1.8.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Dion Mendel
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2014-01-15 00:00:00 +08:00
18
+ date: 2014-02-02 00:00:00 +08:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency