From e6e6265e267bc8babf4c7cbf516cfb628a4eee70 Mon Sep 17 00:00:00 2001 From: Torsten Date: Wed, 8 Feb 2023 20:03:20 +0200 Subject: [PATCH] rewrite eval as send, removes parser --- .gitignore | 1 + dummy/app/views/images/show.html.haml | 4 ---- dummy/config/environments/production.rb | 2 +- opal/vue_r.rb | 6 ++---- opal/vue_r/application.rb | 24 ++++++++++++++++++++++++ opal/vue_r/mounter.rb | 6 +++--- 6 files changed, 31 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index 81639b1..5e8466e 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ /dummy/log/*.log /dummy/storage/ /dummy/tmp/ +/dummy/public/assets/ diff --git a/dummy/app/views/images/show.html.haml b/dummy/app/views/images/show.html.haml index 9f9adbf..9928a52 100644 --- a/dummy/app/views/images/show.html.haml +++ b/dummy/app/views/images/show.html.haml @@ -6,7 +6,6 @@ %a.underline{ e_click: "bg_change" , r_class: 'back'} {{ back }} :opal - puts "here1" class Clicker < VueR::Application def bg_change self.state = !self.state @@ -16,8 +15,5 @@ end end - puts "here2" click = Clicker.new(bg: 'bg-cyan-200' , state: true) click.mount("#app") - - puts "here3" diff --git a/dummy/config/environments/production.rb b/dummy/config/environments/production.rb index 8e989b5..b8e74f1 100644 --- a/dummy/config/environments/production.rb +++ b/dummy/config/environments/production.rb @@ -26,7 +26,7 @@ Rails.application.configure do # Compress CSS using a preprocessor. # config.assets.css_compressor = :sass - +# config.assets.js_compressor = :terser # Do not fallback to assets pipeline if a precompiled asset is missed. config.assets.compile = false diff --git a/opal/vue_r.rb b/opal/vue_r.rb index 87a1e98..77eedd7 100644 --- a/opal/vue_r.rb +++ b/opal/vue_r.rb @@ -1,8 +1,6 @@ -require "opal" -require "native" +require 'opal' +require 'native' require 'promise' require 'browser/setup/mini' -require 'opal-parser' - require "vue_r/application" require "vue_r/mounter" diff --git a/opal/vue_r/application.rb b/opal/vue_r/application.rb index b89d570..974d53d 100644 --- a/opal/vue_r/application.rb +++ b/opal/vue_r/application.rb @@ -63,5 +63,29 @@ module VueR effect.call end + def send_self(expression) + method , args = decompose(expression) + puts "Sending #{method}(#{args.join(',')})" + self.send method , *args + end + + def decompose(expression) + matches = expression.match(/\b[^()]+\((.*)\)$/) + return expression if matches.blank? + method = expression.split("(").first + return [method , [] ] if m[1].empty? + args = m[1].split(",").collect{| arg | transform_arg(arg) } + return [method , args] + end + + def transform_arg(arg) + arg = arg.strip + puts "Arg:#{arg}:" + return true if arg == "true" + return false if arg == "false" + return nil if arg == "nil" + return arg.to_i if arg == arg.to_to_s + arg + end end end diff --git a/opal/vue_r/mounter.rb b/opal/vue_r/mounter.rb index 398effd..6163122 100644 --- a/opal/vue_r/mounter.rb +++ b/opal/vue_r/mounter.rb @@ -21,7 +21,7 @@ module VueR ruby = match[0][2 ... -2] puts "Text: #{ruby}" proc = Proc.new do - elem.text = str_before + @application.eval(ruby).to_s + str_after + elem.text = str_before + @application.send_self(ruby).to_s + str_after end @application.watch_effect(proc) end @@ -43,7 +43,7 @@ module VueR old_value = element[native_name] puts "Attribute: #{ruby}" proc = Proc.new do - element[native_name] = old_value + " " + @application.eval(ruby).to_s + element[native_name] = old_value + " " + @application.send_self(ruby).to_s end @application.watch_effect(proc) end @@ -54,7 +54,7 @@ module VueR ruby = element[name] puts "Event: #{native_name}:#{ruby}" element.on!(native_name) do - got = @application.eval(ruby) + got = @application.send_self(ruby) puts "Clicked #{got}" end end