[Backgroundrb-devel] Assigning to results - missing something

skaar skaar at waste.org
Fri Nov 17 11:19:46 EST 2006


* James Hughes (hughes.james at gmail.com) [061117 08:47]:
> Hi,
> 
> I'm trying to convert an app to use 0.2.0, and having a little trouble
> understanding how to use the results hash.
> 
> Formerly my worker had an instance variable, @file_stats which was
> initialized to an empty hash; I have replaced @file_stats with
> results[:file_hash]. Actually my initial pass at this was just to do a
> s/@file_stats/results[:file_hash]/. Which gets me the following:
> 
>         results[:file_stats] = {}
>         logger.debug "   Generating checksum for #{path}..."
>         results[:file_stats][path][:checksum] =
> Digest::MD5.hexdigest(File.read(path))
>         logger.debug "   ...done"
>         logger.debug "    Getting file size for #{path}..."
>         results[:file_stats][path][:size] = File.size(path)
>         logger.debug results[:file_stats][path][:size]

give this a try:
   file_stats = {}
   checksum = Digest::MD5.hexdigest(File.read(path))
   file_stats[path][:checksum] = checksum
   results[:file_stats] = file_stats

It's not quite a regular hash. The results method returns a hash, which
include the current data for the particular worker. We then inject a
singleton override for assignment on the top level hash key, so that
when you do assignment, this will be sent to the results worker and
merged with the results data for the worker.

It might be possible to do the same with values (including sub hashes),
but then you loose a lot of visibility, since objects with singleton
methods don't de/serialize easily - this is why you need to call
#to_hash on results to actually see the data.

We might find a better way to do this in a later release. For now you
just have to live with temporary nested hashes, which you assign
directly to results[:key].

This is documented in the README in trunk.

/skaar

-- 
----------------------------------------------------------------------
|\|\             where in the       |          s_u_b_s_t_r_u_c_t_i_o_n 
| | >===========  W.A.S.T.E.        |                  genarratologies 
|/|/    (_)     is the wisdom       |                  skaar at waste.org
----------------------------------------------------------------------


More information about the Backgroundrb-devel mailing list