Sha256: 164c7120df7491ae7e2fb20b22bdc9ada22d42e980e1ed90da6afc1faed384ba

Contents?: true

Size: 1.03 KB

Versions: 2

Compression:

Stored size: 1.03 KB

Contents

class Jcompiler::Provider
  def initialize
    @context = nil
  end

  def object!(arg_src = nil, &block)
    pre_context = @context
    @context = []
    srcs, subst_src = if arg_src
                       [block.call(nil), "#{block.parameters[0][1]}=(#{arg_src});"]
                     else
                       [block.call, '']
                     end
    @context = pre_context
    %{"{\#{#{subst_src}[#{srcs.join(',')}].join(',')}}"}
  end

  def array!(arg_src, &block)
    item_name = block.parameters[0][1].to_s
    src = block.call(nil)
    %{"[\#{(#{arg_src}).map { |#{item_name}| #{src} }.join(',')}]"}
  end

  def number!(num)
    %{#{num}.to_json}
  end

  def string!(str)
    %{#{str}.to_json}
  end

  def extract!(obj_name, *key_names)
    key_names.each do |key_name|
      send(key_name, "#{obj_name}.#{key_name}")
    end
  end

  def method_missing(name, *args, &block)
    if block
      @context << %{"\\"#{name}\\":\#{#{block.call}}"}
    else
      @context << %{"\\"#{name}\\":\#{(#{args[0]}).to_json}"}
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
jcompiler-0.1.126 lib/jcompiler/provider.rb
jcompiler-0.1.124 lib/jcompiler/provider.rb