Class: BinData::SanitizedParameters
- Inherits:
-
Hash
- Object
- Hash
- BinData::SanitizedParameters
- Defined in:
- lib/bindata/sanitize.rb,
lib/bindata/wrapper.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
Instance Attribute Summary collapse
Class Method Summary collapse
Instance Method Summary collapse
- #create_sanitized_choices(choices) ⇒ Object
- #create_sanitized_endian(endian) ⇒ Object
- #create_sanitized_fields ⇒ Object
- #create_sanitized_object_prototype(obj_type, obj_params) ⇒ Object
- #create_sanitized_params(params, the_class) ⇒ Object
-
#initialize(parameters, the_class, endian) ⇒ SanitizedParameters
constructor
A new instance of SanitizedParameters.
- #move_unknown_parameters_to(dest) ⇒ Object
- #needs_sanitizing?(key) ⇒ Boolean
- #warn_replacement_parameter(bad_key, suggested_key) ⇒ Object
Constructor Details
#initialize(parameters, the_class, endian) ⇒ SanitizedParameters
Returns a new instance of SanitizedParameters.
163 164 165 166 167 168 169 170 |
# File 'lib/bindata/sanitize.rb', line 163 def initialize(parameters, the_class, endian) parameters.each_pair { |key, value| self[key.to_sym] = value } @the_class = the_class @endian = endian sanitize! end |
Instance Attribute Details
#endian ⇒ Object
187 188 189 |
# File 'lib/bindata/sanitize.rb', line 187 def endian @endian || self[:endian] end |
Class Method Details
.sanitize(parameters, the_class) ⇒ Object
154 155 156 157 158 159 160 |
# File 'lib/bindata/sanitize.rb', line 154 def sanitize(parameters, the_class) if SanitizedParameters === parameters parameters else SanitizedParameters.new(parameters, the_class, nil) end end |
Instance Method Details
#create_sanitized_choices(choices) ⇒ Object
206 207 208 |
# File 'lib/bindata/sanitize.rb', line 206 def create_sanitized_choices(choices) SanitizedChoices.new(choices, self.endian) end |
#create_sanitized_endian(endian) ⇒ Object
192 193 194 195 196 197 198 199 200 |
# File 'lib/bindata/sanitize.rb', line 192 def create_sanitized_endian(endian) if endian == :big BIG_ENDIAN elsif endian == :little LITTLE_ENDIAN else raise ArgumentError, "unknown value for endian '#{endian}'" end end |
#create_sanitized_fields ⇒ Object
210 211 212 |
# File 'lib/bindata/sanitize.rb', line 210 def create_sanitized_fields SanitizedFields.new(self.endian) end |
#create_sanitized_object_prototype(obj_type, obj_params) ⇒ Object
214 215 216 |
# File 'lib/bindata/sanitize.rb', line 214 def create_sanitized_object_prototype(obj_type, obj_params) SanitizedPrototype.new(obj_type, obj_params, self.endian) end |
#create_sanitized_params(params, the_class) ⇒ Object
202 203 204 |
# File 'lib/bindata/sanitize.rb', line 202 def create_sanitized_params(params, the_class) SanitizedParameters.new(params, the_class, self.endian) end |
#move_unknown_parameters_to(dest) ⇒ Object
6 7 8 9 10 11 |
# File 'lib/bindata/wrapper.rb', line 6 def move_unknown_parameters_to(dest) unused_keys = keys - @the_class.accepted_parameters.all unused_keys.each do |key| dest[key] = delete(key) end end |
#needs_sanitizing?(key) ⇒ Boolean
174 175 176 177 178 |
# File 'lib/bindata/sanitize.rb', line 174 def needs_sanitizing?(key) parameter = self[key] parameter and not parameter.is_a?(SanitizedParameter) end |
#warn_replacement_parameter(bad_key, suggested_key) ⇒ Object
180 181 182 183 184 185 |
# File 'lib/bindata/sanitize.rb', line 180 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 |