Module: AttributeSanitizer::DSL
- Included in:
- AttributeSanitizer
- Defined in:
- lib/attribute_sanitizer_dsl.rb
Instance Method Summary collapse
-
#add_step(method = nil, &block) ⇒ Object
Adds a step to the sanitization routine.
-
#ensure_array(field) ⇒ Object
Adds a step that ensures that a hash value is an array.
-
#remap(mapping) ⇒ Object
Adds a step that renames a hash key.
-
#sanitize_has_many(association_name) ⇒ Object
Adds several steps that can be useful when dealing with accepts_nested_attributes_for in Rails projects.
-
#sanitize_nested_attributes(mapping) ⇒ Object
Adds a step that iterates over items in array and calls sanitize_attributes using the specified class.
Instance Method Details
#add_step(method = nil, &block) ⇒ Object
Adds a step to the sanitization routine.
If passed a block, that block is added as a step.
If passed a symbol or string, then a custom step will be added that calls that method on the method_delegate (typically your class).
13 14 15 16 17 18 19 |
# File 'lib/attribute_sanitizer_dsl.rb', line 13 def add_step(method=nil, &block) if block_given? sanitization_steps << block elsif method add_step { |attrs| method_delegate.send(method, attrs) } end end |
#ensure_array(field) ⇒ Object
Adds a step that ensures that a hash value is an array
42 43 44 45 46 47 48 49 |
# File 'lib/attribute_sanitizer_dsl.rb', line 42 def ensure_array(field) add_step do |attrs| if attrs.has_key?(field) && !attrs[field].is_a?(Array) attrs[field] = [attrs[field]] end attrs end end |
#remap(mapping) ⇒ Object
Adds a step that renames a hash key.
27 28 29 30 31 32 33 34 |
# File 'lib/attribute_sanitizer_dsl.rb', line 27 def remap(mapping) from = mapping.keys.first to = mapping[from] add_step do |attrs| attrs[to] = attrs.delete(from) if attrs.has_key?(from) attrs end end |
#sanitize_has_many(association_name) ⇒ Object
Adds several steps that can be useful when dealing with accepts_nested_attributes_for in Rails projects.
This is equivalent to:
remap :bagel => :bagels_attributes
remap :bagels => :bagels_attributes
ensure_array :bagels_attributes
sanitize_nested_attributes :bagels_attributes => Bagel
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/attribute_sanitizer_dsl.rb', line 83 def sanitize_has_many(association_name) association_name = association_name.to_s singular_name = association_name.singularize nested_name = "#{association_name}_attributes" klass = association_name.singularize.camelize.constantize remap singular_name => nested_name remap association_name => nested_name ensure_array nested_name sanitize_nested_attributes nested_name => klass end |
#sanitize_nested_attributes(mapping) ⇒ Object
Adds a step that iterates over items in array and calls sanitize_attributes using the specified class.
58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/attribute_sanitizer_dsl.rb', line 58 def sanitize_nested_attributes(mapping) key = mapping.keys.first klass = mapping[key] add_step do |attrs| if attrs.has_key?(key) attrs[key].compact! attrs[key].map! { |val| klass.sanitize_attributes(val) } end attrs end end |