Class: XFN
- Inherits:
-
Microformat
- Object
- Microformat
- XFN
- Defined in:
- lib/mofo/xfn.rb
Defined Under Namespace
Classes: Link
Constant Summary collapse
- @@valid_relations =
%w( contact acquaintance friend met co-worker colleague co-resident neighbor child parent sibling spouse kin muse crush date sweetheart me )
Instance Attribute Summary collapse
-
#links ⇒ Object
Returns the value of attribute links.
Class Method Summary collapse
- .find_occurences(doc) ⇒ Object (also: find_first, find_every)
Instance Method Summary collapse
- #[](*rels) ⇒ Object
-
#initialize(doc) ⇒ XFN
constructor
A new instance of XFN.
- #method_missing(method, *args, &block) ⇒ Object
- #relations ⇒ Object
Methods included from Microformat::Base
#find, #find_in_children, #timeout=
Constructor Details
#initialize(doc) ⇒ XFN
Returns a new instance of XFN.
57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/mofo/xfn.rb', line 57 def initialize(doc) @links = doc.search("a[@rel]").map do |rl| relation = rl[:rel].split(' ') # prune invalid relations relation.each { |r| relation.delete(r) unless @@valid_relations.include? r } relation = relation.first if relation.size == 1 next if relation.empty? Link.new(:name => rl.innerHTML, :link => rl[:href], :relation => relation) end.compact end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/mofo/xfn.rb', line 81 def method_missing(method, *args, &block) method = method.to_s if (rels = method.split(/_and_/)).size > 1 self[*rels] elsif @links.class.public_instance_methods.include? method @links.send(method, *args, &block) else check = args.first == true ? :== : :has? @links.select { |link| link.relation.send(check, method) }.first_or_self end end |
Instance Attribute Details
#links ⇒ Object
Returns the value of attribute links.
43 44 45 |
# File 'lib/mofo/xfn.rb', line 43 def links @links end |
Class Method Details
Instance Method Details
#[](*rels) ⇒ Object
74 75 76 77 78 79 |
# File 'lib/mofo/xfn.rb', line 74 def [](*rels) @links.select do |link| relation = link.relation relation.respond_to?(:all?) && rels.all? { |rel| relation.include? rel } end.first_or_self end |
#relations ⇒ Object
70 71 72 |
# File 'lib/mofo/xfn.rb', line 70 def relations @relations ||= @links.map { |l| l.relation } end |