Module: Scrubby::InstanceMethods

Defined in:
lib/scrubby.rb

Instance Method Summary collapse

Instance Method Details

#scrub(value) ⇒ Object

The scrub instance method is the default way in which incoming values are cleaned. By default, the behavior is to strip string values and to return nil for blank strings.

If a different behavoir is required, this method can be overridden at the ActiveRecord::Base level and/or at the level of any of ActiveRecord’s inherited models.



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/scrubby.rb', line 90

def scrub(value)
  value = value.dup if value.duplicable?

  case value
    when String
      value.strip!
      value.blank? ? nil : value
    else
      value
  end
end

#write_attribute_with_scrub(attribute, value) ⇒ Object

An alias of the write_attribute method, write_attribute_with_scrub will check whether a scrubber exists for the given attribute and if so, scrub the given value before passing it on to the original write_attribute method.



77
78
79
80
81
82
83
# File 'lib/scrubby.rb', line 77

def write_attribute_with_scrub(attribute, value)
  if self.class.scrubby? && scrubber = scrubbers[attribute.to_s]
    value = scrubber.bind(self).call(value)
  end

  write_attribute_without_scrub(attribute, value)
end