Class: InputSanitizer::V1::Sanitizer

Inherits:
Object
  • Object
show all
Defined in:
lib/input_sanitizer/v1/sanitizer.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Sanitizer

Returns a new instance of Sanitizer.



2
3
4
5
6
7
# File 'lib/input_sanitizer/v1/sanitizer.rb', line 2

def initialize(data)
  @data = symbolize_keys(data)
  @performed = false
  @errors = []
  @cleaned = InputSanitizer::RestrictedHash.new(self.class.fields.keys)
end

Class Method Details

.clean(data) ⇒ Object



9
10
11
# File 'lib/input_sanitizer/v1/sanitizer.rb', line 9

def self.clean(data)
  new(data).cleaned
end

.convertersObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/input_sanitizer/v1/sanitizer.rb', line 36

def self.converters
  {
    :integer => InputSanitizer::V1::IntegerConverter.new,
    :string => InputSanitizer::V1::StringConverter.new,
    :date => InputSanitizer::V1::DateConverter.new,
    :time => InputSanitizer::V1::TimeConverter.new,
    :boolean => InputSanitizer::V1::BooleanConverter.new,
    :integer_or_blank => InputSanitizer::V1::IntegerConverter.new.extend(InputSanitizer::V1::AllowNil),
    :string_or_blank => InputSanitizer::V1::StringConverter.new.extend(InputSanitizer::V1::AllowNil),
    :date_or_blank => InputSanitizer::V1::DateConverter.new.extend(InputSanitizer::V1::AllowNil),
    :time_or_blank => InputSanitizer::V1::TimeConverter.new.extend(InputSanitizer::V1::AllowNil),
    :boolean_or_blank => InputSanitizer::V1::BooleanConverter.new.extend(InputSanitizer::V1::AllowNil),
  }
end

.custom(*keys) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/input_sanitizer/v1/sanitizer.rb', line 67

def self.custom(*keys)
  options = keys.pop
  converter = options.delete(:converter)
  keys.push(options)
  raise "You did not define a converter for a custom type" if converter == nil
  self.set_keys_to_converter(keys, converter)
end

.inherited(subclass) ⇒ Object



51
52
53
# File 'lib/input_sanitizer/v1/sanitizer.rb', line 51

def self.inherited(subclass)
  subclass.fields = self.fields.dup
end

.initialize_types_dslObject



55
56
57
58
59
60
61
62
63
# File 'lib/input_sanitizer/v1/sanitizer.rb', line 55

def self.initialize_types_dsl
  converters.keys.each do |name|
    class_eval <<-END
      def self.#{name}(*keys)
        set_keys_to_converter(keys, :#{name})
      end
    END
  end
end

.nested(*keys) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/input_sanitizer/v1/sanitizer.rb', line 75

def self.nested(*keys)
  options = keys.pop
  sanitizer = options.delete(:sanitizer)
  keys.push(options)
  raise "You did not define a sanitizer for nested value" if sanitizer == nil
  converter = lambda { |value|
    instance = sanitizer.new(value)
    raise InputSanitizer::ConversionError.new(instance.errors) unless instance.valid?
    instance.cleaned
  }

  keys << {} unless keys.last.is_a?(Hash)
  keys.last[:nested] = true

  self.set_keys_to_converter(keys, converter)
end

Instance Method Details

#[](field) ⇒ Object



13
14
15
# File 'lib/input_sanitizer/v1/sanitizer.rb', line 13

def [](field)
  cleaned[field]
end

#cleanedObject



17
18
19
20
21
22
23
24
# File 'lib/input_sanitizer/v1/sanitizer.rb', line 17

def cleaned
  return @cleaned if @performed

  perform_clean

  @performed = true
  @cleaned.freeze
end

#errorsObject



31
32
33
34
# File 'lib/input_sanitizer/v1/sanitizer.rb', line 31

def errors
  cleaned
  @errors
end

#valid?Boolean

Returns:

  • (Boolean)


26
27
28
29
# File 'lib/input_sanitizer/v1/sanitizer.rb', line 26

def valid?
  cleaned
  @errors.empty?
end