Sha256: 456908518e8c2b956a3b7213ad0c92978fa265b0cb8f7639bad4937f88be8060

Contents?: true

Size: 1.26 KB

Versions: 6

Compression:

Stored size: 1.26 KB

Contents

class UnderOs::Page::Stylesheet
  attr_reader :rules

  def initialize(styles={})
    @rules = {}

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

  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

  def [](rule)
    @rules[rule.to_s]
  end

  def []=(rule, values)
    @rules[rule.to_s] ||= {}
    @rules[rule.to_s].merge! values
  end

  def <<(another_stylesheet)
    another_stylesheet.rules.each do |rule, styles|
      self[rule] = styles
    end
  end

  def +(another_stylesheet)
    self.class.new.tap do |combined_sheet|
      combined_sheet << self
      combined_sheet << another_stylesheet
    end
  end

  def load(filename)
    UnderOs::Parser.parse(filename).each do |rule, styles|
      self[rule] = styles
    end
  end

private

  def weighted_styles_for(view)
    find_styles_for(view).
      sort{|a,b| a[:score] <=> b[:score]}.
      map{|e| e[:style]}
  end

  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

end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
under-os-ui-1.4.0 lib/under_os/page/stylesheet.rb
under-os-1.3.0 lib/under_os/page/stylesheet.rb
under-os-1.2.1 lib/under_os/page/stylesheet.rb
under-os-1.2.0 lib/under_os/page/stylesheet.rb
under-os-1.1.0 lib/under_os/page/stylesheet.rb
under-os-1.0.0 lib/under_os/page/stylesheet.rb