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.
26 27 28 |
# File 'lib/auto_strip_attributes.rb', line 26 def filters @filters end |
.filters_enabled ⇒ Object
Returns the value of attribute filters_enabled.
27 28 29 |
# File 'lib/auto_strip_attributes.rb', line 27 def filters_enabled @filters_enabled end |
.filters_order ⇒ Object
Returns the value of attribute filters_order.
28 29 30 |
# File 'lib/auto_strip_attributes.rb', line 28 def filters_order @filters_order end |
Class Method Details
.set_filter(filter, &block) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/auto_strip_attributes.rb', line 62 def self.set_filter(filter,&block) if filter.is_a?(Hash) then 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 unless @filters_order.include? filter_name end |
.setup(user_options = nil, &block) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/auto_strip_attributes.rb', line 31 def self.setup(=nil,&block) = { :clear => false, :defaults => true, } = .merge if .is_a?(Hash) @filters, @filters_enabled, @filters_order = {}, {}, [] if [:clear] @filters ||= {} @filters_enabled ||= {} @filters_order ||= [] if [:defaults] 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 :squish => false do |value| value.respond_to?(:gsub) ? value.gsub(/\s+/, ' ') : value end end instance_eval &block if block_given? end |