def transform_partial_capture(mapping, capture, processor = nil,
                                  normalize_values = true)
      _, operator, varlist = *capture.match(EXPRESSION)

      vars = varlist.split(",")

      if operator == "?"
        # partial expansion of form style query variables sometimes requires a
        # slight reordering of the variables to produce a valid url.
        first_to_expand = vars.find { |varspec|
          _, name, _ =  *varspec.match(VARSPEC)
          mapping.key?(name) && !mapping[name].nil?
        }

        vars = [first_to_expand] + vars.reject {|varspec| varspec == first_to_expand}  if first_to_expand
      end

      vars.
        inject("".dup) do |acc, varspec|
          _, name, _ =  *varspec.match(VARSPEC)
          next_val = if mapping.key? name
                       transform_capture(mapping, "{#{operator}#{varspec}}",
                                         processor, normalize_values)
                     else
                       "{#{operator}#{varspec}}"
                     end
          # If we've already expanded at least one '?' operator with non-empty
          # value, change to '&'
          operator = "&" if (operator == "?") && (next_val != "")
          acc << next_val
      end
    end
def transform_partial_capture(mapping, capture, processor = nil,
                                  normalize_values = true)
      _, operator, varlist = *capture.match(EXPRESSION)

      vars = varlist.split(",")

      if operator == "?"
        # partial expansion of form style query variables sometimes requires a
        # slight reordering of the variables to produce a valid url.
        first_to_expand = vars.find { |varspec|
          _, name, _ =  *varspec.match(VARSPEC)
          mapping.key?(name) && !mapping[name].nil?
        }

        vars = [first_to_expand] + vars.reject {|varspec| varspec == first_to_expand}  if first_to_expand
      end

      vars.
        inject("".dup) do |acc, varspec|
          _, name, _ =  *varspec.match(VARSPEC)
          next_val = if mapping.key? name
                       transform_capture(mapping, "{#{operator}#{varspec}}",
                                         processor, normalize_values)
                     else
                       "{#{operator}#{varspec}}"
                     end
          # If we've already expanded at least one '?' operator with non-empty
          # value, change to '&'
          operator = "&" if (operator == "?") && (next_val != "")
          acc << next_val
      end
    end
Loops through each capture and expands any values available in mapping

 @param [Hash] mapping
   Set of keys to expand
 @param [String] capture
   The expression to expand
 @param [#validate, #transform] processor
   An optional processor object may be supplied.
 @param [Boolean] normalize_values
   Optional flag to enable/disable unicode normalization. Default: true

 The object should respond to either the <tt>validate</tt> or
 <tt>transform</tt> messages or both. Both the <tt>validate</tt> and
 <tt>transform</tt> methods should take two parameters: <tt>name</tt> and
 <tt>value</tt>. The <tt>validate</tt> method should return <tt>true</tt>
 or <tt>false</tt>; <tt>true</tt> if the value of the variable is valid,
 <tt>false</tt> otherwise. An <tt>InvalidTemplateValueError</tt> exception
 will be raised if the value is invalid. The <tt>transform</tt> method
 should return the transformed variable value as a <tt>String</tt>. If a
 <tt>transform</tt> method is used, the value will not be percent encoded
 automatically. Unicode normalization will be performed both before and
 after sending the value to the transform method.

 @return [String] The expanded expression