Module: TextSanitizer::ClassMethods

Included in:
TextSanitizer
Defined in:
lib/text_sanitizer/text_sanitizer.rb

Instance Method Summary collapse

Instance Method Details

#register_sanitizer(op, hook = :before_validation, *opt_args) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/text_sanitizer/text_sanitizer.rb', line 5

def register_sanitizer op, hook=:before_validation, *opt_args
  self.class.send :define_method, "#{op}_text" do |*args|  # defines the class shorthand
    validate_and_set_text_fields_for op, hook, args
  end
  self.class.send :define_method, "#{op}_formatter" do |*args|  # defines the class shorthand
    create_formatted_attribute op, args
  end
  send :define_method, "#{op}_text_fields" do  # defines the instance shorthand
    attrs = self.send "text_fields_to_#{op}"
    do_for_text_fields attrs, op
  end
  unless opt_args.empty?  # if fields are given, send class shorthand
    self.send "#{op}_text", *opt_args
  end
end