J
johanatan
hi All,
I posted this message to the Ruby on Rails group yesterday, but I
suppose it belongs here instead.
I've been trying to wrestle dynamic code generation into place for a
week now with no success. What I want is a module or class (started
trying to use module but finished up trying to use a class) which can
inject a method into its consumer which will allow:
user.image = #<Ruby:File>
as you would get from a file upload. (This eliminates any need for
special image handling code in the view or controller and lets the image
field act like any other). It uses the RMagick library to convert all
images to jpg's and resize them to a consumer-defined size. What I was
hoping the group could help with was moving all the code out of the
model and into my ImageBlob class (which will inject necessary code into
the model (i.e., consumer)). Here's what I currently have (which
works):
model.rb
----------
def image=( file )
write_attribute( 'image', ImageBlob.getBlob( file, 225 ))
end
def thumbnailImage=( file )
write_attribute( 'thumbnailImage', ImageBlob.getBlob( file, 125
))
end
ImageBlob.rb
------------
require 'RMagick'
include Magick
class ImageBlob
def ImageBlob.getBlob( file, maxImageSize )
#omitted code which resizes and reformats image and returns blob
end
end
I was hoping to have an initialize function in ImageBlob which would
take the consumer class and inject the necessary methods into it (image=
and thumbnailImage=). Here's my best attempt at that (which fails for
an unknown reason):
def initialize( attributesAndSizesHash, consumerClass )
attributesAndSizesHash.each_pair {|attributeName, maxImageSize|
code = "class #{consumerClass}
def #{attributeName}=( file )
write_attribute( '#{attributeName}', ImageBlob.getBlob(
file, #{maxImageSize} ))
end
end"
eval( code ) }
end
I also tried making 'code' define a module and calling
consumerClass.extend( moduleName ). Neither of these approaches are
working.
Of course, if this implemention could work, then consumers of the
ImageBlob class could simply call:
ImageBlob.new( { "image" => 225, "thumbnailImage" => 125 }, self )
in their class definition to have the functionality added (with
any number of image fields each having their own size).
Can anyone out there provide the last piece of this puzzle?
Thanks much,
Jonathan
I posted this message to the Ruby on Rails group yesterday, but I
suppose it belongs here instead.
I've been trying to wrestle dynamic code generation into place for a
week now with no success. What I want is a module or class (started
trying to use module but finished up trying to use a class) which can
inject a method into its consumer which will allow:
user.image = #<Ruby:File>
as you would get from a file upload. (This eliminates any need for
special image handling code in the view or controller and lets the image
field act like any other). It uses the RMagick library to convert all
images to jpg's and resize them to a consumer-defined size. What I was
hoping the group could help with was moving all the code out of the
model and into my ImageBlob class (which will inject necessary code into
the model (i.e., consumer)). Here's what I currently have (which
works):
model.rb
----------
def image=( file )
write_attribute( 'image', ImageBlob.getBlob( file, 225 ))
end
def thumbnailImage=( file )
write_attribute( 'thumbnailImage', ImageBlob.getBlob( file, 125
))
end
ImageBlob.rb
------------
require 'RMagick'
include Magick
class ImageBlob
def ImageBlob.getBlob( file, maxImageSize )
#omitted code which resizes and reformats image and returns blob
end
end
I was hoping to have an initialize function in ImageBlob which would
take the consumer class and inject the necessary methods into it (image=
and thumbnailImage=). Here's my best attempt at that (which fails for
an unknown reason):
def initialize( attributesAndSizesHash, consumerClass )
attributesAndSizesHash.each_pair {|attributeName, maxImageSize|
code = "class #{consumerClass}
def #{attributeName}=( file )
write_attribute( '#{attributeName}', ImageBlob.getBlob(
file, #{maxImageSize} ))
end
end"
eval( code ) }
end
I also tried making 'code' define a module and calling
consumerClass.extend( moduleName ). Neither of these approaches are
working.
Of course, if this implemention could work, then consumers of the
ImageBlob class could simply call:
ImageBlob.new( { "image" => 225, "thumbnailImage" => 125 }, self )
in their class definition to have the functionality added (with
any number of image fields each having their own size).
Can anyone out there provide the last piece of this puzzle?
Thanks much,
Jonathan