lib/metaractor/parameters.rb in metaractor-1.2.0 vs lib/metaractor/parameters.rb in metaractor-1.2.1

- old
+ new

@@ -6,10 +6,11 @@ include Metaractor::Errors class << self attr_writer :_required_parameters attr_writer :_optional_parameters + attr_writer :_allow_blank end before :remove_blank_values before :validate_required_parameters end @@ -31,10 +32,18 @@ def optional(*params) self._optional_parameters += params end + def _allow_blank + @_allow_blank ||= [] + end + + def allow_blank(*params) + self._allow_blank += params + end + def validate_parameters(*hooks, &block) hooks << block if block hooks.each {|hook| validate_hooks.push(hook) } end @@ -44,9 +53,11 @@ end def remove_blank_values to_delete = [] context.each_pair do |k,v| + next if self.class._allow_blank.include?(k) + # The following regex is borrowed from Rails' String#blank? to_delete << k if (v.is_a?(String) && /\A[[:space:]]*\z/ === v) || v.nil? end to_delete.each do |k| context.delete_field k