Class: Excesselt::Rule
- Inherits:
-
Object
- Object
- Excesselt::Rule
- Defined in:
- lib/excesselt/rule.rb
Instance Attribute Summary collapse
-
#block ⇒ Object
readonly
Returns the value of attribute block.
-
#element ⇒ Object
readonly
Returns the value of attribute element.
-
#selector ⇒ Object
readonly
Returns the value of attribute selector.
-
#stylesheet ⇒ Object
readonly
Returns the value of attribute stylesheet.
Instance Method Summary collapse
- #applies_to_element? ⇒ Boolean
- #generate(builder) ⇒ Object
-
#initialize(stylesheet, selector, extensions, &block) ⇒ Rule
constructor
A new instance of Rule.
- #matches?(element) ⇒ Boolean
- #matching_elements(document) ⇒ Object
Constructor Details
#initialize(stylesheet, selector, extensions, &block) ⇒ Rule
Returns a new instance of Rule.
6 7 8 9 10 11 |
# File 'lib/excesselt/rule.rb', line 6 def initialize(stylesheet, selector, extensions, &block) @stylesheet = stylesheet @selector = selector @extensions = extensions @block = block end |
Instance Attribute Details
#block ⇒ Object (readonly)
Returns the value of attribute block.
4 5 6 |
# File 'lib/excesselt/rule.rb', line 4 def block @block end |
#element ⇒ Object (readonly)
Returns the value of attribute element.
4 5 6 |
# File 'lib/excesselt/rule.rb', line 4 def element @element end |
#selector ⇒ Object (readonly)
Returns the value of attribute selector.
4 5 6 |
# File 'lib/excesselt/rule.rb', line 4 def selector @selector end |
#stylesheet ⇒ Object (readonly)
Returns the value of attribute stylesheet.
4 5 6 |
# File 'lib/excesselt/rule.rb', line 4 def stylesheet @stylesheet end |
Instance Method Details
#applies_to_element? ⇒ Boolean
18 19 20 |
# File 'lib/excesselt/rule.rb', line 18 def applies_to_element? matching_elements(element.document).include? element end |
#generate(builder) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/excesselt/rule.rb', line 31 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. =~ /With selector '.*' and included modules/ raise e else raise e.class, "With selector '#{selector}' and included modules: #{@extensions.inspect}\n#{e.}\n#{e.backtrace.join("\n")}" end end |
#matches?(element) ⇒ Boolean
22 23 24 25 26 27 28 29 |
# File 'lib/excesselt/rule.rb', line 22 def matches?(element) @element = element if applies_to_element? self # if it matches, nil otherwise else nil end end |
#matching_elements(document) ⇒ Object
13 14 15 16 |
# File 'lib/excesselt/rule.rb', line 13 def matching_elements(document) @selector_cache ||= {} @selector_cache[document] ||= document.css(@selector) end |