Class: AutoStripAttributes::Config
- Inherits:
-
Object
- Object
- AutoStripAttributes::Config
- Defined in:
- lib/auto_strip_attributes.rb
Class Attribute Summary collapse
-
.filters ⇒ Object
Returns the value of attribute filters.
-
.filters_enabled ⇒ Object
Returns the value of attribute filters_enabled.
-
.filters_order ⇒ Object
Returns the value of attribute filters_order.
Class Method Summary collapse
Class Attribute Details
.filters ⇒ Object
Returns the value of attribute filters.
43 44 45 |
# File 'lib/auto_strip_attributes.rb', line 43 def filters @filters end |
.filters_enabled ⇒ Object
Returns the value of attribute filters_enabled.
44 45 46 |
# File 'lib/auto_strip_attributes.rb', line 44 def filters_enabled @filters_enabled end |
.filters_order ⇒ Object
Returns the value of attribute filters_order.
45 46 47 |
# File 'lib/auto_strip_attributes.rb', line 45 def filters_order @filters_order end |
Class Method Details
.set_filter(filter, &block) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/auto_strip_attributes.rb', line 81 def self.set_filter(filter, &block) if filter.is_a?(Hash) filter_name = filter.keys.first filter_enabled = filter.values.first else filter_name = filter filter_enabled = false end @filters[filter_name] = block @filters_enabled[filter_name] = filter_enabled # in case filter is redefined, we probably don't want to change the order @filters_order << filter_name if !@filters_order.include?(filter_name) end |
.setup(clear_previous: false, defaults: true, &block) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/auto_strip_attributes.rb', line 48 def self.setup(clear_previous: false, defaults: true, &block) @filters, @filters_enabled, @filters_order = {}, {}, [] if clear_previous @filters ||= {} @filters_enabled ||= {} @filters_order ||= [] if defaults set_filter(convert_non_breaking_spaces: false) do |value| value.respond_to?(:gsub) ? value.gsub("\u00A0", " ") : value end set_filter(strip: true) do |value| value.respond_to?(:strip) ? value.strip : value end set_filter(nullify: true) do |value| # We check for blank? and empty? because rails uses empty? inside blank? # e.g. MiniTest::Mock.new() only responds to .blank? but not empty?, check tests for more info # Basically same as value.blank? ? nil : value (value.respond_to?(:'blank?') and value.respond_to?(:'empty?') and value.blank?) ? nil : value end set_filter(nullify_array: true) {|value| value} set_filter(squish: false) do |value| value = value.respond_to?(:gsub) ? value.gsub(/[[:space:]]+/, ' ') : value value.respond_to?(:strip) ? value.strip : value end set_filter(delete_whitespaces: false) do |value| value.respond_to?(:delete) ? value.delete(" \t") : value end end instance_eval(&block) if block_given? end |