Class: BinData::Sanitizer

Inherits:
Object
  • Object
show all
Defined in:
lib/bindata/sanitize.rb

Overview

The Sanitizer sanitizes the parameters that are passed when creating a BinData object. Sanitizing consists of checking for mandatory, optional and default parameters and ensuring the values of known parameters are valid.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSanitizer

Returns a new instance of Sanitizer.



144
145
146
# File 'lib/bindata/sanitize.rb', line 144

def initialize
  @endian = nil
end

Class Method Details

.sanitize(params, the_class) ⇒ Object

Sanitize params for the_class. Returns sanitized parameters.



134
135
136
137
138
139
140
141
# File 'lib/bindata/sanitize.rb', line 134

def sanitize(params, the_class)
  if params.is_a?(SanitizedParameters) and params.all_sanitized?
    params
  else
    sanitizer = self.new
    sanitizer.create_sanitized_params(params, the_class)
  end
end

Instance Method Details

#create_sanitized_choices(choices) ⇒ Object



166
167
168
# File 'lib/bindata/sanitize.rb', line 166

def create_sanitized_choices(choices)
  SanitizedChoices.new(self, choices)
end

#create_sanitized_endian(endian) ⇒ Object



155
156
157
158
159
160
161
162
163
164
# File 'lib/bindata/sanitize.rb', line 155

def create_sanitized_endian(endian)
  # memoize return value to reduce memory usage
  if endian == :big
    @@sbe ||= SanitizedBigEndian.new
  elsif endian == :little
    @@sle ||= SanitizedLittleEndian.new
  else
    raise ArgumentError, "unknown value for endian '#{endian}'"
  end
end

#create_sanitized_fields(fields = nil) ⇒ Object



170
171
172
173
174
# File 'lib/bindata/sanitize.rb', line 170

def create_sanitized_fields(fields = nil)
  new_fields = SanitizedFields.new(self)
  new_fields.copy_fields(fields) if fields
  new_fields
end

#create_sanitized_object_prototype(obj_type, obj_params, endian = nil) ⇒ Object



176
177
178
# File 'lib/bindata/sanitize.rb', line 176

def create_sanitized_object_prototype(obj_type, obj_params, endian = nil)
  SanitizedPrototype.new(self, obj_type, obj_params, endian)
end

#create_sanitized_params(params, the_class) ⇒ Object



148
149
150
151
152
153
# File 'lib/bindata/sanitize.rb', line 148

def create_sanitized_params(params, the_class)
  sanitized_params = as_sanitized_params(params, the_class)
  sanitized_params.sanitize!(self)

  sanitized_params
end

#lookup_class(type) ⇒ Object



191
192
193
# File 'lib/bindata/sanitize.rb', line 191

def lookup_class(type)
  RegisteredClasses.lookup(type, @endian)
end

#with_endian(endian, &block) ⇒ Object



180
181
182
183
184
185
186
187
188
189
# File 'lib/bindata/sanitize.rb', line 180

def with_endian(endian, &block)
  if endian != nil
    saved_endian = @endian
    @endian = endian.respond_to?(:endian) ? endian.endian : endian
    yield
    @endian = saved_endian
  else
    yield
  end
end