From 65fb44239cee788aa5e87c9b4a5055c82d7daceb Mon Sep 17 00:00:00 2001 From: Michael Mitchell + Chris Stringer Date: Fri, 22 Mar 2013 16:20:18 -0700 Subject: [PATCH] DRY up the logic to create new property classes. Take some code that was duplicated in both the PropertyParser and Format classes and move into a method in the Property module. --- lib/microformats2/format.rb | 8 +------- lib/microformats2/property.rb | 13 +++++++++++++ lib/microformats2/property_parser.rb | 7 +------ 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/lib/microformats2/format.rb b/lib/microformats2/format.rb index 8460888..d06427e 100644 --- a/lib/microformats2/format.rb +++ b/lib/microformats2/format.rb @@ -33,13 +33,7 @@ module Microformats2 end def add_property(property_class, value) - # NOTE: Might want to DRY this up with what is in PropertyParser - prefix = property_class.split("-").first - # find ruby class for kind of property - klass = Microformats2::Property::PREFIX_CLASS_MAP[prefix] - raise InvalidPropertyPrefix unless klass - # We don't have a nokogiri element so pass in nil - property = klass.new(nil, property_class, value) + property = Property.new(nil, property_class, value) assign_property(property) end diff --git a/lib/microformats2/property.rb b/lib/microformats2/property.rb index 9204fae..651f644 100644 --- a/lib/microformats2/property.rb +++ b/lib/microformats2/property.rb @@ -6,5 +6,18 @@ module Microformats2 "u" => Url, "dt" => DateTime, "e" => Embedded } + + class << self + + def new(element, property_class, value=nil) + # p-class-name -> p + prefix = property_class.split("-").first + # find ruby class for kind of property + klass = PREFIX_CLASS_MAP[prefix] + raise InvalidPropertyPrefix unless klass + klass.new(element, property_class, value) + end + end + end end diff --git a/lib/microformats2/property_parser.rb b/lib/microformats2/property_parser.rb index a51f473..62f00d1 100644 --- a/lib/microformats2/property_parser.rb +++ b/lib/microformats2/property_parser.rb @@ -26,12 +26,7 @@ module Microformats2 def parse_property(element) property_classes(element).map do |property_class| - # p-class-name -> p - prefix = property_class.split("-").first - # find ruby class for kind of property - klass = Microformats2::Property::PREFIX_CLASS_MAP[prefix] - - property = klass.new(element, property_class).parse + property = Property.new(element, property_class).parse properties = format_classes(element).empty? ? PropertyParser.parse(element.children) : [] [property].concat properties