Problem with a lambda not executing immediately

by Fernando Perezon 2010-07-29T20:38:34+00:00
Hi,
I'm using the paperclip plugin for one of my rails app, but my problem
is pure Ruby. I'm not good enough with Ruby and lambdas so I need your
advice.
Here is the code I am trying to make work:
class Asset < ActiveRecord::Base
has-attached-file :data, :styles = :theora =[:video-converter] : []}}]
}
end
What fails is the lambda, because in Paperclip behind the scenes this is
what happens:
@var = style.processors.inject(something) do |file, processor|
...
end
style.processors has received the lambda defined earlier.
And I get the following error message:
NoMethodError (undefined method `inject' for #)
So how can I fix it, and after that which book or resources can I read
to get proficient in such dynamic coding practice? Currently in use
blocks all the time ([array].each { ...}) without really mastering them.
Thanks for your support

Re: Problem with a lambda not executing immediately

by Eric Christophersonon 2010-07-29T23:03:05+00:00.
On Thu, Jul 29, 2010 at 3:38 PM, Fernando Perez wrote:
You have an extra } before ]. I'm assuming that wasn't part of your
actual script, because then you'd get a syntax error.
It looks like style.processors is itself a proc instead of an
enumerable. Try to find where it was assigned and make sure you pass
the proc inside of an array.

Re: Problem with a lambda not executing immediately

by Jesús Gabriel y Galánon 2010-07-30T06:20:44+00:00.
On Thu, Jul 29, 2010 at 10:38 PM, Fernando Perez wrote:
I have no idea about paperclip, but looking at the above snippets
style.processor should be an Enumerable (which is where inject is
defined), for example an array. Given that the key in the hash is
:processors (in plural) I would assume that the value should be an
array of processors. So, even if you only have one lambda, can you try
this:
class Asset < ActiveRecord::Base
has-attached-file :data, :styles = :theora =? [:video-converter] : []}]}]
}
Jesus.

Re: Problem with a lambda not executing immediately

by Fernando Perezon 2010-07-30T09:40:56+00:00.

Yes style.processors is expected to be an array
Unfortunately it doesn't work, as when the lambda gets executed, the "a"
object it receives is not the not it expects: "wrong constant name
passed".

Re: Problem with a lambda not executing immediately

by Rick DeNataleon 2010-07-31T14:55:35+00:00.
On Fri, Jul 30, 2010 at 5:40 AM, Fernando Perez wrote:
I suspect that you would get better answers on the rails forum since
paperclip is better known there than in the general Ruby community.
but..
First, the :processors option isn't supposed to be part of the :style hash
has-attached-file :date,
:styles = :processors = # something goes here
]
Now it appears you are trying to use a feature of paperclip which
doesn't seem to be documented, normally the value for the :processors
option is just an array of symbols which are converted to an instance
of a subclass of Paperclip::Processor. But from reading the code it
appears that you can also do:
class Asset < ActiveRecord::Base
has-attached-file :date,
:styles = :processors =instance of the Asset model
asset.video? ? [:video-converter] : []
}
end
I'd probably refactor this a bit:
class Asset < ActiveRecord::Base
has-attached-file :date,
:styles = :processors =asset.processors }
def processors
self.video? ? [:video-converter] : []
end
end
I've tested none of this so take it with the appropriate grain of salt.
Blog: http://talklikeaduck.denhaven2.com/
Github: http://github.com/rubyredrick
Twitter: @RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

Re: Problem with a lambda not executing immediately

by Fernando Perezon 2010-08-01T09:26:28+00:00.
Hi Rick,
Yes it can. It's undocumented, I have tried and it works, but only if I
hardcode the processors array. Using the lambda doesn't work for
processors defined for specific styles.
I'll read the pickaxe to improve my ruby skills.