Sha256: 16e3b94a658d44c5a9c04067651eb4af0dd94346923d830a02a0641c7cacf28b

Contents?: true

Size: 747 Bytes

Versions: 1

Compression:

Stored size: 747 Bytes

Contents

require "javascript_template_literals/version"
require "binding_of_caller"

module JavaScriptTemplateLiterals
  class Error < StandardError; end

  def self.interpolate(b, str)
    str.gsub(/\$\{([^\}]*)\}/) { b.eval($1) }
  end

  def self.template_arguments(b, str)
    strings = []
    exps = []
    loop do
      h, _, str = str.partition(/\$\{([^\}]*)\}/)
      strings << h
      break unless $1
      exps << b.eval($1)
    end
    [strings, *exps]
  end

  refine(Object) do
    def `(str)
      b = binding.of_caller(1)
      JavaScriptTemplateLiterals.interpolate(b, str).tap do |ret|
        ret.define_singleton_method(:to_ary) do
          JavaScriptTemplateLiterals.template_arguments(b, str)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
javascript_template_literals-0.1.0 lib/javascript_template_literals.rb