bindata 0.10.0 → 0.11.0

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.

@@ -0,0 +1,148 @@
1
+ module BinData
2
+ class Base
3
+ def single_value?
4
+ warn "#single_value? is deprecated. It should no longer be needed"
5
+ false
6
+ end
7
+ end
8
+
9
+ class SingleValue < Primitive
10
+ class << self
11
+ def inherited(subclass) #:nodoc:
12
+ warn "BinData::SingleValue is deprecated. Replacing with BinData::Primitive"
13
+ super
14
+ end
15
+ end
16
+ end
17
+
18
+ class MultiValue < Record
19
+ class << self
20
+ def inherited(subclass) #:nodoc:
21
+ warn "BinData::MultiValue is deprecated. Replacing with BinData::Record"
22
+ super
23
+ end
24
+ end
25
+ end
26
+
27
+ class Registry
28
+ def Registry.instance #:nodoc:
29
+ warn "'Registry.instance' is deprecated. Replacing with 'RegisteredClasses'"
30
+ RegisteredClasses
31
+ end
32
+ end
33
+
34
+ class Array
35
+ alias_method :orig_clear?, :clear?
36
+ def clear?(index = nil) #:nodoc:
37
+ if index.nil?
38
+ orig_clear?
39
+ elsif index < elements.length
40
+ warn "'obj.clear?(n)' is deprecated. Replacing with 'obj[n].clear?'"
41
+ elements[index].clear?
42
+ else
43
+ true
44
+ end
45
+ end
46
+
47
+ alias_method :orig_clear, :clear
48
+ def clear(index = nil) #:nodoc:
49
+ if index.nil?
50
+ orig_clear
51
+ elsif index < elements.length
52
+ warn "'obj.clear(n)' is deprecated. Replacing with 'obj[n].clear'"
53
+ elements[index].clear
54
+ end
55
+ end
56
+
57
+ alias_method :orig__do_num_bytes, :_do_num_bytes
58
+ def _do_num_bytes(index) #:nodoc:
59
+ if index.nil?
60
+ orig__do_num_bytes(nil)
61
+ elsif index < elements.length
62
+ warn "'obj.num_bytes(n)' is deprecated. Replacing with 'obj[n].num_bytes'"
63
+ elements[index].do_num_bytes
64
+ else
65
+ 0
66
+ end
67
+ end
68
+
69
+ def append(value = nil) #:nodoc:
70
+ warn "#append is deprecated, use push or slice instead"
71
+ if value.nil?
72
+ slice(length)
73
+ else
74
+ push(value)
75
+ end
76
+ self.last
77
+ end
78
+ end
79
+
80
+ class String < BinData::BasePrimitive
81
+ class << self
82
+ def deprecate!(params, old_key, new_key) #:nodoc:
83
+ if params.has_parameter?(old_key)
84
+ warn ":#{old_key} is deprecated. Replacing with :#{new_key}"
85
+ params[new_key] = params.delete(old_key)
86
+ end
87
+ end
88
+
89
+ alias_method :orig_sanitize_parameters!, :sanitize_parameters!
90
+ def sanitize_parameters!(params, sanitizer) #:nodoc:
91
+ deprecate!(params, :trim_value, :trim_padding)
92
+ orig_sanitize_parameters!(params, sanitizer)
93
+ end
94
+ end
95
+ end
96
+
97
+
98
+ class Struct < BinData::Base
99
+ class << self
100
+ def inherited(subclass) #:nodoc:
101
+ if subclass != Record
102
+ fail "error: inheriting from BinData::Struct has been deprecated. Inherit from BinData::Record instead."
103
+ end
104
+ end
105
+ end
106
+
107
+ alias_method :orig_clear, :clear
108
+ def clear(name = nil) #:nodoc:
109
+ if name.nil?
110
+ orig_clear
111
+ else
112
+ warn "'obj.clear(name)' is deprecated. Replacing with 'obj.name.clear'"
113
+ obj = find_obj_for_name(name)
114
+ obj.clear unless obj.nil?
115
+ end
116
+ end
117
+
118
+ alias_method :orig_clear?, :clear?
119
+ def clear?(name = nil) #:nodoc:
120
+ if name.nil?
121
+ orig_clear?
122
+ else
123
+ warn "'obj.clear?(name)' is deprecated. Replacing with 'obj.name.clear?'"
124
+ obj = find_obj_for_name(name)
125
+ obj.nil? ? true : obj.clear?
126
+ end
127
+ end
128
+
129
+ alias_method :orig__do_num_bytes, :_do_num_bytes
130
+ def _do_num_bytes(name) #:nodoc:
131
+ if name.nil?
132
+ orig__do_num_bytes(nil)
133
+ else
134
+ warn "'obj.num_bytes(name)' is deprecated. Replacing with 'obj.name.num_bytes'"
135
+ obj = find_obj_for_name(name)
136
+ obj.nil? ? 0 : obj.do_num_bytes
137
+ end
138
+ end
139
+
140
+ alias_method :orig_offset_of, :offset_of
141
+ def offset_of(child)
142
+ if child.class == ::String
143
+ fail "error: 'offset_of(\"fieldname\")' is deprecated. Use 'fieldname.offset' instead"
144
+ end
145
+ orig_offset_of(child)
146
+ end
147
+ end
148
+ end
data/lib/bindata/lazy.rb CHANGED
@@ -71,12 +71,10 @@ module BinData
71
71
  def index
