class UnderOs::Page::Stylesheet

Attributes

rules[R]

Public Class Methods

new(styles={}) click to toggle source
# File lib/under_os/page/stylesheet.rb, line 4
def initialize(styles={})
  @rules = {}

  styles.each do |rule, style|
    self[rule] = style
  end
end

Public Instance Methods

+(another_stylesheet) click to toggle source
# File lib/under_os/page/stylesheet.rb, line 37
def +(another_stylesheet)
  self.class.new.tap do |combined_sheet|
    combined_sheet << self
    combined_sheet << another_stylesheet
  end
end
<<(another_stylesheet) click to toggle source
# File lib/under_os/page/stylesheet.rb, line 31
def <<(another_stylesheet)
  another_stylesheet.rules.each do |rule, styles|
    self[rule] = styles
  end
end
[](rule) click to toggle source
# File lib/under_os/page/stylesheet.rb, line 22
def [](rule)
  @rules[rule.to_s]
end
[]=(rule, values) click to toggle source
# File lib/under_os/page/stylesheet.rb, line 26
def []=(rule, values)
  @rules[rule.to_s] ||= {}
  @rules[rule.to_s].merge! values
end
load(filename) click to toggle source
# File lib/under_os/page/stylesheet.rb, line 44
def load(filename)
  UnderOs::Parser.parse(filename).each do |rule, styles|
    self[rule] = styles
  end
end
styles_for(view) click to toggle source
# File lib/under_os/page/stylesheet.rb, line 12
def styles_for(view)
  {}.tap do |styles|
    weighted_styles_for(view).each do |hash|
      hash.each do |key, value|
        styles[key] = value
      end
    end
  end
end

Private Instance Methods

find_styles_for(view) click to toggle source
# File lib/under_os/page/stylesheet.rb, line 58
def find_styles_for(view)
  [].tap do |styles|
    @rules.each do |css, rule|
      score = UnderOs::Page::StylesMatcher.new(css).score_for(view)
      styles << {score: score, style: rule} if score != 0
    end
  end
end
weighted_styles_for(view) click to toggle source
# File lib/under_os/page/stylesheet.rb, line 52
def weighted_styles_for(view)
  find_styles_for(view).
    sort{|a,b| a[:score] <=> b[:score]}.
    map{|e| e[:style]}
end