Created
February 25, 2012 15:32
-
-
Save pragmaticpat-zz/1909102 to your computer and use it in GitHub Desktop.
Rails 3.1 STI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Wod < ActiveRecord::Base | |
HOST_BOX_NAME = "CrossFit HQ" | |
def from_html crossfit_hq_source | |
page = Nokogiri::HTML(crossfit_hq_source) | |
self.date = Date.parse(page.search("//h3")[0].inner_text.split[1]).to_s | |
self.details = page.search("//p")[0].inner_text | |
if(hero_wod || games_wod) | |
self.details << "\n" << page.search("//p")[1].inner_html.gsub(/<br>/,"\n") | |
end | |
wod_id = page.search("//div[@class='blogbody']")[0].search("//a[@name]").attr("name").value | |
self.host_box_comment_uri = "http://www.crossfit.com/mt-archive2/00#{wod_id}.html" | |
self.host_box_name = HOST_BOX_NAME | |
end | |
def hero_wod | |
self.details.starts_with?("\"") | |
end | |
def games_wod | |
self.details.starts_with?("CrossFit Games") | |
end | |
end | |
#app/models/wod_crossfit_lackland.rb | |
class WodCrossFitLackland < Wod | |
HOST_BOX_NAME = "CrossFit Lackland" | |
def from_html lackland_source | |
page = Nokogiri::HTML(lackland_source) | |
self.details = page.search("//div[@class='rt-article-content']")[0].inner_text.split("Add new comment")[0] | |
self.host_box_comment_uri = "" | |
self.host_box_name = HOST_BOX_NAME | |
self.date = Date.parse(page.search("//h1[@class='rt-article-title']").inner_text).to_s | |
end | |
end | |
#ERROR | |
ruby-1.9.2-p290 :001 > w = Wod.new | |
=> #<Wod id: nil, date: nil, details: nil, created_at: nil, updated_at: nil, host_box_comment_uri: nil, host_box_name: nil> | |
ruby-1.9.2-p290 :003 > WodCrossFitLackland.new | |
NameError: uninitialized constant WodCrossFitLackland | |
from (irb):3 | |
from /Users/patricksullivan/.rvm/gems/ruby-1.9.2-p290@wodbox/gems/railties-3.1.3/lib/rails/commands/console.rb:45:in `start' | |
from /Users/patricksullivan/.rvm/gems/ruby-1.9.2-p290@wodbox/gems/railties-3.1.3/lib/rails/commands/console.rb:8:in `start' | |
from /Users/patricksullivan/.rvm/gems/ruby-1.9.2-p290@wodbox/gems/railties-3.1.3/lib/rails/commands.rb:40:in `<top (required)>' | |
from script/rails:6:in `require' | |
from script/rails:6:in `<main>' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I actually decided to move away from class inheritance altogether. Doing special parsing in the model makes it fatter than need be. I instead have segregated parsing to actual "parser" implementations that all return the same model, yet hydrate that model through slightly different means.