Archive for September, 2006


Rails iCalendar improvement

Saturday, September 30th, 2006

I came across this thread that simplifies the ical controller from my previous example. It correctly sets the content type header and removes the need for a template. The new version is:

class IcalController < ApplicationController

caches_page :competitions

def competitions headers[’Content-Type’] = “text/calendar”

cal = Icalendar::Calendar.new

Competition.find_all.each do |comp|
  event = Icalendar::Event.new
  event.start = comp.date
  event.end = comp.date
  event.summary = comp.name

  cal.add event
end

render_without_layout :text => cal.to_ical

end

I hadn’t used the render_without_layout command before, but it’s very handy for situations like this.

Spread the word: Technorati related  |  del.icio.us bookmark it!  |  submit Rails iCalendar improvement digg.com digg it!  |  reddit reddit!

Getters and Setters

Friday, September 29th, 2006

I was chatting to Simon the other day and the question of, should you have getters and setters in your Ruby classes, or should you just let other classes access the attributes directly, came up. We were talking about ActiveRecord, which dynamically creates them for any columns in the table the class is representing.

The short answer is, if you want other classes to access that data, yes, you need getters and/or setters, but for different reasons that you might think if you come from a Java world. Getters and setters are one of my pet peeves in Java. I hate it when I see them thrown in by default for every field in the class, reducing it to nothing more than a C struct.

The good reason for not allowing direct access to a field in Java is data encapsulation. By making it private you can decide if it’s read-only or read/write (or neither). But I think this is a poor reason, in so much that other objects shouldn’t be pulling data out, but asking it to do things. Rather than starting from a view point of what data does this class provide, you should think of what are its responsibilities.

But that is tangential to what I want to talk about, why are getters and setters so prevalent in Java? I think the first reason is the early OR mapping frameworks. They typically required you to inherit from a class, or broke OO (no inheritance in EJBs?!?), so you treated the domain object as struct, just to ferry data from Java to the database. Then you’d put logic in other objects and you’re just doing procedural programming. I think the situation is better now that you can work with POJOs (e.g. Hibernate) but you still see tutorials following the old pattern.

The second reason is the madness that is field access in Java. Getters and setters can protect you from this. Java binds field access at compile time, i.e. to a variable’s static type, e.g.:

public class A {
    int num = 0;
}

public class B extends A {
    int num = 2;
}

public class Test {
    public static void main(String[] args) throws Exception {
        A a = new B();
        System.out.println(a.num);

        B b = new B();
        System.out.println(b.num);
    }
}

Would output:

0
2

when you’d hope it would do something sensible like:

2
2

i.e. you shadow fields rather than override them, so you can get weird bugs if the static type is different from the runtime type. The variables can even be different types! Methods are determined by your runtime type so we can make sure we’re always accessing the right variable, e.g.:

public class A {
    int num = 0;

    public int getNum() { return this.num; }
}

public class B extends A {
    int num = 2;

    public int getNum() { return this.num; }
}

public class Test {
    public static void main(String[] args) throws Exception {
        A a = new B();
        System.out.println(a.getNum());

        B b = new B();
        System.out.println(b.getNum());
    }
}

Does output:

2
2

so unless the field is defined in your class, you really shouldn’t access it directly. This means to be safe, all fields should be private and we need protected scope getters and setters if we want that field available for subclasses, what a pain, but helps explain why they are so prevalent.

Back to Ruby, how does it deal with this? If you look back at the top I said if you want to access the data you need getters and setters. This is because attributes (Ruby’s name for fields) are private to an object, and everything you ask from an object has to be a method, basically you couldn’t get to that attribute directly if you wanted to! Ruby also doesn’t have the problem of field shadowing because you don’t declare variables, so when you reference it, you’re referencing the only variable with that name.

So what do the getters and setters look like?

class Test 
    def someField
        @someField
    end

    def someField=(newValue)
        @someField = newValue
    end
end

aObject  = Test.new
test.someField = "blah"
test.someField >> "blah"

i.e. our getters and setters look like we’re accessing a field directly but they are actually method calls. Without these, if we tried to access a field, Ruby would complain that no such method exists.

Since this is such a common pattern Ruby has some shortcuts:

read-only:

class Test
    attr_reader :someField
end

write only:

class Test
    attr_writer :someField
end

read/write:

