Sha256: 50eaf51e497c10ed0207a5a90f67af52e9b8c7d99266258022b0fddfa6e2cfce

Contents?: true

Size: 1.14 KB

Versions: 7

Compression:

Stored size: 1.14 KB

Contents

require "singleton"

module Microformat
  class Selectors < Hash
    include Singleton
    
    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
    
    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
    
    def self.filter(klasses = nil)
      if klasses
        instance.filter(Array(klasses))
      else
        instance.keys
      end
    end
    
    def filter(klasses)
      select do |selector, klass|
        Array(klasses).include?(klass)
      end.keys
    end
  end
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
elcamino-microformat-0.0.8 lib/microformat/selectors.rb
microformat-0.0.7 lib/microformat/selectors.rb
microformat-0.0.6 lib/microformat/selectors.rb
microformat-0.0.5 lib/microformat/selectors.rb
microformat-0.0.4 lib/microformat/selectors.rb
microformat-0.0.3 lib/microformat/selectors.rb
microformat-0.0.2 lib/microformat/selectors.rb