< mockra >

Sorting Project Resources - 02 May 2012


Here’s a snippet of the code I’ve been using to sort basic resources for my project management application. I’m planning on tweaking the regular expressions more, but they’ve been working so far for basic usage tests. The following code is taken from model/resource.rb.

  before_save :find_format

  def find_format
    self.format = 'string' unless supported_format
  end

  def supported_format
    twitter? || link?
  end

  def twitter?
    self.format = 'twitter' if self.value =~ /\A@[A-Z0-9_]{1,}\Z/i
  end

  def link?
    self.format = 'link' if self.value =~
      /\Ahttp|https|www|(:\/\/){0,1}\.{0,1}[a-z0-9-]{1,}\.{1}[^ ]*\Z/i
  end

As you can see the find_format method is called before a resource is created or updated. The default format is a basic string, which is assigned if the resource value doesn’t match any of the supported regular expressions.

I currently have support for Twitter and Links; however, I plan to add an RSS format, as well as many others in the future.