class Test
    attr_accessor :someField
end

All these methods can take multiple arguments if you’re defining multiple fields.

A good summary of all this can be found here:

http://www.rubycentral.com/book/tut_classes.html#S2

But Ruby’s not perfect. A subclass can access attributes from its parent. which means you’re dependant on its implementation, but a code review can stop that.

The bigger problem is clashes with mixins. If a mixin uses an instance variable you can get collisions. A mixin has no state, that instance variable it refers to belongs to the object that included the mixin, so if it uses a variable with the same name you can get some weird behaviour. Because there is no static typing, the object’s methods and the mixin’s method can change what is held in that attribute and you won’t see that error until runtime where you’ll probably see a missing method error. Typical workarounds are prefixing variables with some sort of namespace to avoid collisions, or using a module level hash to store the mixin’s state outside of the object.

All in all I prefer the Ruby way of getters and setters. On the surface it looks the same but by preventing direct access to attributes and using virtual attributes (not covered here), you classes are less likely to become structs and more like genuine objects.

Spread the word: Technorati related  |  del.icio.us bookmark it!  |  submit Getters and Setters digg.com digg it!  |  reddit reddit!

My lap is safe for now…

Friday, September 29th, 2006

I noticed a while ago that an IBM Thinkpad finally caught fire. IBM (Lenovo really) has issued a recall for some batteries. Thankfully my trusty T30 is not on the list. It’s only Sony batteries that are affected, and my one is made by Sanyo, so I’m safe for now.

But if it’s overheating that’s causing these fires it won’t be long before my hard disk or power adapter bursts into flames…

Spread the word: Technorati related  |  del.icio.us bookmark it!  |  submit My lap is safe for now… digg.com digg it!  |  reddit reddit!

Weird Hibernation Issues

Wednesday, September 20th, 2006

No, nothing to do with sleeping for a very long time, but weird things my Thinkpad does when it hibernates. For the most part hiberation works very well on my Thinkpad. I use the hardware supported one rather than the one built into Linux, but it does do a couple of odd things.

The first is that Firefox occassionaly (but not always!) dies during hibernation. I think I’ve narrowed it does to either Ajax apps running (some network connection getting killed, then killing the app?) or I’m using swap (more likely Firefox has been opened long enough that something else is the cause). It’s not really a problem because I use Tab Mix Plus, which can restore my session after a crash, but it is frustrating if you open a bunch of articles that you want to read on say a long train journey, only to find the browser is gone!

The other weird issue is that my fonts disappear from OpenOffice and Microsoft Office (running via Crossover Office after hiberation. This includes menu fonts and the fonts used in the document. All the spacing remains correct, just no text. It only happens to these two programs, everything else is fine. Restarting xfs doesn’t make a difference, the only way to fix it is to restart X. This problem is just bizarre.

If any one has any solutions to these problems, I’d love to hear them!

Update 1: It looks like the hibernation processes shuts down the network connection and all programs that depend on it. I don’t know if that’s a Gentoo specific thing. The only other process that isn’t running when the computer is turned back on is gconfd-2, but unfortunately starting it up doesn’t help with the font problem.

Update 2: The gconfd-2 thing was a red herring, it was started up by Firefox. Killing it doesn’t affect the fonts. I guess something in X is dying, which can only be fixed with a restart.

Spread the word: Technorati related  |  del.icio.us bookmark it!  |  submit Weird Hibernation Issues digg.com digg it!  |  reddit reddit!

Upgrading a Rails app

Wednesday, September 20th, 2006

Now that I have some free time I can work on my golf league Rails app. There’s a bunch of stuff I want to do:

  • Upgrade to Rails 1.1
  • Upgrade to the stable release of Typo (4.0.x)
  • Start using Capistrano for deployment
  • Add in some tests

Typo

Last time round I started using Typo to manage news and photos (via Flickr). The problem was that I was using a trunk version (r933), which had some stability issues to say the least, but did the job. The biggest problem was a memory leak that caused DreamHost to kill it off whenever it got some significant load. A lot of these issues have been fixed in the current stable release (4.0.x), and it also uses Rails 1.1, so that’s a good excuse to upgrade (although unnessary for the other bit because it’s a separate app).

One thing I always wanted to do was integrate the admin screens of the league manager as tabs in Typo’s admin pages. I think this is being talked about for 4.1 but isn’t available now. So that doesn’t leave much space for tighter integration, and I don’t like having to maintain two code trees. Typo is also overkill for the odd monthly announcement.

Another thing I’ve considered is using a simpler CMS, e.g. Radiant. I haven’t looked at it much, but it supports something called behaviours, which allows you to do something different on a particular page. That could replace the standalone app that handles leaderboards, forms, etc. Another alternative is to just write a simple news posting system, then I wouldn’t have to worry about merging code bases, user models and all the authentication headaches.

If anyone else has experience in running two Rails apps as part of one web app, I’d love to hear about it.

Rails 1.1

I only just upgraded to 1.0 the last time round. It would be nice to see what the current state of the art is. The main problem here is that my copy of Agile Web Development with Rails only covers Rails 1.0, all my info on Rails 1.1 is a bunch of blog clippings.

What I’d prefer to focus on is the performance and memory enhancements I’ve been reading about on RailsExpress.blog. Rails on DreamHost is slow, and if you consume too much memory, your app gets killed. It’d be nice to sort this stuff out, especially since my users aren’t all technical.

Capistrano

Another pain with Rails is that almost certainly, deploying the thing that works on my laptop onto DreamHost won’t work. I don’t know much about Capistrano, but I’ve heard it takes away deployment pain, so I want to give it a go.

Testing

Being a hobby app there are no tests. Most of the logic is just editing records so I don’t need tests, but there are some tricky scoring and ranking things that should be verified with test cases. After that there’s ZenTest. It’s an automated tester that makes sure everything is still working with each change. It’s a different style of development that I’d like to try, it sounds a lot more helpful than occassionaly doing an ‘ant test’ that takes several minutes to run. ;)

