lib/atome/helpers/utilities.rb in atome-0.5.2.5 vs lib/atome/helpers/utilities.rb in atome-0.5.2.7

- old
+ new

@@ -26,33 +26,10 @@ def history(property, value) "historize : #{property} #{value}" end - # def broadcasting(modified_particle, value) - # @broadcast.each_value do |particle_monitored| - # if particle_monitored[modified_particle] - # code_found = particle_monitored[modified_particle] - # instance_exec(self, modified_particle, value, &code_found) if code_found.is_a?(Proc) - # end - # end - # end - # - # public - # - # def monitor(params, &proc_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}" - # atome[:monitor] [monitor_id]=params.merge({code: proc_monitoring}) - # params[:particles].each do |targeted_particle| - # target_broadcaster[monitor_id] = { targeted_particle => proc_monitoring } - # end - # end - # end - def broadcasting(altered_particle, value) @broadcast.each_value do |particle_monitored| if particle_monitored[:particles].include?(altered_particle) code_found=particle_monitored[:code] instance_exec(self, altered_particle, value, &code_found) if code_found.is_a?(Proc) @@ -60,25 +37,33 @@ end end public - def monitor(params, &proc_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}" - atome[:monitor] [monitor_id]=params.merge({code: proc_monitoring}) - target_broadcaster[monitor_id] = { particles: params[:particles], code: proc_monitoring } + def monitor(params=nil, &proc_monitoring) + if params + 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}" + atome[:monitor] [monitor_id]=params.merge({code: proc_monitoring}) + target_broadcaster[monitor_id] = { particles: params[:particles], code: proc_monitoring } + end + else + atome[:monitor] end + 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) end + def particles(particles_found = nil) if particles_found particles_found.each do |particle_found, value_found| atome[particle_found] = value_found end @@ -88,9 +73,29 @@ end def <<(particle) value = particle.value real_atome[property] << value + end + + def add_to_hash(particle, values, &user_proc) + # 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 + end + end + + def add_to_array(particle, value, &_user_proc) + # we update the holder of any new particle if user pass a bloc + @atome[particle] << value + end + + def add(particles, &user_proc) + particles.each do |particle, value| + particle_type = Universe.particle_list[particle] + send("add_to_#{particle_type}", particle, value, &user_proc) + end end def refresh collapse end