Class: BinData::SanitizedParameters

Inherits:
Object
  • Object
show all
Defined in:
lib/bindata/sanitize.rb

Overview

When a BinData object is instantiated, it can be supplied parameters to determine its behaviour. These parameters must be sanitized to ensure their values are valid. When instantiating many objects, such as an array of records, there is much duplicated validation.

The purpose of the sanitizing code is to eliminate the duplicated validation.

SanitizedParameters is a hash-like collection of parameters. Its purpose it to recursively sanitize the parameters of an entire BinData object chain at a single time.

Instance Method Summary collapse

Constructor Details

#initialize(parameters, the_class) ⇒ SanitizedParameters

Returns a new instance of SanitizedParameters.



18
19
20
21
22
23
24
25
26
# File 'lib/bindata/sanitize.rb', line 18

def initialize(parameters, the_class)
  @all_sanitized = false
  @the_class = the_class

  @parameters = {}
  parameters.each { |key, value| @parameters[key.to_sym] = value }

  ensure_no_nil_values
end

Instance Method Details

#[](key) ⇒ Object



33
34
35
# File 'lib/bindata/sanitize.rb', line 33

def [](key)
  @parameters[key]
end

#[]=(key, value) ⇒ Object



37
38
39
# File 'lib/bindata/sanitize.rb', line 37

def []=(key, value)
  @parameters[key] = value unless @all_sanitized
end

#all_sanitized?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/bindata/sanitize.rb', line 51

def all_sanitized?
  @all_sanitized
end

#has_parameter?(key) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/bindata/sanitize.rb', line 41

def has_parameter?(key)
  @parameters.has_key?(key)
end

#lengthObject Also known as: size



28
29
30
# File 'lib/bindata/sanitize.rb', line 28

def length
  @parameters.size
end

#move_unknown_parameters_to(dest) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/bindata/sanitize.rb', line 68

def move_unknown_parameters_to(dest)
  unless @all_sanitized
    unused_keys = @parameters.keys - @the_class.accepted_parameters.all
    unused_keys.each do |key|
      dest[key] = @parameters.delete(key)
    end
  end
end

#needs_sanitizing?(key) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
49
# File 'lib/bindata/sanitize.rb', line 45

def needs_sanitizing?(key)
  parameter = @parameters[key]

  parameter and not parameter.is_a?(SanitizedParameter)
end

#sanitize!(sanitizer) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/bindata/sanitize.rb', line 55

def sanitize!(sanitizer)
  unless @all_sanitized
    merge_default_parameters!

    @the_class.sanitize_parameters!(self, sanitizer)

    ensure_mandatory_parameters_exist
    ensure_mutual_exclusion_of_parameters

    @all_sanitized = true
  end
end

#warn_replacement_parameter(bad_key, suggested_key) ⇒ Object



77
78
79
80
81
82
# File 'lib/bindata/sanitize.rb', line 77

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