Class: BinData::SanitizedParameters
- Inherits:
-
Hash
- Object
- Hash
- BinData::SanitizedParameters
- 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
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.
- #needs_sanitizing?(key) ⇒ Boolean
- #warn_renamed_parameter(old_key, new_key) ⇒ Object
- #warn_replacement_parameter(bad_key, suggested_key) ⇒ Object
Constructor Details
#initialize(parameters, the_class, endian) ⇒ SanitizedParameters
Returns a new instance of SanitizedParameters.
197 198 199 200 201 202 203 204 |
# File 'lib/bindata/sanitize.rb', line 197 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
230 231 232 |
# File 'lib/bindata/sanitize.rb', line 230 def endian @endian || self[:endian] end |
Class Method Details
.sanitize(parameters, the_class) ⇒ Object
188 189 190 191 192 193 194 |
# File 'lib/bindata/sanitize.rb', line 188 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
249 250 251 |
# File 'lib/bindata/sanitize.rb', line 249 def create_sanitized_choices(choices) SanitizedChoices.new(choices, self.endian) end |
#create_sanitized_endian(endian) ⇒ Object
235 236 237 238 239 240 241 242 243 |
# File 'lib/bindata/sanitize.rb', line 235 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
253 254 255 |
# File 'lib/bindata/sanitize.rb', line 253 def create_sanitized_fields SanitizedFields.new(self.endian) end |
#create_sanitized_object_prototype(obj_type, obj_params) ⇒ Object
257 258 259 |
# File 'lib/bindata/sanitize.rb', line 257 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
245 246 247 |
# File 'lib/bindata/sanitize.rb', line 245 def create_sanitized_params(params, the_class) SanitizedParameters.new(params, the_class, self.endian) end |
#needs_sanitizing?(key) ⇒ Boolean
208 209 210 211 212 |
# File 'lib/bindata/sanitize.rb', line 208 def needs_sanitizing?(key) parameter = self[key] parameter and not parameter.is_a?(SanitizedParameter) end |
#warn_renamed_parameter(old_key, new_key) ⇒ Object
221 222 223 224 225 226 227 228 |
# File 'lib/bindata/sanitize.rb', line 221 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) ⇒ Object
214 215 216 217 218 219 |
# File 'lib/bindata/sanitize.rb', line 214 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 |