<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>Martian Intuition - of Mogrify, Ruby Tempfile &amp; Dynamic Class Definitions Comments</title>
  <id>tag:aki.marsorange.com,2011:/archives/of-mogrify-ruby-tempfile-dynamic-class-definitions/comments</id>
  <generator uri="http://mephistoblog.com" version="0.8.0">Mephisto Drax</generator>
  <link href="http://aki.marsorange.com/archives/of-mogrify-ruby-tempfile-dynamic-class-definitions/comments.xml" rel="self" type="application/atom+xml"/>
  <link href="/archives/of-mogrify-ruby-tempfile-dynamic-class-definitions" rel="alternate" type="text/html"/>
  <updated>2011-10-09T03:28:09Z</updated>
  <entry xml:base="http://aki.marsorange.com/">
    <author>
      <name>Fred Liang</name>
    </author>
    <id>tag:aki.marsorange.com,2006-07-17:72:20481</id>
    <published>2011-10-09T03:28:09Z</published>
    <updated>2011-10-09T03:28:09Z</updated>
    <category term="Work"/>
    <link href="http://aki.marsorange.com/archives/of-mogrify-ruby-tempfile-dynamic-class-definitions" rel="alternate" type="text/html"/>
    <title>Comment on 'of Mogrify, Ruby Tempfile &amp; Dynamic Class Definitions' by Fred Liang</title>
<content type="html">&lt;p&gt;Why you didn't pass params like this
    Tempfile.new ['filenane', '.stuffix']&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://aki.marsorange.com/">
    <author>
      <name>Michael MacDonald</name>
    </author>
    <id>tag:aki.marsorange.com,2006-07-17:72:17392</id>
    <published>2011-01-06T04:43:23Z</published>
    <updated>2011-01-06T04:43:23Z</updated>
    <category term="Work"/>
    <link href="http://aki.marsorange.com/archives/of-mogrify-ruby-tempfile-dynamic-class-definitions" rel="alternate" type="text/html"/>
    <title>Comment on 'of Mogrify, Ruby Tempfile &amp; Dynamic Class Definitions' by Michael MacDonald</title>
<content type="html">&lt;p&gt;When I upgraded to Ruby 1.9.2 I got the following error with this patch:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;can't convert nil into Integer
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;To solve this, I used the same trick as the paperclip folks used:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sprintf('%s%d-%d%s', File::basename(basename, ext), $$, n.to_i, ext)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Notice the addition of n.to_i.&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://aki.marsorange.com/">
    <author>
      <name>Shyam Khadka</name>
    </author>
    <id>tag:aki.marsorange.com,2006-07-17:72:15676</id>
    <published>2010-10-05T06:38:17Z</published>
    <updated>2010-10-05T06:38:17Z</updated>
    <category term="Work"/>
    <link href="http://aki.marsorange.com/archives/of-mogrify-ruby-tempfile-dynamic-class-definitions" rel="alternate" type="text/html"/>
    <title>Comment on 'of Mogrify, Ruby Tempfile &amp; Dynamic Class Definitions' by Shyam Khadka</title>
<content type="html">&lt;p&gt;Looks great!! But in my case, 
ext = File::extname(basename) 
doesn't return anything.&lt;/p&gt;

&lt;p&gt;So that I am not able to get extension of temp file using file_field of rails.
Any suggestions are highly welcomed!!&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://aki.marsorange.com/">
    <author>
      <name>silverdr</name>
    </author>
    <id>tag:aki.marsorange.com,2006-07-17:72:9155</id>
    <published>2009-08-08T18:21:00Z</published>
    <updated>2009-08-08T18:21:00Z</updated>
    <category term="Work"/>
    <link href="http://aki.marsorange.com/archives/of-mogrify-ruby-tempfile-dynamic-class-definitions" rel="alternate" type="text/html"/>
    <title>Comment on 'of Mogrify, Ruby Tempfile &amp; Dynamic Class Definitions' by silverdr</title>
<content type="html">&lt;p&gt;Steve - thanks. Simple solution to my problem.&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://aki.marsorange.com/">
    <author>
      <name>Matt</name>
    </author>
    <id>tag:aki.marsorange.com,2006-07-17:72:1288</id>
    <published>2008-04-28T16:57:28Z</published>
    <updated>2008-04-28T16:57:28Z</updated>
    <category term="Work"/>
    <link href="http://aki.marsorange.com/archives/of-mogrify-ruby-tempfile-dynamic-class-definitions" rel="alternate" type="text/html"/>
    <title>Comment on 'of Mogrify, Ruby Tempfile &amp; Dynamic Class Definitions' by Matt</title>
<content type="html">&lt;p&gt;Do you know if the newest version fixes this?
== 1.2.2 / 2007-06-01&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://aki.marsorange.com/">
    <author>
      <name>Steve</name>
    </author>
    <id>tag:aki.marsorange.com,2006-07-17:72:94</id>
    <published>2007-07-15T17:48:23Z</published>
    <updated>2007-07-15T17:48:23Z</updated>
    <category term="Work"/>
    <link href="http://aki.marsorange.com/archives/of-mogrify-ruby-tempfile-dynamic-class-definitions" rel="alternate" type="text/html"/>
    <title>Comment on 'of Mogrify, Ruby Tempfile &amp; Dynamic Class Definitions' by Steve</title>
<content type="html">I had the same exact problem and extended your idea a little. This modification to make_tmpname allows you to create temp files with whatever extension you like (but if you don't specify an extension, it works like normal):
&lt;pre&gt;
Module MiniMagick
  class ImgTempFile &amp;lt; Tempfile
    def make_tmpname(basename, n)
      # force tempfile to use basename's extension if provided
      ext = File::extname(basename)
      # force hyphens instead of periods in name
      sprintf('%s%d-%d%s', File::basename(basename, ext), $$, n, ext)
    end
  end # class
end # module
&lt;/pre&gt;

Also, I chose to subclass Tempfile rather than overwrite, which required a small alteration to mini_magick.rb:
&lt;pre&gt;
          tmp = ImgTempFile.new(&quot;minimagic&quot;)
&lt;/pre&gt;
Finally, here's a short test that shows it's working (put it in any convenient test file that gets run on related stuff):
&lt;pre&gt;
  def test_image_temp_file
    tmp = MiniMagick::ImgTempFile.new('test')
    assert_match %r{^test}, File::basename(tmp.path)
    tmp = MiniMagick::ImgTempFile.new('test.jpg')
    assert_match %r{^test}, File::basename(tmp.path)
    assert_match %r{\.jpg$}, File::basename(tmp.path)
  end
&lt;/pre&gt;</content>  </entry>
  <entry xml:base="http://aki.marsorange.com/">
    <author>
      <name>mzpn</name>
    </author>
    <id>tag:aki.marsorange.com,2006-07-17:72:93</id>
    <published>2007-07-15T17:48:23Z</published>
    <updated>2007-07-15T17:48:23Z</updated>
    <category term="Work"/>
    <link href="http://aki.marsorange.com/archives/of-mogrify-ruby-tempfile-dynamic-class-definitions" rel="alternate" type="text/html"/>
    <title>Comment on 'of Mogrify, Ruby Tempfile &amp; Dynamic Class Definitions' by mzpn</title>
<content type="html">fun</content>  </entry>
</feed>

