class BinData::SanitizedParameters

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.

Constants

BIG_ENDIAN

Memoized constants

LITTLE_ENDIAN

Public Class Methods

new(parameters, the_class, hints) click to toggle source
# File lib/bindata/sanitize.rb, line 197
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] && !hints[:search_prefix].empty?
    self[:search_prefix] = Array(self[:search_prefix]).concat(Array(hints[:search_prefix]))
  end

  sanitize!
end
sanitize(parameters, the_class) click to toggle source
# File lib/bindata/sanitize.rb, line 188
def sanitize(parameters, the_class)
  if SanitizedParameters === parameters
    parameters
  else
    SanitizedParameters.new(parameters, the_class, {})
  end
end

Public Instance Methods

create_sanitized_params(params, the_class) click to toggle source
# File lib/bindata/sanitize.rb, line 287
def create_sanitized_params(params, the_class)
  SanitizedParameters.new(params, the_class, hints)
end
has_at_least_one_of?(*keys) click to toggle source
# File lib/bindata/sanitize.rb, line 215
def has_at_least_one_of?(*keys)
  keys.each do |key|
    return true if has_parameter?(key)
  end

  false
end
hints() click to toggle source
# File lib/bindata/sanitize.rb, line 291
def hints
  { endian: self[:endian], search_prefix: self[:search_prefix] }
end
must_be_integer(*keys) click to toggle source

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

# File lib/bindata/sanitize.rb, line 239
def must_be_integer(*keys)
  keys.each do |key|
    if has_parameter?(key)
      parameter = self[key]
      unless Symbol === parameter ||
             parameter.respond_to?(:arity) ||
             parameter.respond_to?(:to_int)
        raise ArgumentError, "parameter '#{key}' in #{@the_class} must "                                   "evaluate to an integer, got #{parameter.class}"
      end
    end
  end
end
rename_parameter(old_key, new_key) click to toggle source
# File lib/bindata/sanitize.rb, line 253
def rename_parameter(old_key, new_key)
  if has_parameter?(old_key)
    self[new_key] = delete(old_key)
  end
end
sanitize(key) { |self| ... } click to toggle source
# File lib/bindata/sanitize.rb, line 281
def sanitize(key, &block)
  if needs_sanitizing?(key)
    self[key] = yield(self[key])
  end
end
sanitize_choices(key) { |obj| ... } click to toggle source
# File lib/bindata/sanitize.rb, line 271
def sanitize_choices(key, &block)
  sanitize(key) do |obj|
    create_sanitized_choices(yield(obj))
  end
end
sanitize_endian(key) click to toggle source
# File lib/bindata/sanitize.rb, line 277
def sanitize_endian(key)
  sanitize(key) { |endian| create_sanitized_endian(endian) }
end
sanitize_fields(key) { |fields, sanitized_fields| ... } click to toggle source
# File lib/bindata/sanitize.rb, line 263
def sanitize_fields(key, &block)
  sanitize(key) do |fields|
    sanitized_fields = create_sanitized_fields
    yield(fields, sanitized_fields)
    sanitized_fields
  end
end
sanitize_object_prototype(key) click to toggle source
# File lib/bindata/sanitize.rb, line 259
def sanitize_object_prototype(key)
  sanitize(key) { |obj_type, obj_params| create_sanitized_object_prototype(obj_type, obj_params) }
end
warn_replacement_parameter(bad_key, suggested_key) click to toggle source
# File lib/bindata/sanitize.rb, line 223
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

Private Instance Methods

create_sanitized_choices(choices) click to toggle source
# File lib/bindata/sanitize.rb, line 359
def create_sanitized_choices(choices)
  SanitizedChoices.new(choices, hints)
end
create_sanitized_endian(endian) click to toggle source
# File lib/bindata/sanitize.rb, line 347
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_fields() click to toggle source
# File lib/bindata/sanitize.rb, line 363
def create_sanitized_fields
  SanitizedFields.new(hints)
end
create_sanitized_object_prototype(obj_type, obj_params) click to toggle source
# File lib/bindata/sanitize.rb, line 367
def create_sanitized_object_prototype(obj_type, obj_params)
  SanitizedPrototype.new(obj_type, obj_params, hints)
end
ensure_mandatory_parameters_exist() click to toggle source
# File lib/bindata/sanitize.rb, line 327
def ensure_mandatory_parameters_exist
  @the_class.mandatory_parameters.each do |key|
    unless has_parameter?(key)
      raise ArgumentError,
              "parameter '#{key}' must be specified in #{@the_class}"
    end
  end
end
ensure_mutual_exclusion_of_parameters() click to toggle source
# File lib/bindata/sanitize.rb, line 336
def ensure_mutual_exclusion_of_parameters
  return if length < 2

  @the_class.mutually_exclusive_parameters.each do |key1, key2|
    if has_parameter?(key1) && has_parameter?(key2)
      raise ArgumentError, "params '#{key1}' and '#{key2}' "                                 "are mutually exclusive in #{@the_class}"
    end
  end
end
ensure_no_nil_values() click to toggle source
# File lib/bindata/sanitize.rb, line 312
def ensure_no_nil_values
  each do |key, value|
    if value.nil?
      raise ArgumentError,
            "parameter '#{key}' has nil value in #{@the_class}"
    end
  end
end
merge_default_parameters!() click to toggle source
# File lib/bindata/sanitize.rb, line 321
def merge_default_parameters!
  @the_class.default_parameters.each do |key, value|
    self[key] = value unless has_key?(key)
  end
end
needs_sanitizing?(key) click to toggle source
# File lib/bindata/sanitize.rb, line 308
def needs_sanitizing?(key)
  has_key?(key) && ! self[key].is_a?(SanitizedParameter)
end
sanitize!() click to toggle source
# File lib/bindata/sanitize.rb, line 298
def sanitize!
  ensure_no_nil_values
  merge_default_parameters!

  @the_class.arg_processor.sanitize_parameters!(@the_class, self)

  ensure_mandatory_parameters_exist
  ensure_mutual_exclusion_of_parameters
end