Spread the word: Technorati related  |  del.icio.us bookmark it!  |  submit Upgrading a Rails app digg.com digg it!  |  reddit reddit!

When did PayPal get so slow?

Wednesday, September 20th, 2006

When did paying for things by PayPal get so slow? I recently bought a couple of things off eBay and paid via PayPal. For days after I kept getting these payment reminder emails from eBay, but the website showed the payment as made. On closer inspection PayPal had marked the payment as pending. It seems now rather than doing an instant transfer PayPal uses something called echeque that takes 7-9 business days, that’s almost two weeks! It now takes twice as long as it does to write a real cheque, post it and clear into the seller’s bank account!

What’s really frustrating is that PayPal kept recommended to use a bank account rather than a credit card, or better yet keep money with them. But did they use the funds sitting in my PayPal account first? No. I assume credit card payments can’t be as slow, so I’ll be using them with PayPal in future, or better yet, another payment provider altogether.

Update: It turns out the e-cheque only applies if you don’t have a credit card registered. My credit card had expired, but I didn’t bother updating PayPal because I use my bank account. Once I updated it, the instant transfer option was back.

Spread the word: Technorati related  |  del.icio.us bookmark it!  |  submit When did PayPal get so slow? digg.com digg it!  |  reddit reddit!

End of an Era

Wednesday, September 20th, 2006

Last week marked my last day Runtime Collective (now Magpie). I had worked there for just over four years, and it was my first job after university, so I left with mixed feelings. Sad that I was leaving what I knew behind, but at the same time felt it was the right time to leave. It’s been a good four years, especially watching the company transform from a consultancy to an ISV. I had a great opportunity to work on their Search and Alert product from the get go, which helped me expand my programming beyond just the web and databases.

What made my mind up was when our London office was closed. The main office was always in Brighton, and try as we might, we never got the London operation going, the offices were just too close together. But so it’s not forgotten, I took some pictures of those final days. :)

Just two desks left Empty Office Giles and Sian That's everything Locking up the van Heading to the pub

Thanks to everyone at Magpie (past and present) for the chances they gave me and I wish them all the best in the future.

Spread the word: Technorati related  |  del.icio.us bookmark it!  |  submit End of an Era digg.com digg it!  |  reddit reddit!

