class Addressable::Template

def expand_join_operator(argument, variables, mapping, partial=false)

Returns:
  • (String) - The expanded result.

Parameters:
  • mapping (Hash) -- The mapping of variables to values.
  • variables (Array) -- The variables the operator is working on.
  • argument (String) -- The argument to the operator.
def expand_join_operator(argument, variables, mapping, partial=false)
  if !partial
    variable_values = variables.inject([]) do |accu, variable|
      if !mapping[variable].kind_of?(Array)
        if mapping[variable]
          accu << variable + "=" + (mapping[variable])
        end
      else
        raise InvalidTemplateOperatorError,
          "Template operator 'join' does not accept Array values."
      end
      accu
    end
    variable_values.join(argument)
  else
    buffer = ""
    state = :suffix
    variables.each_with_index do |variable, index|
      if !mapping[variable].kind_of?(Array)
        if mapping[variable]
          if buffer.empty? || buffer[-1..-1] == "}"
            buffer << (variable + "=" + (mapping[variable]))
          elsif state == :suffix
            buffer << argument
            buffer << (variable + "=" + (mapping[variable]))
          else
            buffer << (variable + "=" + (mapping[variable]))
          end
        else
          if !buffer.empty? && (buffer[-1..-1] != "}" || state == :prefix)
            buffer << "{-opt|#{argument}|#{variable}}"
            state = :prefix
          end
          if buffer.empty? && variables.size == 1
            # Evaluates back to itself
            buffer << "{-join|#{argument}|#{variable}}"
          else
            buffer << "{-prefix|#{variable}=|#{variable}}"
          end
          if (index != (variables.size - 1) && state == :suffix)
            buffer << "{-opt|#{argument}|#{variable}}"
          elsif index != (variables.size - 1) &&
              mapping[variables[index + 1]]
            buffer << argument
            state = :prefix
          end
        end
      else
        raise InvalidTemplateOperatorError,
          "Template operator 'join' does not accept Array values."
      end
    end
    buffer
  end
end