class Jcompiler::Provider def initialize @context = nil end def object!(arg_name = nil, &block) pre_context = @context @context = [] srcs, subst_src = if arg_name [block.call(nil), "#{block.parameters[0][1]}=#{arg_name};"] else [block.call, ''] end @context = pre_context %{"{\#{#{subst_src}[#{srcs.join(',')}].join(',')}}"} end def array!(arg_name, &block) item_name = block.parameters[0][1].to_s src = block.call(nil) %{"[\#{#{arg_name}.map { |#{item_name}| #{src} }.join(',')}]"} end def number!(num) %{"#{num}"} end def method_missing(name, *args, &block) src = if block block.call else args[0] end @context << %{"\\"#{name}\\":\#{ActiveSupport::JSON.encode(#{src})}"} end protected def value! end end