Class: UnderOs::Page::StylesMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/under_os/page/matcher.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(css_rule) ⇒ StylesMatcher

Returns a new instance of StylesMatcher.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/under_os/page/matcher.rb', line 8

def initialize(css_rule)
  css_rules = css_rule.strip.split(/\s*,\s*/)

  if css_rules.size == 1
    rules    = css_rules[0].scan(/([\S]+)/).map(&:first)
    @rule    = parse(rules.pop)
    @parent  = rules.size == 0 ? false : self.class.new(rules.join(' '))
  else
    @multiple_matchers = css_rules.map{ |rule| self.class.new(rule) }
  end
end

Class Method Details

.new(css_rule) ⇒ Object



3
4
5
6
# File 'lib/under_os/page/matcher.rb', line 3

def self.new(css_rule)
  @cache           ||= {}
  @cache[css_rule] ||= super
end

Instance Method Details

#match(view) ⇒ Object



20
21
22
# File 'lib/under_os/page/matcher.rb', line 20

def match(view)
  score_for(view) != 0
end

#score_for(view) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/under_os/page/matcher.rb', line 24

def score_for(view)
  return summary_score_for(view) if @multiple_matchers

  id_score    = id_score_for(view)
  tag_score   = tag_score_for(view)
  class_score = class_score_for(view)
  attrs_score = attrs_score_for(view)
  total_score = id_score + tag_score + class_score + attrs_score

  total_score = 0 if id_score    == 0 && @rule[:id]
  total_score = 0 if tag_score   == 0 && @rule[:tag]
  total_score = 0 if class_score == 0 && @rule[:classes].size != 0
  total_score = 0 if attrs_score == 0 && @rule[:attrs].size != 0

  if @parent && total_score != 0
    parent_score = parent_score_for(view)
    total_score  = parent_score == 0 ? 0 : total_score + parent_score
  end

  total_score
end