lib/bindata/primitive.rb in bindata-0.10.0 vs lib/bindata/primitive.rb in bindata-0.11.0

- old
+ new

@@ -1,7 +1,6 @@ require 'bindata/base_primitive' -require 'bindata/registry' require 'bindata/struct' module BinData # A Primitive is a declarative way to define a new BinData data type. # The data type must contain a primitive value only, i.e numbers or strings. @@ -66,15 +65,10 @@ def inherited(subclass) #:nodoc: # Register the names of all subclasses of this class. register(subclass.name, subclass) end - def recursive? - # A Primitive can possibly self reference itself. - true - end - def endian(endian = nil) @endian ||= nil if [:little, :big].include?(endian) @endian = endian elsif endian != nil @@ -88,54 +82,49 @@ type = symbol name = name.to_s params ||= {} - ensure_type_exists(type) - ensure_valid_name(name) unless name.nil? - append_field(type, name, params) end - def sanitize_parameters!(sanitizer, params) + def sanitize_parameters!(params, sanitizer) struct_params = {} struct_params[:fields] = fields struct_params[:endian] = endian unless endian.nil? params[:struct_params] = struct_params - - super(sanitizer, params) 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.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 end - - def append_field(type, name, params) - fields.push([type, name, params]) - end - - def fields - @fields ||= [] - end end mandatory_parameter :struct_params def initialize(params = {}, parent = nil) @@ -143,15 +132,11 @@ @struct = BinData::Struct.new(get_parameter(:struct_params), self) end def method_missing(symbol, *args, &block) - if @struct.respond_to?(symbol) - @struct.__send__(symbol, *args, &block) - else - super - end + @struct.__send__(symbol, *args, &block) end def debug_name_of(child) debug_name + "-internal-" end @@ -191,16 +176,7 @@ raise NotImplementedError end # To be implemented by subclasses ########################################################################### - end - - class SingleValue < Primitive - class << self - def inherited(subclass) #:nodoc: - warn "BinData::BasePrimitiveValue is deprecated. Replacing with BinData::Primitive" - super - end - end end end