Mailing List Archive

Followup to find_or_create_alternate bug
Hi everybody,

I posted to the users list on Friday about trouble I was having with
automatic thumbnailing. Long story short, mixed-case filenames were
colliding with lowercase URI formats.

Here's that thread, if you're interested:
http://www.gossamer-threads.com/lists/bricolage/users/39880


David came up with a better fix for this bug than the one I submitted,
which is awesome. It raised a question for me, though. This is more a
matter of programming practice than an actual Bricolage question, but I
wonder if somebody could shed a little light.

The code in find_or_create_alternate goes like this:

1. Figure out the URI of the desired thumbnail
2. Search for an existing document with that URI (return it if found)
3. No existing document with that URI. Make a new one and return it

Here's the bit of code that figures out the URI of the desired
thumbnail:

my $uri = do {
# We need to use the same element type and the new file name, so that
# the URI is correct. So we trick get_element_type() to return the
# element type and file name we need. Yeah, it's a hack, but it's the
# cleanest way to do it without creating unnecesary pain.
local $self->{_element_type_object} = $et;
local $self->{file_name} = $alt_fn;
URI::Escape::uri_unescape($self->get_uri($self->get_primary_oc))
};

Basically, it takes the "source" image object, locally changes its
element type and its filename to match the "target," and then it simply
calls get_uri(). Awesome!

Here's my question: why is this a hack? To my ignorant eye, it just
seems clever. What about this code makes its author queasy enough to add
the comment?


Curious and grateful for explanations,

Bret





On Sun, 2011-04-10 at 16:19 -0400, Bret Dawson wrote:
> Hi everybody,
>
> I just figured this out. The inital media document had a mixed-case
> filename, but a lowercase URI format.
>
> So the regex that was trying to change the initial image's URI into the
> thumbnail's URI was failing to find the filename, which left the
> "target" URI the same as the starter one. Then the lookup found the
> image with the desired URI and returned the inital picture.
>
> I've committed a patch and sent a pull request.
>
>
> Cheers,
>
> Bret
>
>
> On Sun, 2011-04-10 at 19:51 +0200, Rolf Schaufelberger wrote:
> > Hi,
> >
> > I just started using this function two days ago and it works fine.
> > May be something wrong with $element_names_and_sizes{$size} ?
> >
> > Am 09.04.2011 um 19:18 schrieb Bret Dawson:
> >
> > > Hi David,
> > >
> > >> Is $pic a auto_generated_image already?
> > >
> > > No, it's just an "image". Do they need to be the same type?
> > >
> > >
> > > Thanks,
> > >
> > > Bret
> > >
> > >
> > >
> > > On Sat, 2011-04-09 at 09:06 -0700, David E. Wheeler wrote:
> > >> On Apr 8, 2011, at 3:10 PM, Bret Dawson wrote:
> > >>
> > >>> Hi everybody,
> > >>>
> > >>> I'm just starting to use the find_or_create_alternate() method to make
> > >>> thumbnails, and I must be doing something wrong. When I try something
> > >>> like this:
> > >>>
> > >>> my $size = 240;
> > >>> my $thumb = $pic->find_or_create_alternate({
> > >>> title_prefix => '',
> > >>> title_suffix => '-' . $size,
> > >>> file_suffix => '_sized_nicely,
> > >>> et_key_name => 'auto_generated_image',
> > >>> relate => 0,
> > >>> transformer => sub {
> > >>> shift->scale( xpixels => $element_names_and_sizes{$size} );
> > >>> },
> > >>> });
> > >>>
> > >>> ...no new media document is created, and the $thumb returned is the
> > >>> original $pic. Any idea what I'm doing incorrectly?
> > >>
> > >
> > >>
> > >> Best,
> > >>
> > >> David
> > >>
> > >>
> > >
> > > --
> > > Bret Dawson
> > > Producer
> > > Pectopah Productions Inc.
> > > (416) 895-7635
> > > bret@pectopah.com
> > > www.pectopah.com
> > >
> >
> > Mit freundlichen Grüßen
> > Rolf Schaufelberger
> > Geschäftsführer
> >
> > plusW GmbH
> > Vorstadtstr. 61 -67 Tel. 07181 47 47 305
> > 73614 Schorndorf Fax. 07181 47 45 344
> >
> > www.plusw.de
> > www.mypixler.com
> > www.calendrino.de
> >
> >
> >
> >
> >
> >
> >
>

--
Bret Dawson
Producer
Pectopah Productions Inc.
(416) 895-7635
bret@pectopah.com
www.pectopah.com
Re: Followup to find_or_create_alternate bug [ In reply to ]
On Apr 12, 2011, at 2:13 PM, Bret Dawson wrote:

