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 partial!(obj_name) end def method_missing(name, *args, &block) if block @context << %{"\\"#{name}\\":\#{#{block.call}}"} else @context << %{"\\"#{name}\\":\#{(#{args[0]}).to_json}"} end end end