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

- old
+ new

@@ -66,23 +66,32 @@ # Register the names of all subclasses of this class. register(subclass.name, subclass) end def endian(endian = nil) - @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) @@ -97,21 +106,33 @@ end #------------- private - def fields - unless defined? @fields - sanitizer = Sanitizer.new - @fields = sanitizer.create_sanitized_fields(endian) + 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 - @fields end def append_field(type, name, params) ensure_valid_name(name) - fields.add_field(type, name, params) + 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)