> my $uri = do {
> # We need to use the same element type and the new file name, so that
> # the URI is correct. So we trick get_element_type() to return the
> # element type and file name we need. Yeah, it's a hack, but it's the
> # cleanest way to do it without creating unnecesary pain.
> local $self->{_element_type_object} = $et;
> local $self->{file_name} = $alt_fn;
> URI::Escape::uri_unescape($self->get_uri($self->get_primary_oc))
> };
>
> Basically, it takes the "source" image object, locally changes its
> element type and its filename to match the "target," and then it simply
> calls get_uri(). Awesome!
>
> Here's my question: why is this a hack? To my ignorant eye, it just
> seems clever. What about this code makes its author queasy enough to add
> the comment?

Thanks for airing my dirty undewear in public. Sheesh.

It's a hack because I'm violating encapsulation. The image object has a method for the element type and an accessor for the file name. I should not have to know or care that underneath it's implemented as a hash. But I do, and so I exploit that to force it to return different values for those methods, just for that scope. It's a simple hack, very clean, and also very very bad practice.

I'd even call it a form of fuck typing.

http://www.justatheory.com/computers/programming/methodology/fuck-typing.html

A more correct approach would be to construct a new image object, set those values, and then use it for a search. But I was way too lazy to do that.

Best,

David
Re: Followup to find_or_create_alternate bug [ In reply to ]
> Thanks for airing my dirty undewear in public. Sheesh.

Sorry!

On the plus side, I get to spend the evening saying "Fuckin' prosciutto"
to myself.


Cheers,

Bret


> It's a hack because I'm violating encapsulation. The image object has a method for the element type and an accessor for the file name. I should not have to know or care that underneath it's implemented as a hash. But I do, and so I exploit that to force it to return different values for those methods, just for that scope. It's a simple hack, very clean, and also very very bad practice.
>
> I'd even call it a form of fuck typing.
>
> http://www.justatheory.com/computers/programming/methodology/fuck-typing.html
>
> A more correct approach would be to construct a new image object, set those values, and then use it for a search. But I was way too lazy to do that.
>
> Best,
>
> David
>
>

--
Bret Dawson
Producer
Pectopah Productions Inc.
(416) 895-7635
bret@pectopah.com
www.pectopah.com
Re: Followup to find_or_create_alternate bug [ In reply to ]
Zelo zanimiva misel Davida Wheelerja. :)

Zdravko

> I'd even call it a form of fuck typing.
>
> http://www.justatheory.com/computers/programming/methodology/fuck-typing.html
Re: Followup to find_or_create_alternate bug [ In reply to ]
This was mistakenly sent to this list. So, here is what it means:

A very interesting taught by David. :)

Regards, Zdravko.

Zdravko Balorda wrote:
>
> Zelo zanimiva misel Davida Wheelerja. :)
>
> Zdravko
>
>> I'd even call it a form of fuck typing.
>>
>>
>> http://www.justatheory.com/computers/programming/methodology/fuck-typing.html
>>
>
>
Re: Followup to find_or_create_alternate bug [ In reply to ]
On Apr 14, 2011, at 11:32 PM, Zdravko Balorda wrote:

> This was mistakenly sent to this list. So, here is what it means:
>
> A very interesting taught by David. :)
>
> Regards, Zdravko.
>
> Zdravko Balorda wrote:
>> Zelo zanimiva misel Davida Wheelerja. :)

“Davida Wheelerja”? Pardon my ignorance, but what language is this? I don't know that I've seen a name modified like that before. Kinda like "Wheelerja.” :-)

Best,

David
Re: Followup to find_or_create_alternate bug [ In reply to ]
David E. Wheeler wrote:
>>> Zelo zanimiva misel Davida Wheelerja. :)
>
> “Davida Wheelerja”? Pardon my ignorance, but what language is this? I don't know that I've seen a name modified like that before. Kinda like "Wheelerja.” :-)

:) It's slovene. :) We have six cases in our grammar compared
to only two in English, where the genitive, thay say, is not even
a proper genitive. :)

Zdravko
Re: Followup to find_or_create_alternate bug [ In reply to ]
On Apr 17, 2011, at 10:08 PM, Zdravko Balorda wrote:

>>>> Zelo zanimiva misel Davida Wheelerja. :)
>> “Davida Wheelerja”? Pardon my ignorance, but what language is this? I don't know that I've seen a name modified like that before. Kinda like "Wheelerja.” :-)
>
> :) It's slovene. :) We have six cases in our grammar compared
> to only two in English, where the genitive, thay say, is not even
> a proper genitive. :)

Sounds like a fun language. :-)

Best,

David