Sha256: 1e755b54753268a47466eeff2af7377ceff33df4dea9d78f2a4d2f8c95e9f1f2

Contents?: true

Size: 1.16 KB

Versions: 5

Compression:

Stored size: 1.16 KB

Contents

module Excesselt
  class Rule
    
    attr_reader :stylesheet, :element, :block, :selector

    def initialize(stylesheet, selector, extensions, &block)
      @stylesheet = stylesheet
      @selector = selector
      @extensions = extensions
      @block = block
    end
    
    def matching_elements(document)
      @selector_cache ||= {}
      @selector_cache[document] ||= document.css(@selector)
    end
    
    def applies_to_element?
      matching_elements(element.document).include? element
    end
    
    def matches?(element)
      @element = element
      if applies_to_element?
        self # if it matches, nil otherwise
      else
        nil
      end
    end
    
    def generate(builder)
      # Call the block in the elements context
      wrapper = ElementWrapper.new(stylesheet, element, builder)
      @extensions.each {|e| wrapper.extend e }
      wrapper.instance_eval(&@block)
    rescue Exception => e
      if e.message =~ /With selector .* and included modules/
        raise e
      else
        raise e.class, "With selector #{selector} and included modules: #{@extensions.inspect}\n#{e.message}\n#{e.backtrace.join("\n")}"
      end
    end
    
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
excesselt-1.1.4 lib/excesselt/rule.rb
excesselt-1.1.3 lib/excesselt/rule.rb
excesselt-1.1.2 lib/excesselt/rule.rb
excesselt-1.1.1 lib/excesselt/rule.rb
excesselt-1.1.0 lib/excesselt/rule.rb