Sha256: 78fdde5434b332779200bf224cf3276575aea4d52abb1ccc1fb0497c0dc33893

Contents?: true

Size: 838 Bytes

Versions: 1

Compression:

Stored size: 838 Bytes

Contents

module StrawberryCough

  class PathCompiler

    def self.compile(path)
      params  = PathParser.parse_params(path)
      anchors = PathParser.parse_anchors(path)
      js = <<-COMPILED
function (#{args_list(params)}) {
    var url = #{url_concatenation(anchors, params)};
    if (format === undefined) {
      return url;
    } else {
      return url + "." + format;
    }
}
      COMPILED
      js.strip
    end

    private
      def self.url_concatenation(anchors, params)
        params_without_format = params.reject { |p| p == "format" }
        quoted_anchors = anchors.map { |a| "\"#{a}\"" }
        anchors_paired_with_params = quoted_anchors.zip(params_without_format)
        anchors_paired_with_params.flatten.compact.join(" + ")
      end

      def self.args_list(params)
        params.join(", ")
      end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
strawberry_cough-0.1.0 lib/strawberry_cough/path_compiler.rb