lib/bindata/primitive.rb in bindata-1.1.0 vs lib/bindata/primitive.rb in bindata-1.2.0
- old
+ new
@@ -1,6 +1,7 @@
require 'bindata/base_primitive'
+require 'bindata/dsl'
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.
@@ -57,115 +58,34 @@
# == Parameters
#
# Primitive objects accept all the parameters that BinData::BasePrimitive do.
#
class Primitive < BasePrimitive
+ include DSLMixin
- class << self
+ register_subclasses
+ dsl_parser :multiple_fields, :optional_fieldnames, :sanitize_fields
- def inherited(subclass) #:nodoc:
- # Register the names of all subclasses of this class.
- register(subclass.name, subclass)
- end
-
- def endian(endian = nil)
- @endian ||= default_endian
- if [:little, :big].include?(endian)
- @endian = endian
- elsif endian != nil
- raise ArgumentError,
- "unknown value for endian '#{endian}' in #{self}", caller(1)
- end
- @endian
- end
-
- def fields #:nodoc:
- @fields ||= default_fields
- end
-
- def method_missing(symbol, *args) #:nodoc:
- name, params = args
-
- if name.is_a?(Hash)
- params = name
- name = nil
- end
-
- type = symbol
- name = name.to_s
- params ||= {}
-
- append_field(type, name, params)
- end
-
+ class << self
def sanitize_parameters!(params, sanitizer) #:nodoc:
- struct_params = {}
- struct_params[:fields] = fields
- struct_params[:endian] = endian unless endian.nil?
-
- params[:struct_params] = struct_params
+ params[:struct_params] = sanitizer.create_sanitized_params(to_struct_params, BinData::Struct)
end
-
- #-------------
- private
-
- def parent_primitive
- ancestors[1..-1].find { |cls|
- cls.ancestors[1..-1].include?(BinData::Primitive)
- }
- end
-
- def default_endian
- prim = parent_primitive
- prim ? prim.endian : nil
- end
-
- def default_fields
- prim = parent_primitive
- if prim
- Sanitizer.new.clone_sanitized_fields(prim.fields)
- else
- Sanitizer.new.create_sanitized_fields
- end
- end
-
- def append_field(type, name, params)
- ensure_valid_name(name)
-
- fields.add_field(type, name, params, endian)
- rescue UnknownTypeError => err
- raise TypeError, "unknown type '#{err.message}' for #{self}", caller(2)
- end
-
- def ensure_valid_name(name)
- if fields.field_names.include?(name)
- raise SyntaxError, "duplicate field '#{name}' in #{self}", caller(3)
- end
- if self.instance_methods.collect { |meth| meth.to_s }.include?(name)
- raise NameError.new("", name),
- "field '#{name}' shadows an existing method in #{self}", caller(3)
- end
- end
end
mandatory_parameter :struct_params
- def initialize(params = {}, parent = nil)
- super(params, parent)
+ def initialize(parameters = {}, parent = nil)
+ super
@struct = BinData::Struct.new(get_parameter(:struct_params), self)
end
def method_missing(symbol, *args, &block) #:nodoc:
@struct.__send__(symbol, *args, &block)
end
def debug_name_of(child) #:nodoc:
debug_name + "-internal-"
- end
-
- def offset_of(child) #:nodoc:
- @struct.offset_of(child)
end
#---------------
private