1945ab4cdb87eaf5a5c906fa884c29f1

Learning Ruby, and learning it's functional support...

#!/usr/bin/env ruby -w

require 'rexml/document'

class TomboyDirectory < Hash
  attr_reader :directory

  def initialize(directory)
    raise "#{directory} is not a directory" unless File.lstat(directory).directory?

    @directory = directory

    for fn in Dir.glob "#{directory}/*.note"
      doc = REXML::Document.new File.read fn
      self[doc.elements['note/title'].text] = File.basename(fn)
    end
  end
end

if __FILE__ == $0
  raise "No left-directory (source) specified"   if ARGV[0].nil?
  raise "No right-directory (target) specified"  if ARGV[1].nil?

  left = TomboyDirectory.new ARGV[0]
  right = TomboyDirectory.new ARGV[1]

  # Find notes (by title) in both Tomboy directories that don't have the same UUID.
  # Then, rename them so they do.
  right.reject { |t, u| (not left.key? t) or left[t] == u } \
    .inject({}) { |m, (t, u)| m.update({u => left[t]}) } \
    .inject({}) { |m, (orig, new)| m.update({File.join(right.directory, orig) => File.join(right.directory, new)}) } \
    .each { |orig, new| raise "Right-side note collision" if File.exists? new
      File.rename(orig, new) }
end

Refactorings

No refactoring yet !

4f3f651e606c5a27c1d1b6f31c95c176

Jared

December 9, 2009, December 09, 2009 18:17, permalink

No rating. Login to rate!

Why are you inheriting from Hash class ?

Your refactoring





Format Copy from initial code

or Cancel