Class: Sanitizable::AttributeSanitizer

Inherits:
Object
  • Object
show all
Defined in:
lib/sanitizable/attribute_sanitizer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ AttributeSanitizer

Returns a new instance of AttributeSanitizer.



5
6
7
8
9
10
# File 'lib/sanitizable/attribute_sanitizer.rb', line 5

def initialize(name, options = {})
  @name = name
  @sanitizers = Array(options[:with])
  @sanitizer_procs = @sanitizers.map(&:to_proc)
  @context = options[:on]
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



3
4
5
# File 'lib/sanitizable/attribute_sanitizer.rb', line 3

def context
  @context
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/sanitizable/attribute_sanitizer.rb', line 3

def name
  @name
end

#sanitizer_procsObject (readonly)

Returns the value of attribute sanitizer_procs.



3
4
5
# File 'lib/sanitizable/attribute_sanitizer.rb', line 3

def sanitizer_procs
  @sanitizer_procs
end

#sanitizersObject (readonly)

Returns the value of attribute sanitizers.



3
4
5
# File 'lib/sanitizable/attribute_sanitizer.rb', line 3

def sanitizers
  @sanitizers
end

Instance Method Details

#inspectObject



12
13
14
# File 'lib/sanitizable/attribute_sanitizer.rb', line 12

def inspect
  "#<#{self.class.name} name: #{name.inspect}, sanitizers: #{sanitizers.inspect}, context: #{context.inspect}>"
end

#sanitize(value) ⇒ Object



16
17
18
19
20
# File 'lib/sanitizable/attribute_sanitizer.rb', line 16

def sanitize(value)
  @sanitizer_procs.inject(value) do |prev_value, sanitizer_proc|
    sanitizer_proc.call(prev_value)
  end
end