Parent

Files

Class/Module Index [+]

Quicksearch

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

Attributes

endian[W]

Public Class Methods

new(parameters, the_class, endian) click to toggle source
# File lib/bindata/sanitize.rb, line 191
def initialize(parameters, the_class, endian)
  parameters.each_pair { |key, value| self[key.to_sym] = value }

  @the_class = the_class
  @endian    = endian

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

Public Instance Methods

create_sanitized_choices(choices) click to toggle source
# File lib/bindata/sanitize.rb, line 259
def create_sanitized_choices(choices)
  SanitizedChoices.new(choices, self.endian)
end
create_sanitized_endian(endian) click to toggle source
# File lib/bindata/sanitize.rb, line 243
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 263
def create_sanitized_fields
  SanitizedFields.new(self.endian)
end
create_sanitized_object_prototype(obj_type, obj_params) click to toggle source
# File lib/bindata/sanitize.rb, line 267
def create_sanitized_object_prototype(obj_type, obj_params)
  SanitizedPrototype.new(obj_type, obj_params, self.endian)
end
create_sanitized_params(params, the_class) click to toggle source
# File lib/bindata/sanitize.rb, line 255
def create_sanitized_params(params, the_class)
  SanitizedParameters.new(params, the_class, self.endian)
end
endian() click to toggle source
# File lib/bindata/sanitize.rb, line 238
def endian
  @endian || self[:endian]
end
must_be_integer(*keys) click to toggle source
# File lib/bindata/sanitize.rb, line 224
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) click to toggle source
# File lib/bindata/sanitize.rb, line 202
def needs_sanitizing?(key)
  parameter = self[key]

  parameter and not parameter.is_a?(SanitizedParameter)
end
warn_renamed_parameter(old_key, new_key) click to toggle source
# File lib/bindata/sanitize.rb, line 215
def warn_renamed_parameter(old_key, new_key)
  val = delete(old_key)
  if val
    self[new_key] = val
    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
warn_replacement_parameter(bad_key, suggested_key) click to toggle source
# File lib/bindata/sanitize.rb, line 208
def warn_replacement_parameter(bad_key, suggested_key)
  if has_parameter?(bad_key)
    warn ":#{bad_key} is not used with #{@the_class}.  " +
    "You probably want to change this to :#{suggested_key}"
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.