Class: BinData::SanitizedParameters

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

Overview

A BinData object accepts arbitrary parameters. This class sanitizes those parameters so they can be used by the BinData object.

Instance Method Summary collapse

Constructor Details

#initialize(params, the_class) ⇒ SanitizedParameters

Returns a new instance of SanitizedParameters.



9
10
11
12
13
14
15
16
17
# File 'lib/bindata/sanitize.rb', line 9

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

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

  ensure_no_nil_values
end

Instance Method Details

#[](param) ⇒ Object



24
25
26
# File 'lib/bindata/sanitize.rb', line 24

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

#[]=(param, value) ⇒ Object



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

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

#all_sanitized?Boolean

Returns:

  • (Boolean)


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

def all_sanitized?
  @all_sanitized
end

#delete(param) ⇒ Object



67
68
69
# File 'lib/bindata/sanitize.rb', line 67

def delete(param)
  @parameters.delete(param)
end

#has_parameter?(param) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#lengthObject Also known as: size



19
20
21
# File 'lib/bindata/sanitize.rb', line 19

def length
  @parameters.size
end

#move_unknown_parameters_to(dest) ⇒ Object



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

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

#needs_sanitizing?(param) ⇒ Boolean

Returns:

  • (Boolean)


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

def needs_sanitizing?(param)
  has_parameter?(param) and not self[param].is_a?(SanitizedParameter)
end

#sanitize!(sanitizer) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/bindata/sanitize.rb', line 44

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