Class: Microformat::Selectors

Inherits:
Hash
  • Object
show all
Includes:
Singleton
Defined in:
lib/microformat/selectors.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.class_matching(element) ⇒ Object

Raises:

  • (RuntimeError)


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/microformat/selectors.rb', line 7

def self.class_matching(element)
  # load the CSS classes of the element
  css_classes = element["class"].split(/\s+/).map do |c|
    ".#{c}"
  end
  # loop through the classes
  Array(css_classes).each do |css_class|
    instance.each do |selector, klass|
      if selector == css_class
        return klass
      end
    end
  end
  raise RuntimeError, "No class is defined for any of the selectors: #{css_classes.join(" / ")}"
end

.define(selector, klass) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/microformat/selectors.rb', line 23

def self.define(selector, klass)
  if instance.has_key?(selector) && instance.fetch(selector) != klass
    raise ArgumentError, "#{instance.fetch(selector).name} has already implemented the selector '#{selector}'"
  else
    instance.merge!(selector => klass)
  end
end

.filter(klasses = nil) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/microformat/selectors.rb', line 31

def self.filter(klasses = nil)
  if klasses
    instance.filter(Array(klasses))
  else
    instance.keys
  end
end

Instance Method Details

#filter(klasses) ⇒ Object



39
40
41
42
43
# File 'lib/microformat/selectors.rb', line 39

def filter(klasses)
  select do |selector, klass|
    Array(klasses).include?(klass)
  end.keys
end