lib/atome/helpers/utilities.rb in atome-0.5.4.1.9 vs lib/atome/helpers/utilities.rb in atome-0.5.5.6.4
- old
+ new
@@ -1,270 +1,283 @@
# frozen_string_literal: true
# toolbox method here
+require 'json'
class Atome
- private
+ class << self
+ def controller_sender(message)
+ return if $host == :html
+ json_msg = message.to_json
+ js_json_msg = json_msg.inspect
+ js_command = "atomeJS.controller_sender(#{js_json_msg})"
+ JS.eval(js_command)
+ end
- # local server messaging
- def self.controller_sender(message)
- return if $host == :browser
+ # 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
- json_msg = message.to_json
- atome_js.JS.controller_sender(json_msg)
-
end
+ # local server messaging
+
def response_listener(hashed_msg)
js_action = hashed_msg.JS[:action]
js_body = hashed_msg.JS[:body]
send(js_action, js_body)
end
def self.controller_listener
+ return if $host == :html
- return if $host == :browser
-
- atome_js.JS.controller_listener()
-
+ atome_js.JS.controller_listener() # js folder atome/helipers/atome/communication
end
- # def self.mode=(val)
- # @atome_mode=val
- # end
- #
- # def self.mode
- # "@atome_mode"
- # end
-
- def collapse
- @atome.each do |element, value|
+ def collapse(new_atome)
+ # TODO : try to optimise the two lines below to avoid conditions
+ new_atome.each do |element, value|
send(element, value)
end
end
- def security_pass(_element, _value)
- true
+ def particle_main(element, params, &user_proc)
+ # TODO : optimise below removing all conditions if possible
+ if Atome.instance_variable_get("@main_#{element}").is_a?(Proc) # post is before rendering and broadcasting
+ result = instance_exec(params, user_proc, self, &Atome.instance_variable_get("@main_#{element}"))
+ params = result if result && !result.instance_of?(Atome)
+ end
+ params
end
- def sanitize(element, params, &user_proc)
+ def particle_read(element, params, &user_proc)
+ if Atome.instance_variable_get("@read_#{element}").is_a?(Proc) # post is before rendering and broadcasting
+ params = instance_exec(params, user_proc, self, &Atome.instance_variable_get("@read_#{element}"))
+ end
+ params
+ end
+
+ def particle_sanitizer(element, params, &user_proc)
+
bloc_found = Universe.get_sanitizer_method(element)
+ # sanitizer occurs before any treatment
+ # it's call at the very start when a new atome is created : in genesis.rb /new_atome
+ # it's also call when creating a new particle in genesis/ new_particle befre creating the particle
+ # and also before creating additional method in genesis/ additional_particle_methods
params = instance_exec(params, user_proc, &bloc_found) if bloc_found.is_a?(Proc)
params
end
- def history(property, value)
- "historize : #{property} #{value}"
+ def particle_pre(element, params, &user_proc)
+ if Atome.instance_variable_get("@pre_#{element}").is_a?(Proc) # post is before rendering and broadcasting
+ params = instance_exec(params, user_proc, self, &Atome.instance_variable_get("@pre_#{element}"))
+ end
+ params
end
- def broadcasting(element)
- params = instance_variable_get("@#{element}")
- @broadcast.each_value do |particle_monitored|
- if particle_monitored[:particles].include?(element)
- code_found = particle_monitored[:code]
- instance_exec(self, element, params, &code_found) if code_found.is_a?(Proc)
- end
+ def particle_post(element, params, &user_proc)
+ if Atome.instance_variable_get("@post_#{element}").is_a?(Proc) # post is after rendering and broadcasting
+ params = instance_exec(params, user_proc, self, &Atome.instance_variable_get("@post_#{element}"))
end
+ params
end
- public
-
- def monitor(params = nil, &proc_monitoring)
- if params
- monitoring = atome[:monitor] ||= {}
- params[:atomes].each do |atome_id|
- target_broadcaster = grab(atome_id).instance_variable_get('@broadcast')
- monitor_id = params[:id] || "monitor#{target_broadcaster.length}"
- monitoring[monitor_id] = params.merge({ code: proc_monitoring })
- target_broadcaster[monitor_id] = { particles: params[:particles], code: proc_monitoring }
- end
- else
- atome[:monitor]
+ def particle_after(element, params, &user_proc)
+ if Atome.instance_variable_get("@after_#{element}").is_a?(Proc) # after is post saving
+ params = instance_exec(params, user_proc, self, &Atome.instance_variable_get("@after_#{element}"))
end
-
+ params
end
- def store_code_bloc(element, &user_proc)
- # TODO : maybe we have to change tis code if we need multiple proc for an particle
- Object.attr_accessor "#{element}_code"
-
- instance_variable_set("@#{element}_code", user_proc)
+ def atome_pre_process(element, params, &user_proc)
+ if Atome.instance_variable_get("@pre_#{element}").is_a?(Proc)
+ params = instance_exec(params, self, user_proc, &Atome.instance_variable_get("@pre_#{element}"))
+ end
+ params
end
- def particles(particles_found = nil)
- if particles_found
- particles_found.each do |particle_found, value_found|
- atome[particle_found] = value_found
- end
- else
- atome
- end
+ def atome_sanitizer(element, params, &user_proc)
+ # Attention: the method is the same as the one used for the partcicle
+ particle_sanitizer(element, params)
end
- def <<(particle)
+ def atome_post_process(element, params, new_atome, &user_proc)
- real_atome[property] << particle
- end
+ return unless Atome.instance_variable_get("@post_#{element}").is_a?(Proc)
+ new_atome.instance_exec(params, user_proc, &Atome.instance_variable_get("@post_#{element}"))
- def add_to_integer(_atome_found, _particle_found, &_user_proc)
- puts "there's no interest to add anything to an integer!"
end
- def add_to_float(_atome_found, _particle_found, &_user_proc)
- puts "there's no interest to add anything to an integer!"
- end
+ def atome_processor(element, params, &user_proc)
+ # TODO: replace with the line below but need extensive testing as it crash some demos ex: animation
+ params = atome_common(element, params)
- def add_to_bignum(_atome_found, _particle_found, &_user_proc)
- puts "there's no interest to add anything to an integer!"
- end
+ atome_pre_process(element, params, &user_proc)
- def add_to_string(_atome_found, _particle_found, &_user_proc)
- puts "there's no interest to add anything to an string!"
+ new_atome = send("set_#{element}", params, &user_proc) # it call Atome.define_method "set_#{element}" in new_atome method
+ # TODO : check if we don't have a security issue allowing atome modification after creation
+ # if we have one find another solution the keep this facility
+ atome_post_process(element, params, new_atome, &user_proc)
+
+ new_atome
end
- def add_to_hash(particle, values, &user_proc)
- @atome[:add][particle] = true
- # we update the holder of any new particle if user pass a bloc
- store_code_bloc(particle, &user_proc) if user_proc
- values.each do |value_id, value|
- @atome[particle][value_id] = value
+ def store(params)
+ params.each do |particle_to_save, data|
+ # @!atome[particle_to_save]=data
+ # instance_variable_set(particle_to_save,data)
end
- end
- def add_to_array(particle, value, &_user_proc)
- @atome[:add][particle] = true
- # we update the holder of any new particle if user pass a bloc
- @atome[particle] << value
end
- def add_to_atome(atome_type, particle_found, &user_proc)
- # puts "-----> atome_type : #{atome_type}, particle_found : #{particle_found}"
- # @atome[:add] = [] unless @atome[:add]
- @atome[:add][atome_type] = particle_found
- send(atome_type, particle_found, &user_proc)
+ def history(filter = {})
+ filter[:id] = @id
+ Universe.story(filter)
end
- def add(particles, &user_proc)
+ # def broadcasting(element)
+ # params = instance_variable_get("@#{element}")
+ # @broadcast.each_value do |particle_monitored|
+ # if particle_monitored[:particles].include?(element)
+ # code_found = particle_monitored[:code]
+ # instance_exec(self, element, params, &code_found) if code_found.is_a?(Proc)
+ # end
+ # end
+ # end
- @atome[:add] = {} unless @atome[:add]
- particles.each do |particle, value|
- particle_type = Universe.particle_list[particle] || 'atome'
- send("add_to_#{particle_type}", particle, value, &user_proc)
- # now we remove systematically the added hash so next particle won't be automatically added
- @atome[:add].delete(particle)
+ 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]
+ 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
- def substract(_particles, &_user_proc)
- # TODO : write code here to remove add elements"
- puts "write code here to remove add elements"
- # @atome[:add]=:poi
- # particles.each do |particle, value|
- # particle_type = Universe.particle_list[particle] || 'atome'
- # puts "<<<<<< this the place to b ....>>>>>>#{particles} #{particle_type}"
- # send("add_to_#{particle_type}", particle, value, &user_proc)
- # end
+ # this method is used to automatically create a callback method sufifxed par '_callbback' ex :.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
+ end
end
- def refresh
- collapse
+ # 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")
end
- # def collector(params = {}, &bloc)
- # atome_type = :collector
- # # generated_render = params[:renderers] || []
- # # generated_id = params[:id] || identity_generator(atome_type)
- # #
- # # generated_parents = params[:parents] || [id]
- # params = atome_common(atome_type, params)
- # Batch.new({ atome_type => params }, &bloc)
- # end
+ def callback(data)
+ @callback[data.keys[0]] = data[data.keys[0]]
+ end
- def each(&proc)
- value.each do |val|
- instance_exec(val, &proc) if proc.is_a?(Proc)
+ def particles(particles_found = nil)
+ if particles_found
+ particles_found.each do |particle_found, value_found|
+ atome[particle_found] = value_found
+ end
+ else
+ atome
end
end
- def include?(value)
- self.include?(value)
+ def atome
+ # allow to get all atomes instance variables available as a Hash
+ instance_variables.each_with_object({}) do |var, hash|
+ hash[var[1..-1].to_sym] = instance_variable_get(var) # var[1..-1] enlève le '@' au début
+ end
end
- def each_with_index(*args, &block)
- value.each_with_index(&block)
+ def to_hash
+ hash = {}
+ instance_variables.each do |var|
+ next if %i[@html_object @history].include?(var)
+
+ hash[var.to_s.delete('@').to_sym] = instance_variable_get(var)
+ end
+ hash
end
- def [](range)
- if instance_of?(Atome)
- value[range]
- # elsif value[range].instance_of?(Atome)
- # return value[range]
- elsif value[range].instance_of?(Array)
- # collector_object = Object.collector({})
- # collected_atomes = []
- # value[range].each do |atome_found|
- # collected_atomes << atome_found
- # end
- # collector_object.data(collected_atomes)
- Batch.new(value[range])
- # return collector_object
+ def refresh
+ particles_found = to_hash
+ particles_found.each do |particle_found, value_found|
+ send(particle_found, value_found)
end
end
- def []=(params, value)
- # TODO : it may miss some code, see above
- self[params] = value
- end
-
- def set(params)
- params.each do |particle, value|
- send(particle, value)
+ def each(&proc)
+ collect.each do |val|
+ instance_exec(val, &proc) if proc.is_a?(Proc)
end
end
- def particle_to_remove_decision(particle_to_remove)
- if particle_to_remove.instance_of? Hash
- particle_to_remove.each do |particle_found, value|
- send("remove_#{particle_found}", value)
- end
- else
- send(particle_to_remove, 0)
- end
+ def <<(item)
+ collect << item
end
- def materials
- # TODO: the code below need a rewrite, we must find a new algorithm to avoid all those conditions
- images_found = atome[:image] || []
- videos_found = atome[:video] || []
- shapes_found = atome[:shape] || []
- web_found = atome[:web] || []
- texts_found = atome[:text] || []
- images_found.concat(videos_found).concat(shapes_found).concat(web_found).concat(texts_found)
+ def include?(value)
+ include?(value)
end
-
- def physical
- types=[:text, :image, :video, :shape, :web]
- atomes_found=[]
- types.each do |type|
- ids_found = self.send(type)
- next unless ids_found
-
- ids_found.each do |id_found|
- atomes_found << id_found
- end
+ def set(params)
+ params.each do |particle, value|
+ send(particle, value)
end
- atomes_found
end
def detach_atome(atome_id_to_detach)
atome_to_detach = grab(atome_id_to_detach)
# TODO: remove the condition below and find why it try to detach an atome that doesn't exist
- return unless atome_to_detach
- atome_type_found = atome_to_detach.atome[:type]
- atome_id_found = atome_to_detach.atome[:id]
- @atome[atome_type_found].delete(atome_id_found)
- @atome[:attached].delete(atome_id_to_detach)
+ nil unless atome_to_detach
+ end
+ def debug(msg)
+ puts msg
end
+ def set_current_user(user_id)
+ if Universe.users[user_id]
+ Universe.current_user = user_id
+ else
+ debug "#{user_id} not found"
+ end
+ end
end