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
Class Method Summary collapse
Instance Method Summary collapse
- #create_sanitized_params(params, the_class) ⇒ Object
- #has_at_least_one_of?(*keys) ⇒ Boolean
- #hints ⇒ Object
-
#initialize(parameters, the_class, namespace, hints) ⇒ SanitizedParameters
constructor
A new instance of SanitizedParameters.
- #merge_dsl_params ⇒ Object
-
#must_be_integer(*keys) ⇒ Object
def warn_renamed_parameter(old_key, new_key) val = delete(old_key) if val self = val Kernel.warn ":#old_key has been renamed to :#new_key in #@the_class.
- #must_have_at_least_one_of(*keys) ⇒ Object
- #rename_parameter(old_key, new_key) ⇒ Object
- #sanitize(key, &block) ⇒ Object
- #sanitize_choices(key, &block) ⇒ Object
- #sanitize_endian(key) ⇒ Object
- #sanitize_fields(key, &block) ⇒ Object
- #sanitize_object_prototype(key) ⇒ Object
- #warn_replacement_parameter(bad_key, suggested_key) ⇒ Object
Constructor Details
#initialize(parameters, the_class, namespace, hints) ⇒ SanitizedParameters
Returns a new instance of SanitizedParameters.
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/bindata/sanitize.rb', line 194 def initialize(parameters, the_class, namespace, hints) parameters.each_pair { |key, value| self[key.to_sym] = value } @the_class = the_class @namespace = namespace if hints[:endian] self[:endian] ||= hints[:endian] end if hints[:search_namespace] && !hints[:search_namespace].empty? self[:search_namespace] = Array(self[:search_namespace]).concat(Array(hints[:search_namespace])) end if hints[:search_prefix] && !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
180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/bindata/sanitize.rb', line 180 def sanitize(parameters, the_class) if SanitizedParameters === parameters parameters else namespace = parameters[:namespace] unless namespace m = /(.*)::[^:].*/.match(the_class.name) namespace = m ? m[1] : "" end SanitizedParameters.new(parameters, the_class, namespace, {}) end end |
Instance Method Details
#create_sanitized_params(params, the_class) ⇒ Object
301 302 303 |
# File 'lib/bindata/sanitize.rb', line 301 def create_sanitized_params(params, the_class) SanitizedParameters.new(params, the_class, @namespace, hints) end |
#has_at_least_one_of?(*keys) ⇒ Boolean
217 218 219 220 221 222 223 |
# File 'lib/bindata/sanitize.rb', line 217 def has_at_least_one_of?(*keys) keys.each do |key| return true if has_parameter?(key) end false end |
#hints ⇒ Object
305 306 307 308 309 310 311 |
# File 'lib/bindata/sanitize.rb', line 305 def hints { endian: self[:endian], search_namespace: self[:search_namespace], search_prefix: self[:search_prefix] } end |
#merge_dsl_params ⇒ Object
267 268 269 |
# File 'lib/bindata/sanitize.rb', line 267 def merge_dsl_params self.merge!(@the_class.dsl_params) end |
#must_be_integer(*keys) ⇒ Object
def warn_renamed_parameter(old_key, new_key)
val = delete(old_key)
if val
self = 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
247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/bindata/sanitize.rb', line 247 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 |
#must_have_at_least_one_of(*keys) ⇒ Object
225 226 227 228 229 |
# File 'lib/bindata/sanitize.rb', line 225 def must_have_at_least_one_of(*keys) unless has_at_least_one_of?(*keys) raise ArgumentError, "#{@the_class} requires one of #{keys}" end end |
#rename_parameter(old_key, new_key) ⇒ Object
261 262 263 264 265 |
# File 'lib/bindata/sanitize.rb', line 261 def rename_parameter(old_key, new_key) if has_parameter?(old_key) self[new_key] = delete(old_key) end end |
#sanitize(key, &block) ⇒ Object
295 296 297 298 299 |
# File 'lib/bindata/sanitize.rb', line 295 def sanitize(key, &block) if needs_sanitizing?(key) self[key] = yield(self[key]) end end |
#sanitize_choices(key, &block) ⇒ Object
285 286 287 288 289 |
# File 'lib/bindata/sanitize.rb', line 285 def sanitize_choices(key, &block) sanitize(key) do |obj| create_sanitized_choices(yield(obj)) end end |
#sanitize_endian(key) ⇒ Object
291 292 293 |
# File 'lib/bindata/sanitize.rb', line 291 def sanitize_endian(key) sanitize(key) { |endian| create_sanitized_endian(endian) } end |
#sanitize_fields(key, &block) ⇒ Object
277 278 279 280 281 282 283 |
# File 'lib/bindata/sanitize.rb', line 277 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) ⇒ Object
271 272 273 274 275 |
# File 'lib/bindata/sanitize.rb', line 271 def sanitize_object_prototype(key) sanitize(key) do |obj_type, obj_params| create_sanitized_object_prototype(obj_type, obj_params) end end |
#warn_replacement_parameter(bad_key, suggested_key) ⇒ Object
231 232 233 234 235 236 |
# File 'lib/bindata/sanitize.rb', line 231 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 |