Problem with a lambda not executing immediately

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 => [:format => :ogv, :processors => lambda {|a| a.video? ?
[: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 #Proc:0x455d864)

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

On Thu, Jul 29, 2010 at 3:38 PM, Fernando P. [email protected]
wrote:

has_attached_file :data, :styles => {
:theora => [:format => :ogv, :processors => lambda {|a| a.video? ?
[:video_converter] : []}}]
}

end

You have an extra } before ]. I’m assuming that wasn’t part of your
actual script, because then you’d get a syntax error.

And I get the following error message:

NoMethodError (undefined method `inject’ for #Proc:0x455d864)

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.

On Thu, Jul 29, 2010 at 10:38 PM, Fernando P. [email protected]
wrote:

has_attached_file :data, :styles => {
@var = style.processors.inject(something) do |file, processor|

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.

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 => [:format => :ogv, :processors => [lambda {|a| a.video?
? [:video_converter] : []}]}]
}

Jesus.

Given that the key in the hash is
:processors (in plural) I would assume that the value should be an
array of processors.

Yes style.processors is expected to be an array

So, even if you only have one lambda, can you try this:

class Asset < ActiveRecord::Base
has_attached_file :data, :styles => {
:theora => [:format => :ogv, :processors => [lambda {|a| a.video?
? [:video_converter] : []}]}]
}

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”.

Here is the correct error message with the latter code:

wrong constant name #Proc:[email protected]:14::App::Models::Asset.rb

Hi Rick,

the :processors option isn’t supposed to be part of the :style

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.

On Fri, Jul 30, 2010 at 5:40 AM, Fernando P. [email protected]
wrote:

:theora => [:format => :ogv, :processors => [lambda {|a| a.video?

? [:video_converter] : []}]}]
}

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”.

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 => {:theora => {:format => :ogv},
: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 => {:theora => {:format => :ogv},
:processors => lambda { | asset | # the
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 => {:theora => {:format => :ogv},
:processors => lambda { | asset |
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.


Rick DeNatale

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs