Class: SULStyles::Colors

Inherits:
Object
  • Object
show all
Defined in:
lib/sul_styles/colors.rb

Overview

SULStyles::Colors class provides the ability to parse a CSS

stylesheet to get all of the colors assigned to variables.

Defined Under Namespace

Classes: ColorData, CssFile

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.parse_colors(file_name) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/sul_styles/colors.rb', line 22

def parse_colors(file_name)
  file = File.read(file_name)
  CssFile.new(
    title: file.match(%r{(?<=\/\*\n)(.*)(?=\*\/)}m).to_s.strip,
    colors: file.scan(/^\$.*;$/).map do |c|
      color_data = c.split(':')
      ColorData.new(
        variable: color_data.first, value: color_data.last.strip
      )
    end
  )
end

Instance Method Details

#allObject



7
8
9
10
11
# File 'lib/sul_styles/colors.rb', line 7

def all
  @all ||= color_files.map do |file|
    self.class.parse_colors(file)
  end
end

#colorsObject



13
14
15
16
17
18
19
# File 'lib/sul_styles/colors.rb', line 13

def colors
  all.each_with_object({}) do |file, hash|
    file.colors.each do |color_data|
      hash[color_data.value] = color_data.variable
    end
  end
end