Class: SanitizedAttributes::SanitizedAttribute
- Inherits:
-
Object
- Object
- SanitizedAttributes::SanitizedAttribute
- Defined in:
- lib/sanitized_attributes/sanitized_attribute.rb
Class Method Summary collapse
Instance Method Summary collapse
- #define_ar_writer_method(klass) ⇒ Object
- #define_writer_method(klass) ⇒ Object
-
#initialize(attr_name, options = {}) ⇒ SanitizedAttribute
constructor
A new instance of SanitizedAttribute.
- #sanitize(content) ⇒ Object
Constructor Details
#initialize(attr_name, options = {}) ⇒ SanitizedAttribute
Returns a new instance of SanitizedAttribute.
3 4 5 6 |
# File 'lib/sanitized_attributes/sanitized_attribute.rb', line 3 def initialize(attr_name, = {}) @attr_name = attr_name @options = end |
Class Method Details
.add(klass, attr_name, options = {}) ⇒ Object
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/sanitized_attributes/sanitized_attribute.rb', line 43 def add(klass, attr_name, = {}) attrib = new(attr_name, ) if klass.respond_to?(:alias_method_chain) attrib.define_ar_writer_method(klass) else attrib.define_writer_method(klass) klass.send(:alias_method, "#{attr_name}_without_sanitization=", "#{attr_name}=") klass.send(:alias_method, "#{attr_name}=", "#{attr_name}_with_sanitization=") end end |
Instance Method Details
#define_ar_writer_method(klass) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/sanitized_attributes/sanitized_attribute.rb', line 12 def define_ar_writer_method(klass) this = self attr_name = @attr_name if klass.instance_methods.include?("#{attr_name}=") klass.send(:define_method, "#{attr_name}_with_sanitization=") {|value| send(:"#{attr_name}_without_sanitization=", this.sanitize(value)) } klass.send(:alias_method_chain, :"#{attr_name}=", :sanitization) else klass.send(:define_method, "#{attr_name}=") {|value| send(:write_attribute, attr_name, this.sanitize(value)) } end end |
#define_writer_method(klass) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/sanitized_attributes/sanitized_attribute.rb', line 27 def define_writer_method(klass) this = self attr_name = @attr_name klass.send(:define_method, "#{@attr_name}_with_sanitization=") {|value| send("#{attr_name}_without_sanitization=", this.sanitize(value)) } end |
#sanitize(content) ⇒ Object
8 9 10 |
# File 'lib/sanitized_attributes/sanitized_attribute.rb', line 8 def sanitize(content) Sanitize.clean("<SPURIOUS-TOPLEVEL>" + content + "</SPURIOUS-TOPLEVEL>", sanitize_config).gsub(%r{</?SPURIOUS-TOPLEVEL>}, "") end |