extracted method_adding to its own method
This commit is contained in:
parent
f108aac104
commit
fcf1d25716
1 changed files with 16 additions and 9 deletions
|
@ -25,11 +25,10 @@ module Microformats2
|
||||||
obj = klass.new
|
obj = klass.new
|
||||||
|
|
||||||
# Add any properties to the object
|
# Add any properties to the object
|
||||||
self.add_properties(microformat, obj)
|
add_properties(microformat, obj)
|
||||||
self.add_urls(microformat, obj)
|
add_urls(microformat, obj)
|
||||||
self.add_dates(microformat, obj)
|
add_dates(microformat, obj)
|
||||||
self.add_times(microformat, obj)
|
add_times(microformat, obj)
|
||||||
#letters = %w(p u d n e i t)
|
|
||||||
|
|
||||||
microformats[constant_name.downcase.to_sym] << obj
|
microformats[constant_name.downcase.to_sym] << obj
|
||||||
end
|
end
|
||||||
|
@ -37,6 +36,14 @@ module Microformats2
|
||||||
return microformats
|
return microformats
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.add_method(obj, method_name)
|
||||||
|
unless obj.respond_to?(method_name)
|
||||||
|
obj.class.class_eval { attr_accessor method_name }
|
||||||
|
end
|
||||||
|
|
||||||
|
obj
|
||||||
|
end
|
||||||
|
|
||||||
def self.add_properties(mf, obj)
|
def self.add_properties(mf, obj)
|
||||||
%w(p n e i).each do |letter|
|
%w(p n e i).each do |letter|
|
||||||
mf.css("*[class|=#{letter}]").each do |property|
|
mf.css("*[class|=#{letter}]").each do |property|
|
||||||
|
@ -46,7 +53,7 @@ module Microformats2
|
||||||
method_name = css_class.gsub("-","_")
|
method_name = css_class.gsub("-","_")
|
||||||
value = property.text.gsub(/\n+/, " ").gsub(/\s+/, " ").strip
|
value = property.text.gsub(/\n+/, " ").gsub(/\s+/, " ").strip
|
||||||
|
|
||||||
obj.class.class_eval { attr_accessor method_name }
|
add_method(obj, method_name)
|
||||||
|
|
||||||
if cur = obj.send(method_name)
|
if cur = obj.send(method_name)
|
||||||
if cur.kind_of? Array
|
if cur.kind_of? Array
|
||||||
|
@ -71,7 +78,7 @@ module Microformats2
|
||||||
method_name = css_class.gsub("-","_")
|
method_name = css_class.gsub("-","_")
|
||||||
value = property.attribute("href").to_s
|
value = property.attribute("href").to_s
|
||||||
|
|
||||||
obj.class.class_eval { attr_accessor method_name }
|
add_method(obj, method_name)
|
||||||
|
|
||||||
if cur = obj.send(method_name)
|
if cur = obj.send(method_name)
|
||||||
if cur.kind_of? Array
|
if cur.kind_of? Array
|
||||||
|
@ -95,7 +102,7 @@ module Microformats2
|
||||||
method_name = css_class.gsub("-","_")
|
method_name = css_class.gsub("-","_")
|
||||||
value = DateTime.parse((property.attribute("title") || property.text).to_s)
|
value = DateTime.parse((property.attribute("title") || property.text).to_s)
|
||||||
|
|
||||||
obj.class.class_eval { attr_accessor method_name }
|
add_method(obj, method_name)
|
||||||
|
|
||||||
if cur = obj.send(method_name)
|
if cur = obj.send(method_name)
|
||||||
if cur.kind_of? Array
|
if cur.kind_of? Array
|
||||||
|
@ -119,7 +126,7 @@ module Microformats2
|
||||||
method_name = css_class.gsub("-","_")
|
method_name = css_class.gsub("-","_")
|
||||||
value = Time.parse((property.attribute("title") || property.text).to_s)
|
value = Time.parse((property.attribute("title") || property.text).to_s)
|
||||||
|
|
||||||
obj.class.class_eval { attr_accessor method_name }
|
add_method(obj, method_name)
|
||||||
|
|
||||||
if cur = obj.send(method_name)
|
if cur = obj.send(method_name)
|
||||||
if cur.kind_of? Array
|
if cur.kind_of? Array
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue