Problem with a lambda not executing immediately

F

Fernando Perez

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 => :eek:gv, :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
 
E

Eric Christopherson

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

=A0has_attached_file :data, :styles =3D> {
=A0 =A0:theora =3D> [:format =3D> :eek:gv, :processors =3D> lambda {|a| a.vi= deo? ?
[: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.
What fails is the lambda, because in Paperclip behind the scenes this is
what happens:

@var =3D 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>)

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

Jesús Gabriel y Galán

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

=A0has_attached_file :data, :styles =3D> {
=A0 =A0:theora =3D> [:format =3D> :eek:gv, :processors =3D> lambda {|a| a.vi= deo? ?
[:video_converter] : []}}]
}

end


What fails is the lambda, because in Paperclip behind the scenes this is
what happens:

@var =3D 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.

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 =3D> {
:theora =3D> [:format =3D> :eek:gv, :processors =3D> [lambda {|a| a.video?
? [:video_converter] : []}]}]
}

Jesus.
 
F

Fernando Perez

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 => :eek:gv, :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".
 
R

Rick DeNatale

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
=A0 has_attached_file :data, :styles =3D> {
=A0 =A0 :theora =3D> [:format =3D> :eek:gv, :processors =3D> [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 =3D> {:theora =3D> {:format =3D> :eek:gv}=
,
:processors =3D> [
# 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 =3D> {:theora =3D> {:format =3D> :eek:gv}=
,
:processors =3D> 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 =3D> {:theora =3D> {:format =3D> :eek:gv}=
,
:processors =3D> 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.

--=20
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
 
F

Fernando Perez

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top