Class: ForemanThemeSatellite::CssCompare

Inherits:
Object
  • Object
show all
Defined in:
lib/generators/foreman_theme_satellite/lib/css_compare.rb

Overview

Semantically compares two css files. returns hash: rule => prop => value

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(left_filename: nil, left_css: nil, right_filename: nil, right_css: nil) ⇒ CssCompare

Returns a new instance of CssCompare.



11
12
13
14
# File 'lib/generators/foreman_theme_satellite/lib/css_compare.rb', line 11

def initialize(left_filename: nil, left_css: nil, right_filename: nil, right_css: nil)
  @left = left_filename ? Sass::Engine.for_file(left_filename, syntax: :scss) : Sass::Engine.new(left_css, syntax: :scss)
  @right = right_filename ? Sass::Engine.for_file(right_filename, syntax: :scss) : Sass::Engine.new(right_css, syntax: :scss)
end

Instance Attribute Details

#leftObject (readonly)

Returns the value of attribute left.



9
10
11
# File 'lib/generators/foreman_theme_satellite/lib/css_compare.rb', line 9

def left
  @left
end

#rightObject (readonly)

Returns the value of attribute right.



9
10
11
# File 'lib/generators/foreman_theme_satellite/lib/css_compare.rb', line 9

def right
  @right
end

Instance Method Details

#diffObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/generators/foreman_theme_satellite/lib/css_compare.rb', line 16

def diff
  result_hash = {}

  right_hash = rules_hash(right)

  rules_hash(left).each do |rule, props|
    right_props = right_hash[rule]
    unless props
      result_hash[rule] = props
      next
    end

    diffed_props = props_diff(props_hash(props), props_hash(right_props))

    result_hash[rule] = diffed_props unless diffed_props.empty?
  end

  result_hash
end

#to_css(diff) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/generators/foreman_theme_satellite/lib/css_compare.rb', line 36

def to_css(diff)
  diff.map do |rule, props|
    [
      "#{rule} {",
      to_properties_css(props, '  '),
      '}'
    ]
  end.flatten.join("\n")
end