72
72
  return @overrides[:index] if @overrides.has_key?(:index)
73
73
 
74
- bindata_array_class = BinData.const_defined?("Array") ?
75
- BinData.const_get("Array") : nil
76
74
  child = @obj
77
75
  parent = @obj.parent
78
76
  while parent
79
- if parent.class == bindata_array_class
77
+ if parent.respond_to?(:find_index_of)
80
78
  return parent.find_index_of(child)
81
79
  end
82
80
  child = parent
@@ -1,79 +1,23 @@
1
+ require 'bindata/lazy'
2
+
1
3
  module BinData
2
4
  # BinData objects accept parameters when initializing. AcceptedParameters
3
5
  # allow a BinData class to declaratively identify accepted parameters as
4
6
  # mandatory, optional, default or mutually exclusive.
5
7
  class AcceptedParameters
6
- class << self
7
- def define_all_accessors(obj_class, param_name)
8
- all_accessors = [:mandatory, :optional, :default, :mutually_exclusive]
9
- all_accessors.each do |accessor|
10
- define_accessor(obj_class, param_name, accessor)
11
- end
12
- end
13
-
14
- def define_accessors(obj_class, param_name, *accessors)
15
- accessors.each do |accessor|
16
- define_accessor(obj_class, param_name, accessor)
17
- end
18
- end
19
-
20
- def get(obj_class, param_name)
21
- obj_class.__send__(internal_storage_method_name(param_name))
22
- end
23
-
24
- #-------------
25
- private
26
-
27
- def define_accessor(obj_class, param_name, accessor)
28
- singular_name = accessor_method_name(accessor)
29
- plural_name = singular_name + "s"
30
- internal_storage_method = internal_storage_method_name(param_name)
31
-
32
- ensure_parameter_storage_exists(obj_class, internal_storage_method)
33
-
34
- obj_class.class_eval <<-END
35
- def #{singular_name}(*args)
36
- #{internal_storage_method}.#{accessor}(*args)
37
- end
38
- alias_method :#{plural_name}, :#{singular_name}
39
- END
40
- end
41
-
42
- def accessor_method_name(accessor)
43
- "#{accessor}_parameter"
44
- end
45
-
46
- def internal_storage_method_name(param_name)
47
- "_bindata_accepted_parameters_#{param_name}"
48
- end
49
-
50
- def ensure_parameter_storage_exists(obj_class, method_name)
51
- return if obj_class.instance_methods.include?(method_name)
52
-
53
- iv = "@#{method_name}"
54
- obj_class.class_eval <<-END
55
- def #{method_name}
56
- unless defined? #{iv}
57
- ancestor = ancestors[1..-1].find { |a| a.instance_variable_defined?(:#{iv}) }
58
- ancestor_params = ancestor.nil? ? nil : ancestor.instance_variable_get(:#{iv})
59
- #{iv} = AcceptedParameters.new(ancestor_params)
60
- end
61
- #{iv}
62
- end
63
- END
64
- end
65
- end
66
8
 
67
9
  def initialize(ancestor_params = nil)
68
10
  @mandatory = ancestor_params ? ancestor_params.mandatory : []
69
- @optional = ancestor_params ? ancestor_params.optional : []
70
- @default = ancestor_params ? ancestor_params.default : Hash.new
71
- @mutually_exclusive = ancestor_params ? ancestor_params.mutually_exclusive : []
11
+ @optional = ancestor_params ? ancestor_params.optional : []
12
+ @default = ancestor_params ? ancestor_params.default : Hash.new
13
+ @mutually_exclusive = ancestor_params ?
14
+ ancestor_params.mutually_exclusive : []
72
15
  end
73
16
 
74
17
  def mandatory(*args)
75
18
  if not args.empty?
76
- @mandatory.concat(args.collect { |a| a.to_sym })
19
+ ensure_valid_names(args)
20
+ @mandatory.concat(args.collect { |arg| arg.to_sym })
77
21
  @mandatory.uniq!
78
22
  end
79
23
  @mandatory.dup
@@ -81,7 +25,8 @@ module BinData
81
25
 
82
26
  def optional(*args)
83
27
  if not args.empty?
84
- @optional.concat(args.collect { |a| a.to_sym })
28
+ ensure_valid_names(args)
29
+ @optional.concat(args.collect { |arg| arg.to_sym })
85
30
  @optional.uniq!
86
31
  end
87
32
  @optional.dup
@@ -89,8 +34,9 @@ module BinData
89
34
 
90
35
  def default(args = {})
91
36
  if not args.empty?
92
- args.each_pair do |k,v|
93
- @default[k.to_sym] = v
37
+ ensure_valid_names(args.keys)
38
+ args.each_pair do |param, value|
39
+ @default[param.to_sym] = value
94
40
  end
95
41
  end
96
42
  @default.dup
@@ -109,34 +55,17 @@ module BinData
109
55
  (@mandatory + @optional + @default.keys).uniq
110
56
  end
111
57
 
112
- def sanitize_parameters!(sanitizer, params)
113
- merge_default_parameters!(params)
114
- ensure_mandatory_parameters_exist(params)
115
- ensure_mutual_exclusion_of_parameters(params)
116
- end
117
-
118
58
  #---------------
119
59
  private
120
60
 
121
- def merge_default_parameters!(params)
122
- @default.each do |k,v|
123
- params[k] = v unless params.has_key?(k)
124
- end
125
- end
126
-
127
- def ensure_mandatory_parameters_exist(params)
128
- @mandatory.each do |prm|
129
- unless params.has_key?(prm)
130
- raise ArgumentError, "parameter ':#{prm}' must be specified"
131
- end
132
- end
133
- end
134
-
135
- def ensure_mutual_exclusion_of_parameters(params)
136
- @mutually_exclusive.each do |param1, param2|
137
- if params.has_key?(param1) and params.has_key?(param2)
138
- raise ArgumentError, "params ':#{param1}' and ':#{param2}' " +
139
- "are mutually exclusive"
61
+ def ensure_valid_names(names)
62
+ invalid_names = LazyEvaluator.instance_methods(true) +
63
+ Kernel.methods - ["type"]
64
+ names.each do |name|
65
+ name = name.to_s
66
+ if invalid_names.include?(name)
67
+ raise NameError.new("Rename parameter '#{name}' " +
68
+ "as it shadows an existing method.", name)
140
69
  end
141
70
  end
142
71
  end
@@ -1,5 +1,4 @@
1
1
  require 'bindata/base_primitive'
2
- require 'bindata/registry'
3
2
  require 'bindata/struct'
4
3
 
5
4
  module BinData
@@ -68,11 +67,6 @@ module BinData
68
67
  register(subclass.name, subclass)
69
68
  end
70
69
 
71
- def recursive?
72
- # A Primitive can possibly self reference itself.
73
- true
74
- end
75
-
76
70
  def endian(endian = nil)
77
71
  @endian ||= nil
78
72
  if [:little, :big].include?(endian)
@@ -90,50 +84,45 @@ module BinData
90
84
  name = name.to_s
91
85
  params ||= {}
92
86
 
93
- ensure_type_exists(type)
94
- ensure_valid_name(name) unless name.nil?
95
-
96
87
  append_field(type, name, params)
97
88
  end
98
89
 
99
- def sanitize_parameters!(sanitizer, params)
90
+ def sanitize_parameters!(params, sanitizer)
100
91
  struct_params = {}
101
92
  struct_params[:fields] = fields
102
93
  struct_params[:endian] = endian unless endian.nil?
103
94
 
104
95
  params[:struct_params] = struct_params
105
-
106
- super(sanitizer, params)
107
96
  end
108
97
 
109
98
  #-------------
110
99
  private
111
100
 
112
- def ensure_type_exists(type)
113
- unless RegisteredClasses.is_registered?(type, endian)
114
- raise TypeError, "unknown type '#{type}' for #{self}", caller(2)
101
+ def fields
102
+ unless defined? @fields
103
+ sanitizer = Sanitizer.new
104
+ @fields = sanitizer.create_sanitized_fields(endian)
115
105
  end
106
+ @fields
107
+ end
108
+
109
+ def append_field(type, name, params)
110
+ ensure_valid_name(name)
111
+
112
+ fields.add_field(type, name, params)
113
+ rescue TypeError
114
+ raise TypeError, "unknown type '#{type}' for #{self}", caller(2)
116
115
  end
117
116
 
118
117
  def ensure_valid_name(name)
119
- fields.each do |t, n, p|
120
- if n == name
121
- raise SyntaxError, "duplicate field '#{name}' in #{self}", caller(4)
122
- end
118
+ if fields.field_names.include?(name)
119
+ raise SyntaxError, "duplicate field '#{name}' in #{self}", caller(3)
123
120
  end
124
121
  if self.instance_methods.include?(name)
125
122
  raise NameError.new("", name),
126
- "field '#{name}' shadows an existing method", caller(2)
123
+ "field '#{name}' shadows an existing method", caller(3)
127
124
  end
128
125
  end
129
-
130
- def append_field(type, name, params)
131
- fields.push([type, name, params])
132
- end
133
-
134
- def fields
135
- @fields ||= []
136
- end
137
126
  end
138
127
 
139
128
  mandatory_parameter :struct_params
@@ -145,11 +134,7 @@ module BinData
145
134
  end
146
135
 
147
136
  def method_missing(symbol, *args, &block)
148
- if @struct.respond_to?(symbol)
149
- @struct.__send__(symbol, *args, &block)
150
- else
151
- super
152
- end
137
+ @struct.__send__(symbol, *args, &block)
153
138
  end
154
139
 
155
140
  def debug_name_of(child)
@@ -194,13 +179,4 @@ module BinData
194
179
  # To be implemented by subclasses
195
180
  ###########################################################################
196
181
  end
197
-
198
- class SingleValue < Primitive
199
- class << self
200
- def inherited(subclass) #:nodoc:
201
- warn "BinData::BasePrimitiveValue is deprecated. Replacing with BinData::Primitive"
202
- super
203
- end
204
- end
205
- end
206
182
  end
@@ -1,4 +1,4 @@
1
- require 'bindata/registry'
1
+ require 'bindata/sanitize'
2
2
  require 'bindata/struct'
3
3
 
4
4
  module BinData
@@ -51,11 +51,6 @@ module BinData
51
51
  register(subclass.name, subclass)
52
52
  end
53
53
 
54
- def recursive?
55
- # A Record can self reference itself.
56
- true
57
- end
58
-
59
54
  def endian(endian = nil)
60
55
  @endian ||= nil
61
56
  if [:little, :big].include?(endian)
@@ -79,75 +74,49 @@ module BinData
79
74
  name = name.to_s
80
75
  params ||= {}
81
76
 
82
- ensure_type_exists(type)
83
- ensure_valid_name(name)
84
-
85
77
  append_field(type, name, params)
86
78
  end
87
79
 
88
- def sanitize_parameters!(sanitizer, params)
89
- merge_endian!(params)
90
- merge_fields!(params)
91
- merge_hide!(params)
80
+ def sanitize_parameters!(params, sanitizer)
81
+ params[:fields] = fields
82
+ params[:endian] = endian unless endian.nil?
83
+ params[:hide] = hide unless hide.empty?
92
84
 
93
- super(sanitizer, params)
85
+ super(params, sanitizer)
94
86
  end
95
87
 
96
88
  #-------------
97
89
  private
98
90
 
99
- def ensure_type_exists(type)
100
- unless RegisteredClasses.is_registered?(type, endian)
101
- raise TypeError, "unknown type '#{type}' for #{self}", caller(2)
91
+ def fields
92
+ unless defined? @fields
93
+ sanitizer = Sanitizer.new
94
+ @fields = sanitizer.create_sanitized_fields(endian)
102
95
  end
96
+ @fields
97
+ end
98
+
99
+ def append_field(type, name, params)
100
+ ensure_valid_name(name)
101
+
102
+ fields.add_field(type, name, params)
103
+ rescue TypeError
104
+ raise TypeError, "unknown type '#{type}' for #{self}", caller(2)
103
105
  end
104
106
 
105
107
  def ensure_valid_name(name)
106
- @fields ||= []
107
- @fields.each do |t, n, p|
108
- if n == name
109
- raise SyntaxError, "duplicate field '#{name}' in #{self}", caller(4)
110
- end
108
+ if fields.field_names.include?(name)
109
+ raise SyntaxError, "duplicate field '#{name}' in #{self}", caller(3)
111
110
  end
112
111
  if self.instance_methods.include?(name)
113
112
  raise NameError.new("", name),
114
- "field '#{name}' shadows an existing method", caller(2)
113
+ "field '#{name}' shadows an existing method", caller(3)
115
114
  end
116
115
  if self::RESERVED.include?(name)
117
116
  raise NameError.new("", name),
118
- "field '#{name}' is a reserved name", caller(2)
117
+ "field '#{name}' is a reserved name", caller(3)
119
118
  end
120
119
  end
121
-
122
- def append_field(type, name, params)
123
- @fields ||= []
124
- @fields.push([type, name, params])
125
- end
126
-
127
- def merge_endian!(params)
128
- endian = params[:endian] || self.endian
129
- params[:endian] = endian unless endian.nil?
130
- end
131
-
132
- def merge_fields!(params)
133
- @fields ||= []
134
- fields = params[:fields] || @fields || []
135
- params[:fields] = fields
136
- end
137
-
138
- def merge_hide!(params)
139
- hide = params[:hide] || self.hide
140
- params[:hide] = hide
141
- end
142
- end
143
- end
144
-
145
- class MultiValue < Record
146
- class << self
147
- def inherited(subclass) #:nodoc:
148
- warn "BinData::MultiValue is deprecated. Replacing with BinData::Record"
149
- super
150
- end
151
120
  end
152
121
  end
153
122
  end