Class: Cyborg::Assets::Stylesheets

Inherits:
AssetType
  • Object
show all
Defined in:
lib/cyborg/plugin/assets/stylesheets.rb

Instance Attribute Summary

Attributes inherited from AssetType

#base, #plugin

Instance Method Summary collapse

Methods inherited from AssetType

#build_failure, #build_success, #change, #compress, #destination, #file_event, #filter_files, #find_files, #find_node_module, #initialize, #local_path, #log_error, #log_success, #npm_command, #url, #urls, #watch

Constructor Details

This class inherits a constructor from Cyborg::Assets::AssetType

Instance Method Details

#asset_tag(*args) ⇒ Object



12
13
14
# File 'lib/cyborg/plugin/assets/stylesheets.rb', line 12

def asset_tag(*args)
  stylesheet_link_tag(args)
end

#build(ext = nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/cyborg/plugin/assets/stylesheets.rb', line 16

def build(ext=nil)
  files = find_files
  files = files.reject {|f| !f.match(/\.#{ext}/) } if ext

  files.each do |file|

    begin
      if File.extname(file).match(/\.css/)
        build_css(file)
      elsif File.extname(file).match(/\.s[ca]ss/)
        build_sass(file)
      end

      puts build_success(file)

    rescue Exception => e
      build_failure file

      if e.backtrace.is_a? Array
        log_error "Error in file: #{local_path(e.backtrace[0])}"
      end

    log_error "  #{e.message}\n" if e.message
    end
  end
end

#build_css(file) ⇒ Object



43
44
45
46
47
48
# File 'lib/cyborg/plugin/assets/stylesheets.rb', line 43

def build_css(file)
  css = AutoprefixerRails.process(File.read(file)).css
  File.open(destination(file), 'w') { |io| io.write(css) }

  compress(destination(file))
end

#build_sass(file) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/cyborg/plugin/assets/stylesheets.rb', line 50

def build_sass(file)
  style = Cyborg.production? ? "compressed" : 'nested'
  dest = destination(file)

  Sass.logger.log_level = :error if Cyborg.production?

  css = Sass.compile_file(file, style: style)
  css = AutoprefixerRails.process(css).css

  File.open(dest, 'w') { |io| io.write(css) }

  compress(dest)
end

#dataObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/cyborg/plugin/assets/stylesheets.rb', line 64

def data
  if @data
    @data
  else
    data = {}

    Dir[File.join(base, "**/*.yml")].each do |file|
      key = file.sub(base+"/", '').sub(/^_/,'').sub('.yml','')

      data[key] = SassParser.parse(file)
    end

    @data = data if Cyborg.production?
    data
  end
end

#extObject



8
9
10
# File 'lib/cyborg/plugin/assets/stylesheets.rb', line 8

def ext
  "*[ca]ss"
end

#versioned(file) ⇒ Object

Convert extension



82
83
84
# File 'lib/cyborg/plugin/assets/stylesheets.rb', line 82

def versioned(file)
  super(file.sub(/(\.css)?\.s[ca]ss$/i,'.css'))
end