ati-tv software ABBYY FineReader 8.0 Professional Multilanguage
ipod nana software ABBYY FineReader 8.0 Professional Multilanguage
easy fax software ABBYY FineReader Professional Edition 9.0 with Djvu Addon
encoder coding software Ableton Live 6.0.9
analytical system software ACD Systems Combo Pack
employee scheduling software Acronis Disk Director Server 10.0
eraise fundraising software Acronis Disk Director Suite 10.0
openeye software Acronis Disk Editor v6.0.360
gateway limited software Acronis Drive Cleanser v6.0 Build 383
tungsten software download Acronis Migrate Easy Deluxe v1.0.0.43
physics software Acronis & Paragon Universal Boot CD USB 2009 1.0
avatar server software Acronis & Paragon Universal Boot CD USB 2009 1.0
imaging software driver Acronis & Paragon Universal Boot CD USB 2009 1.0
software raid performance Acronis PartitionExpert 2003
legislative trackinig software Acronis Privacy Expert Suite 7.0
software listings Acronis Privacy Expert Suite 7.0
art software program Acronis Recovery Expert Deluxe
adams software forms Acronis True Image 7.0
free reservations software Acronis True Image Echo Server for Windows 9.5
panda antiviruse software Acronis True Image Enterprise Server 9.1.3666
microsoft excel software Acronis True Image Home 11.0
tape duplication software Acronis True Image Workstation 9.1.3887
free dayplanner software ActionScript 3.0 in Flash CS3 Professional Essential Training
raft software ActiveState Komodo IDE 4.2.0
software crises ActiveState Perl Dev Kit Pro 7
jq-210 download software Adobe Acrobat 7 Professional for Mac
audit software tracking Adobe Acrobat 7 Professional for Mac
smartist software Adobe Acrobat 7.0 Professional
callender software Adobe Acrobat 7.0 Professional
currency conversion software Adobe Acrobat 8.0 Professional
n95 remove software Adobe Acrobat 8.0 Professional
anderson software internet Adobe Acrobat 8.0 Professional
genealogy publishing software Adobe Acrobat 8.0 Professional for Mac
article distribution software Adobe Acrobat 8.0 Professional for Mac
map maker software Adobe Acrobat 9 Pro Extended
decisionbar software Adobe Acrobat 9 Pro Extended
timekeeping system software Adobe Acrobat 9 Pro Extended
knowledge mapping software Adobe Acrobat V 6.0 Professional PC
writing style software Adobe Acrobat V 6.0 Professional PC
management product software Adobe After Effects 6.5 for Mac
military packaging software Adobe After Effects 6.5 for Mac
knockout software Adobe After Effects 6.5 for Mac
mantel test software Adobe After Effects 7.0 Standard
pst repair software Adobe After Effects 7.0 Standard
pcr software customers Adobe After Effects 7.0 Standard
loopholes software testing Adobe After Effects CS3
ebay software tools Adobe After Effects CS3
volleyball statistic software Adobe After Effects CS3
autoquote software Adobe Atmosphere 1.0
medical documentation software Adobe Audition 2.0
software distrubitor Adobe Audition 2.0
6600 software program Adobe Audition 3.0
buy financial software Adobe Contribute CS3
apartment purchasing software Adobe Contribute CS3
define software application Adobe Creative Suite 2 Premium for Mac
autocad software review Adobe Creative Suite 2 Premium for Mac
pctools software Adobe Creative Suite 2 Premium for Mac
webchat software Adobe Creative Suite 2 Premium for Windows
book software store Adobe Creative Suite 2 Premium for Windows
software remote access Adobe Creative Suite 2 Premium for Windows
aor scanner software Adobe Creative Suite 2 Premium for Windows
software design article Adobe Creative Suite 3 Design Premium for Mac
microstudio 4.001 software Adobe Creative Suite 3 Design Premium for Mac
netscape 8 software Adobe Creative Suite 3 Design Premium for Mac
biblesoft software update Adobe Creative Suite 3 Design Premium for Mac
linux frontbridge software Adobe Creative Suite 3 Design Premium for Mac
code repository software Adobe Creative Suite 3 Design Premium for Win
acceleration software program Adobe Creative Suite 3 Design Premium for Win
clinical database software Adobe Creative Suite 3 Design Premium for Win
midi studio software Adobe Creative Suite 3 Design Premium for Win
aqura software Adobe Creative Suite 3 Master Collection for Mac
ezdata software Adobe Creative Suite 3 Master Collection for Mac
brightstore backup software Adobe Creative Suite 3 Master Collection for Mac
tk20 software Adobe Creative Suite 3 Master Collection for Mac
sequence analysis software Adobe Creative Suite 3 Master Collection for Mac
deployable spy software Adobe Creative Suite 3 Master Collection for Win
dprofiler software Adobe Creative Suite 3 Master Collection for Win
atm switch software Adobe Creative Suite 3 Master Collection for Win
hyperion software operations Adobe Creative Suite 3 Master Collection for Win
voice synthesis software Adobe Creative Suite 3 Master Collection for Win
iomega ghost software Adobe Creative Suite 3 Master Collection for Win
ups calculation software Adobe Creative Suite for Mac
oracle software Adobe Creative Suite for Mac
discount cad software Adobe Creative Suite for Mac
audio software forum Adobe Creative Suite for Mac
architectural renovation software Adobe Creative Suite 3 Master Collection for Win + Microsoft Office 2007 Enterprise
iphone 1.0.2 software Adobe Dreamweaver CS3
edsoft software Adobe Dreamweaver CS3
swing analysis software Adobe Dreamweaver CS3 for Mac
flowsheet software Adobe Encore CS3
lunix old software Adobe Encore CS3
software download drums Adobe Encore CS3
ficiton software Adobe Encore DVD 2.0
ebay software bidding Adobe Encore DVD 2.0
src software incorporated Adobe Encore DVD 2.0
amateur radio software Adobe Fireworks CS3
coin collectors software Adobe Fireworks CS3 for Mac
software package Adobe Flash CS3 Professional
convention software Adobe Flash CS3 Professional
picture software free Adobe Flash CS3 Professional for Mac
software circuit design Adobe Flash CS3 Professional for Mac
lenticular software crack Adobe Flash CS4
journal software engineering Adobe Flash CS4
webpage editor software Adobe Flash CS4
airframe business software Adobe Flex v.3.0.2
delmia software Adobe Flex v.3.0.2
freehand graphics software Adobe Font Folio 11
northrop resume software Adobe Font Folio 11
diabetes software pdas Adobe FrameMaker 7.0
istante software inc Adobe FrameMaker 7.0
vigilance software Adobe FrameMaker 8.0
software kinder preschool Adobe FrameMaker 8.0
dex drive software Adobe FrameMaker 9.0
de elementos software Adobe FrameMaker 9.0
software inspections Adobe FrameMaker 9.0
free cdrw software Adobe GoLive CS V 7.0 PC
ftp p2p software Adobe GoLive CS V 7.0 PC
transmit software Adobe GoLive CS2
becker data software Adobe GoLive CS2
fta firmware software Adobe Illustrator CS V 11.0 PC
liquids marketing software Adobe Illustrator CS V 11.0 PC
motorola v-180 software Adobe Illustrator CS2
explore anywhere software Adobe Illustrator CS3
outsourcing software solution Adobe InDesign CS V 3.0 PC
wntipcfg software Adobe InDesign CS V 3.0 PC
opengl software program Adobe InDesign CS2
nokia 6102i software Adobe InDesign CS2
free coloring software Adobe InDesign CS3
opp blackjack software Adobe InDesign CS3
astronomie software free Adobe InDesign CS4
monitoring email software Adobe InDesign CS4
development proposal software Adobe InDesign CS4
software cpu underclocking Adobe PageMaker 7.0.1
feko software Adobe Photoshop Album V 2.0
educational software seattle Adobe Photoshop Album V 2.0
learning measurement software Adobe Photoshop CS for Mac
flir software Adobe Photoshop CS for Mac
gps tracks software Adobe Photoshop CS v.8.0
free donation software Adobe Photoshop CS v.8.0
software updating freeware Adobe Photoshop CS2 for Mac
mountbridge software Adobe Photoshop CS2 for Mac
scream software seismic Adobe Photoshop CS2 V 9.0
software requirements plan Adobe Photoshop CS2 V 9.0
mpeg4 slideshow software Adobe Photoshop CS2 V 9.0
french genealogy software Adobe Photoshop CS3: Enhancing Digital Photographs
free software imac Adobe Photoshop CS3: Enhancing Digital Photographs
graphics benchmarking software Adobe Photoshop CS3 Extended
firewall software features Adobe Photoshop CS3 Extended
automatic prayer software Adobe Photoshop CS3 Extended
pine software examples Adobe Photoshop CS3 Extended for Mac