lib/atome/helpers/utilities.rb in atome-0.5.6.1.4 vs lib/atome/helpers/utilities.rb in atome-0.5.6.1.9

- old
+ new

@@ -17,39 +17,42 @@ js_json_msg = json_msg.inspect js_command = "atomeJS.controller_sender(#{js_json_msg})" JS.eval(js_command) end - # def global_monitoring(instance, methods_to_monitor, variables_to_monitor) - # methods_to_monitor.each do |methode| - # original_method = instance.method(methode) - # instance.define_singleton_method(methode) do |*args, &block| - # value_before = instance.instance_variable_get("@#{methode}") - # result = original_method.call(*args, &block) - # value_after = instance.instance_variable_get("@#{methode}") - # if args.empty? - # # "read monitoring: #{methode}" - # elsif value_before != value_after - # affect.each do |targeted_atome| - # # get the content of the affect instance_variable and send it - # # to the affect method to apply the atome to all atome children - # grab(targeted_atome).render(:apply, self) - # end - # end - # result - # end - # end - # variables_to_monitor.each do |var| - # instance.define_singleton_method(var) do - # instance_variable_get("@#{var}") - # end - # instance.define_singleton_method("#{var}=") do |value| - # instance_variable_set("@#{var}", value) - # end - # end - # end + # monitoring system + def monitoring(atomes_to_monitor, particles_to_monitor, &bloc) + atomes_to_monitor.each do |atome_to_monitor| + particles_to_monitor.each do |monitored_particle| + # Store original method + original_method = atome_to_monitor.method(monitored_particle) + # redefine method + atome_to_monitor.define_singleton_method(monitored_particle) do |*args, &proc| + + # Monitor before calling original method + value_before = atome_to_monitor.instance_variable_get("@#{monitored_particle}") + if args.empty? + args = nil + else + if monitored_particle == :touch + instance_variable_set("@#{monitored_particle}", { tap: args[0] }) + instance_variable_set("@#{monitored_particle}_code", { touch: proc }) + + args = { tap: args[0] } + else + instance_variable_set("@#{monitored_particle}", args[0]) + end + args = args[0] + end + instance_exec({ original: value_before, altered: args, particle: monitored_particle }, &bloc) if bloc.is_a?(Proc) + original_method.call(*args) + end + end + end + end + end def help(particle, &doc) if doc Universe.set_help(particle, &doc) @@ -201,43 +204,77 @@ instance_variable_set("@#{element}_code", {}) unless instance_variable_get("@#{element}_code") # TODO : we may have to change this code if we need multiple proc for an particle # FIXME : find a better algorithm can be found to avoid test if option is a Hash Object.attr_accessor "#{element}_code" elem_code = "@#{element}_code" - if params.instance_of? Hash - option_found = params.values[0] - instance_variable_get(elem_code)["#{option_found}_code"] = user_proc - else + # if params.instance_of? Hash + # option_found = params.values[0] + # instance_variable_get(elem_code)["#{option_found}_code"] = user_proc + # else instance_variable_get(elem_code)[element] = user_proc - end - # the commented line below will automatically execute the callback method - # we keep it commented because sometime the execution is conditioned, ex : run callbck on a touch - # send("#{element}_callback") + # end end - # this method is used to automatically create a callback method sufifxed par '_callbback' ex :.shell => shell_callback + # ###################### new 1 + # def store_proc(element, params = true, &user_proc) + # instance_variable_set("@#{element}_code", {}) unless instance_variable_get("@#{element}_code") + # # TODO : we may have to change this code if we need multiple proc for an particle + # # FIXME : find a better algorithm can be found to avoid test if option is a Hash + # Object.attr_accessor "#{element}_code" + # elem_code = "@#{element}_code" + # if params.instance_of? Hash + # option_found = params.values[0] + # puts "#{instance_variable_get(elem_code)["#{option_found}_code"]}" + # proc_stored= instance_variable_get(elem_code)["#{option_found}_code"] = [] + # # proc_stored=[] + # else + # proc_stored= instance_variable_get(elem_code)[element] = [] + # end + # proc_stored << user_proc + # # the commented line below will automatically execute the callback method + # # we keep it commented because sometime the execution is conditioned, ex : run callbck on a touch + # # send("#{element}_callback") + # end + # ##################### new 1 + + # This method is used to automatically create a callback method suffixed by '_callback'. For example: shell => shell_callback. # it can be override if you create a method like: # new({callback: :shell}) do |params, bloc| # # …write what you want … # end def particle_callback(element) - Atome.define_method "#{element}_callback" do - proc_found = instance_variable_get("@#{element}_code")[element] - # instance_exec(@callback[element], proc_found)if proc_found.is_a? Proc - proc_found.call(@callback[element]) if proc_found.is_a? Proc + Atome.define_method("#{element}_callback") do |return_params| + # we test if instance_variable_get("@#{element}_code") is a hash for the can se the particle value is a hash + proc_found = if instance_variable_get("@#{element}_code").instance_of? Hash + # Then we get the first item of the hash because the proc is attached to it + instance_variable_get("@#{element}_code").values.first + # instance_exec(@callback[element], proc_found)if proc_found.is_a? Proc + else + instance_variable_get("@#{element}_code")[element] + # instance_exec(@callback[element], proc_found)if proc_found.is_a? Proc + end + # array_of_proc_found.each do |proc_found| + proc_found.call(return_params) if proc_found.is_a? Proc + # end if array_of_proc_found + + # if array_of_proc_found + # proc_found= array_of_proc_found.shift + # proc_found.call(return_params) if proc_found.is_a? Proc + # end + end end # this method generate the method accessible for end developers # it's the send the method define in "particle_callback" - def call(element) - send("#{element}_callback") + def callback(element, return_params = nil) + send("#{element}_callback", return_params) end - def callback(data) - @callback[data.keys[0]] = data[data.keys[0]] - end + # def callback(data) + # @callback[data.keys[0]] = data[data.keys[0]] + # end def particles(particles_found = nil) if particles_found particles_found.each do |particle_found, value_found| atome[particle_found] = value_found @@ -339,18 +376,40 @@ if server_params @current_server = server_params else @current_server end + end + def server_receiver(params) + # alert params + # alert message_code + calllbacks_found= instance_variable_get('@message_code') + # we delete the default message created by atome + calllbacks_found.delete(:message) + # we get the oldest available callback, to treat it + oldest_callback = calllbacks_found.delete(calllbacks_found.keys.first) + params=params[:return] #TODO : format retrun data correctly instead of this line + oldest_callback.call(params) if oldest_callback.is_a? Proc + # callback(:message, params) end def init_websocket - connection(server) + instance_variable_set('@message_code', {}) + # connection is particle (communication.rb) + connection(@current_server) end - def init_database # this method is call from JS (atome/communication) - message({action: :init_db, value: {atome: {}, particles: {}} }) + + + def encrypt(string) + # if RUBY_ENGINE.downcase == 'opal' || 'wasm32-wasi' + # `sha256(#{string})` + js_code = "sha256('#{string}')" + JS.eval(js_code) + # else + # Digest::SHA256.hexdigest(string) + # end end end