Class: ActiveCleaner::BaseCleaner
- Inherits:
-
Object
- Object
- ActiveCleaner::BaseCleaner
show all
- Defined in:
- lib/active_cleaner/base_cleaner.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(attr_name, options = {}) ⇒ BaseCleaner
Accepts options that will be made available through the options
reader.
9
10
11
12
13
14
|
# File 'lib/active_cleaner/base_cleaner.rb', line 9
def initialize(attr_name, options = {})
@attr_name = attr_name
@options = {
:nilify => false,
}.merge(options).freeze
end
|
Instance Attribute Details
#attr_name ⇒ Object
Returns the value of attribute attr_name.
6
7
8
|
# File 'lib/active_cleaner/base_cleaner.rb', line 6
def attr_name
@attr_name
end
|
#options ⇒ Object
Returns the value of attribute options.
6
7
8
|
# File 'lib/active_cleaner/base_cleaner.rb', line 6
def options
@options
end
|
Class Method Details
.kind ⇒ Object
16
17
18
|
# File 'lib/active_cleaner/base_cleaner.rb', line 16
def self.kind
@kind ||= name.split('::').last.underscore.sub(/_cleaner$/, '').to_sym
end
|
Instance Method Details
#==(other) ⇒ Object
43
44
45
|
# File 'lib/active_cleaner/base_cleaner.rb', line 43
def ==(other)
kind == other.kind && attr_name == other.attr_name && options == other.options
end
|
#clean(record) ⇒ Object
24
25
26
27
28
29
30
31
32
|
# File 'lib/active_cleaner/base_cleaner.rb', line 24
def clean(record)
value = record.read_attribute_for_cleaning(attr_name)
new_value = clean_value(value, record)
new_value = nil if @options[:nilify] && nilify_value?(new_value, record)
record.write_attribute_after_cleaning(attr_name, new_value) unless new_value == value
end
|
#clean_value(value, record = nil) ⇒ Object
34
35
36
|
# File 'lib/active_cleaner/base_cleaner.rb', line 34
def clean_value(value, record=nil)
raise NotImplementedError, "Subclasses must implement a clean(value, record=nil) method."
end
|
#kind ⇒ Object
20
21
22
|
# File 'lib/active_cleaner/base_cleaner.rb', line 20
def kind
self.class.kind
end
|
#nilify_value?(value, record = nil) ⇒ Boolean
feel free to subclass for your custom cleaner
39
40
41
|
# File 'lib/active_cleaner/base_cleaner.rb', line 39
def nilify_value?(value, record=nil)
value == ""
end
|