Class: BinData::SanitizedParameters

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

Overview

BinData objects are instantiated with parameters to determine their behaviour. These parameters must be sanitized to ensure their values are valid. When instantiating many objects with identical parameters, such as an array of records, there is much duplicated sanitizing.

The purpose of the sanitizing code is to eliminate the duplicated validation.

SanitizedParameters is a hash-like collection of parameters. Its purpose is to recursively sanitize the parameters of an entire BinData object chain at a single time.

Constant Summary collapse

BIG_ENDIAN =

Memoized constants

SanitizedBigEndian.new
LITTLE_ENDIAN =
SanitizedLittleEndian.new

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parameters, the_class, hints) ⇒ SanitizedParameters

Returns a new instance of SanitizedParameters.



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/bindata/sanitize.rb', line 194

def initialize(parameters, the_class, hints)
  parameters.each_pair { |key, value| self[key.to_sym] = value }

  @the_class = the_class

  if hints[:endian]
    self[:endian] ||= hints[:endian]
  end

  if hints[:search_prefix] and not hints[:search_prefix].empty?
    self[:search_prefix] = Array(self[:search_prefix]).concat(Array(hints[:search_prefix]))
  end

  sanitize!
end

Class Method Details

.sanitize(parameters, the_class) ⇒ Object



185
186
187
188
189
190
191
# File 'lib/bindata/sanitize.rb', line 185

def sanitize(parameters, the_class)
  if SanitizedParameters === parameters
    parameters
  else
    SanitizedParameters.new(parameters, the_class, {})
  end
end

Instance Method Details

#create_sanitized_choices(choices) ⇒ Object



268
269
270
# File 'lib/bindata/sanitize.rb', line 268

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

#create_sanitized_endian(endian) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
# File 'lib/bindata/sanitize.rb', line 252

def create_sanitized_endian(endian)
  if endian == :big
    BIG_ENDIAN
  elsif endian == :little
    LITTLE_ENDIAN
  elsif endian == :big_and_little
    raise ArgumentError, ":endian => :big or :endian => :little is required"
  else
    raise ArgumentError, "unknown value for endian '#{endian}'"
  end
end

#create_sanitized_fieldsObject



272
273
274
# File 'lib/bindata/sanitize.rb', line 272

def create_sanitized_fields
  SanitizedFields.new(hints)
end

#create_sanitized_object_prototype(obj_type, obj_params) ⇒ Object



276
277
278
# File 'lib/bindata/sanitize.rb', line 276

def create_sanitized_object_prototype(obj_type, obj_params)
  SanitizedPrototype.new(obj_type, obj_params, hints)
end

#create_sanitized_params(params, the_class) ⇒ Object



264
265
266
# File 'lib/bindata/sanitize.rb', line 264

def create_sanitized_params(params, the_class)
  SanitizedParameters.new(params, the_class, hints)
end

#hintsObject



248
249
250
# File 'lib/bindata/sanitize.rb', line 248

def hints
  { :endian => self[:endian], :search_prefix => self[:search_prefix] }
end

#must_be_integer(*keys) ⇒ Object

def warn_renamed_parameter(old_key, new_key)

  val = delete(old_key)
  if val
    self[new_key] = val
    Kernel.warn ":#{old_key} has been renamed to :#{new_key} in #{@the_class}.  " +
    "Using :#{old_key} is now deprecated and will be removed in the future"
  end
end


234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/bindata/sanitize.rb', line 234

def must_be_integer(*keys)
  keys.each do |key|
    if has_parameter?(key)
      parameter = self[key]
      unless Symbol === parameter or
             parameter.respond_to? :arity or
             parameter.respond_to? :to_int
        raise ArgumentError, "parameter '#{key}' in #{@the_class} must " +
                             "evaluate to an integer, got #{parameter.class}"
      end
    end
  end
end

#needs_sanitizing?(key) ⇒ Boolean

Returns:

  • (Boolean)


212
213
214
215
216
# File 'lib/bindata/sanitize.rb', line 212

def needs_sanitizing?(key)
  parameter = self[key]

  parameter and not parameter.is_a?(SanitizedParameter)
end

#warn_replacement_parameter(bad_key, suggested_key) ⇒ Object



218
219
220
221
222
223
# File 'lib/bindata/sanitize.rb', line 218

def warn_replacement_parameter(bad_key, suggested_key)
  if has_parameter?(bad_key)
    Kernel.warn ":#{bad_key} is not used with #{@the_class}.  " +
    "You probably want to change this to :#{suggested_key}"
  end
end