lib/bindata/sanitize.rb in bindata-1.0.0 vs lib/bindata/sanitize.rb in bindata-1.1.0

- old
+ new

@@ -147,14 +147,20 @@ def create_sanitized_choices(choices) SanitizedChoices.new(self, choices) end - def create_sanitized_fields(endian = nil) - SanitizedFields.new(self, endian) + def create_sanitized_fields + SanitizedFields.new(self) end + def clone_sanitized_fields(fields) + new_fields = SanitizedFields.new(self) + new_fields.copy_fields(fields) + new_fields + end + def create_sanitized_object_prototype(obj_type, obj_params, endian = nil) SanitizedPrototype.new(self, obj_type, obj_params, endian) end def with_endian(endian, &block) @@ -190,11 +196,11 @@ #---------------------------------------------------------------------------- class SanitizedParameter; end class SanitizedPrototype < SanitizedParameter - def initialize(sanitizer, obj_type, obj_params, endian = nil) + def initialize(sanitizer, obj_type, obj_params, endian) sanitizer.with_endian(endian) do @obj_class = sanitizer.lookup_class(obj_type) @obj_params = sanitizer.create_sanitized_params(obj_params, @obj_class) end end @@ -204,40 +210,42 @@ end end #---------------------------------------------------------------------------- class SanitizedField < SanitizedParameter - def initialize(sanitizer, name, field_type, field_params) - @name = name.to_s - @prototype = sanitizer.create_sanitized_object_prototype(field_type, field_params) + def initialize(sanitizer, name, field_type, field_params, endian) + @name = (name != nil and name != "") ? name.to_s : nil + @prototype = sanitizer.create_sanitized_object_prototype(field_type, field_params, endian) end attr_reader :name def instantiate(parent = nil) @prototype.instantiate(parent) end end #---------------------------------------------------------------------------- class SanitizedFields < SanitizedParameter - def initialize(sanitizer, endian) + def initialize(sanitizer) @sanitizer = sanitizer - @endian = endian @fields = [] end - def add_field(type, name, params) - @sanitizer.with_endian(@endian) do - @fields << SanitizedField.new(@sanitizer, name, type, params) - end + def add_field(type, name, params, endian) + @fields << SanitizedField.new(@sanitizer, name, type, params, endian) end def [](idx) @fields[idx] end def field_names @fields.collect { |field| field.name } + end + + def copy_fields(other) + other_fields = other.instance_variable_get(:@fields) + @fields.concat(other_fields) end end #---------------------------------------------------------------------------- class SanitizedChoices < SanitizedParameter