Module: DataMapper::Sanitizer::InstanceMethods

Defined in:
lib/dm-sanitizer.rb

Instance Method Summary collapse

Instance Method Details

#sanitize!Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/dm-sanitizer.rb', line 77

def sanitize!
  options = self.class.sanitization_options
  return false if options[:disabled]
  
  self.class.properties.each do |property|
    property_name = property.name.to_sym
    
    next unless property.type == String || property.type == DataMapper::Types::Text
    next if !new_record? && !attribute_dirty?(property.name.to_sym)
    next if options[:exclude] && options[:exclude].include?(property_name)
    
    property_mode = options[:modes] ? options[:modes][property_name] || options[:default_mode] : options[:default_mode]
    
    sanitize_property!(property_name, property_mode)
  end
  return true
end

#sanitize_property!(name, mode) ⇒ Object



95
96
97
98
99
100
# File 'lib/dm-sanitizer.rb', line 95

def sanitize_property!(name, mode)
  value = self.send( name )
  return if value.nil? || value.empty?
  sanitized_value = Sanitize.clean(value, self.class.sanitization_options[:mode_definitions][mode])
  self.send( name.to_s+'=', sanitized_value)
end