Image uploader and manager I have recently started using Ruby and would like to hear your take on the following piece of code.

class Generator

  def initialize
    @context = nil
  end

  def start(params)
    @context = Context.new params
    image = create_image
    if cache_update_request?
      upload image
    end
    return image
  end

  def create_image
    composer = Composer.new @context
    execution_context = ExecutionContext.new(@context, composer)
    execution_context.render
  end

  def upload(image)
    uploader = CompositorCommons::CacheUpdater.new
    filename = @context.parameters[:cache]
    location = 'dual/' + filname
    type = 'image/jpeg'
    uploader.upload(image,location,type)
  end

  def cache_update_request?
    @context.parameters.has_key?(:cache)
  end

end
Your code is nice overall, but here are my two cents : 


initializing instance variables before using them is not mandatory, so initialize as you implemented it is useless. A call to @context if it is not initialized will return nil.
Since uninitialized instance variables return nil, you may have have problem when doing things like @context.parameters[:cache]. As such, i would advise to initialize them... in initialize :

class Generator
  def initialize( params )
    @context = Context.new( params )
  end
end



This also leaves you the latitude to inspect the params and throw an ArgumentError if some usefull params are not present.


I think your object has mixed responsibilities. What does it represent ? It is not clear at first glance. Is it an uploader, an image ? It looks like a Service to me (the process is stateless), or maybe a "context" in DCI slang. Maybe your structure could more accurately represent this :

module ImageGeneratorService

  def self.start( params )
    cache = params.delete( :cache )
    image = ImageFactory.create_from_params( params )
    CacheService.cache_image( image, cache ) if cache
    image
  end

end



... ok, it's a bit too much, but i think you get the spirit. YMMV