Created
July 9, 2010 09:19
-
-
Save karmi/469262 to your computer and use it in GitHub Desktop.
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
# Abstract classes, const_get etc examples for a dev friend | |
class String | |
# http://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/string/inflections.rb#L44-49 | |
def camelize | |
self.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase } | |
end | |
end | |
module MyParsers | |
def self.parse!(record={}) | |
parser = const_get( record[:parser].camelize ).new(record) | |
parser.parse! | |
end | |
# Abstract class | |
class Base | |
def initialize(record) | |
@url = record[:url] | |
end | |
def parse! | |
raise NoMethodError, "Implement this, dude" | |
end | |
end | |
class Xml < Base | |
def parse! | |
puts "Launching Nokogiri, eating ur XML at #{@url}" | |
end | |
end | |
class SpecialXml < Xml | |
def parse! | |
puts "Launching Nokogiri, eating ur *** SPECIAL *** XML at #{@url}" | |
end | |
end | |
class Json < Base | |
def parse! | |
puts "Launching yajl, eating ur JSON at #{@url}" | |
end | |
end | |
end | |
record_one = { :url => 'http://example.com/XML', :parser => 'xml' } | |
record_two = { :url => 'http://example.com/JSON', :parser => 'json' } | |
record_three = { :url => 'http://example.com/JSON', :parser => 'special_xml' } | |
MyParsers.parse!(record_one) | |
MyParsers.parse!(record_two) | |
MyParsers.parse!(record_three) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment