Sporkmonger

purveyor of fabulously ambiguous eating utensils

HOWTO: Monkey Patch Without Annoying Others

Posted by sporkmonger
Written July 8th, 2006

It’s really very, very simple. Subclass first, then monkey patch the subclass and use that instead.

Update: Here’s an example, from one of the most over-monkey-patched classes ever:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

require 'logger'
require File.dirname(__FILE__) + '/core_ext/class/attribute_accessors'

class CleanLogger < Logger #:nodoc:
  cattr_accessor :silencer
  self.silencer = true

  # Silences the logger for the duration of the block.
  def silence(temporary_level = Logger::ERROR)
    if silencer
      begin
        old_logger_level, self.level = level, temporary_level
        yield self
      ensure
        self.level = old_logger_level
      end
    else
      yield self
    end
  end

  private
    alias old_format_message format_message

    # Ruby 1.8.3 transposed the msg and progname arguments to format_message.
    # We can't test RUBY_VERSION because some distributions don't keep Ruby
    # and its standard library in sync, leading to installations of Ruby 1.8.2
    # with Logger from 1.8.3 and vice versa.
    if method_defined?(:formatter=)
      def format_message(severity, timestamp, progname, msg)
        "#{msg}\n"
      end
    else
      def format_message(severity, timestamp, msg, progname)
        "#{msg}\n"
      end
    end
end

Notice the difference? See, that wasn’t so bad now, was it?

Update: I’m certainly not the first to complain about this.

Monkey Patching Goodness

Posted by sporkmonger
Written July 8th, 2006

FeedTools 0.2.25: Now with 625 lines of monkey patching, and all the same terrible performance you’ve come to expect!

I decided to extract all of my REXML monkey patches out into a single file instead of leaving them all in feed_tools.rb for this release. Tests should all pass on Ruby 1.8.4 now. And Sam Ruby’s feed should be handled correctly again. His use of ”.” as his link uri caused one of the parser’s heuristics to throw a hissy fit and misreport the feed’s uri as nil and the value of the feed’s link as the feed’s uri. Weird stuff. Anyways, that works again. (NetNewsWire was breaking on Sam’s feed last I checked.) HTTP redirection handling has been changed in that FeedTools won’t barf if a relative Location: header is supplied. And the parser should generally work a little bit better with FeedUpdater.

I’ll probably make another release when I get around to integrating my new URI code. After that, that will likely be the last release for quite some time. Virtually all of my free coding time will be being spent on GentleCMS instead. Just a heads-up.

Schema Fiddling

Posted by sporkmonger
Written February 16th, 2006

I’ve been fiddlin’ with the schema again. The default cache table is now cached_feeds and the url field has been renamed to href. These changes are being made in anticipation of the API changes I’ll be making for 0.3.0 and also because it’s driving me nuts to keep having to “monkey patch” my own code every time I want to use any of the nifty new stuff that I haven’t released yet.

Sorry for the inconvenience.

This change has been checked into Subversion. 0.2.23 should be out soon, and, thus far, seems to be a whole lot more stable than 0.2.22 was.

Update: By popular request, from uh… me, myself, and I, a migration file for this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14

class UpdateFeedToolsSchema < ActiveRecord::Migration
  def self.up
    puts "Updating feed caches table..."
    rename_table(:feeds, :cached_feeds)
    rename_column(:cached_feeds, :url, :href)
  end

  def self.down
    puts "Restoring feed caches table..."
    rename_column(:cached_feeds, :href, :url)
    rename_table(:cached_feeds, :feeds)
  end
end