lib/bindata/record.rb in bindata-0.10.0 vs lib/bindata/record.rb in bindata-0.11.0
- old
+ new
@@ -1,6 +1,6 @@
-require 'bindata/registry'
+require 'bindata/sanitize'
require 'bindata/struct'
module BinData
# A Record is a declarative wrapper around Struct.
#
@@ -49,15 +49,10 @@
def inherited(subclass) #:nodoc:
# Register the names of all subclasses of this class.
register(subclass.name, subclass)
end
- def recursive?
- # A Record can self reference itself.
- true
- end
-
def endian(endian = nil)
@endian ||= nil
if [:little, :big].include?(endian)
@endian = endian
elsif endian != nil
@@ -77,77 +72,51 @@
type = symbol
name = name.to_s
params ||= {}
- ensure_type_exists(type)
- ensure_valid_name(name)
-
append_field(type, name, params)
end
- def sanitize_parameters!(sanitizer, params)
- merge_endian!(params)
- merge_fields!(params)
- merge_hide!(params)
+ def sanitize_parameters!(params, sanitizer)
+ params[:fields] = fields
+ params[:endian] = endian unless endian.nil?
+ params[:hide] = hide unless hide.empty?
- super(sanitizer, params)
+ super(params, sanitizer)
end
#-------------
private
- def ensure_type_exists(type)
- unless RegisteredClasses.is_registered?(type, endian)
- raise TypeError, "unknown type '#{type}' for #{self}", caller(2)
+ def fields
+ unless defined? @fields
+ sanitizer = Sanitizer.new
+ @fields = sanitizer.create_sanitized_fields(endian)
end
+ @fields
end
+ def append_field(type, name, params)
+ ensure_valid_name(name)
+
+ fields.add_field(type, name, params)
+ rescue TypeError
+ raise TypeError, "unknown type '#{type}' for #{self}", caller(2)
+ end
+
def ensure_valid_name(name)
- @fields ||= []
- @fields.each do |t, n, p|
- if n == name
- raise SyntaxError, "duplicate field '#{name}' in #{self}", caller(4)
- end
+ if fields.field_names.include?(name)
+ raise SyntaxError, "duplicate field '#{name}' in #{self}", caller(3)
end
if self.instance_methods.include?(name)
raise NameError.new("", name),
- "field '#{name}' shadows an existing method", caller(2)
+ "field '#{name}' shadows an existing method", caller(3)
end
if self::RESERVED.include?(name)
raise NameError.new("", name),
- "field '#{name}' is a reserved name", caller(2)
+ "field '#{name}' is a reserved name", caller(3)
end
- end
-
- def append_field(type, name, params)
- @fields ||= []
- @fields.push([type, name, params])
- end
-
- def merge_endian!(params)
- endian = params[:endian] || self.endian
- params[:endian] = endian unless endian.nil?
- end
-
- def merge_fields!(params)
- @fields ||= []
- fields = params[:fields] || @fields || []
- params[:fields] = fields
- end
-
- def merge_hide!(params)
- hide = params[:hide] || self.hide
- params[:hide] = hide
- end
- end
- end
-
- class MultiValue < Record
- class << self
- def inherited(subclass) #:nodoc:
- warn "BinData::MultiValue is deprecated. Replacing with BinData::Record"
- super
end
end
end
end