Class: CssMedia::Sprockets

Inherits:
Object
  • Object
show all
Defined in:
lib/css_media/sprockets.rb

Overview

Register cssmedia postprocessor in Sprockets and fix common issues

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, &block) ⇒ Sprockets

Sprockets 2 API new and render



52
53
54
55
# File 'lib/css_media/sprockets.rb', line 52

def initialize(filename, &block)
  @filename = filename
  @source   = block.call
end

Class Method Details

.call(input) ⇒ Object

Sprockets 3 and 4 API



11
12
13
14
15
# File 'lib/css_media/sprockets.rb', line 11

def self.call(input)
  filename  = input[:filename]
  source    = input[:data]
  run(filename, source)
end

.install(env) ⇒ Object

Register postprocessor in Sprockets depend on issues with other gems



30
31
32
33
34
35
36
37
38
# File 'lib/css_media/sprockets.rb', line 30

def self.install(env)
  if ::Sprockets::VERSION.to_f < 4
    env.register_postprocessor('text/css',
      ::CssMedia::Sprockets)
  else
    env.register_bundle_processor('text/css',
      ::CssMedia::Sprockets)
  end
end

.register_processor(processor) ⇒ Object



6
7
8
# File 'lib/css_media/sprockets.rb', line 6

def self.register_processor(processor)
  @processor = processor
end

.run(filename, css) ⇒ Object

Add prefixes to ‘css`



18
19
20
21
22
23
24
25
26
27
# File 'lib/css_media/sprockets.rb', line 18

def self.run(filename, css)
  output = filename.chomp(File.extname(filename)) + '.css'
  result = @processor.process(css, from: filename, to: output)

  result.warnings.each do |warning|
    $stderr.puts "css_media: #{ warning }"
  end

  result.css
end

.uninstall(env) ⇒ Object

Register postprocessor in Sprockets depend on issues with other gems



41
42
43
44
45
46
47
48
49
# File 'lib/css_media/sprockets.rb', line 41

def self.uninstall(env)
  if ::Sprockets::VERSION.to_f < 4
    env.unregister_postprocessor('text/css',
      ::CssMedia::Sprockets)
  else
    env.unregister_bundle_processor('text/css',
      ::CssMedia::Sprockets)
  end
end

Instance Method Details

#render(_, _) ⇒ Object

Sprockets 2 API new and render



58
59
60
# File 'lib/css_media/sprockets.rb', line 58

def render(_, _)
  self.class.run(@filename, @source)
end