Class: Propshaft::Compilers

Inherits:
Object
  • Object
show all
Defined in:
lib/propshaft/compilers.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(assembly) ⇒ Compilers

Returns a new instance of Compilers.



4
5
6
7
# File 'lib/propshaft/compilers.rb', line 4

def initialize(assembly)
  @assembly      = assembly
  @registrations = Hash.new
end

Instance Attribute Details

#assemblyObject (readonly)

Returns the value of attribute assembly.



2
3
4
# File 'lib/propshaft/compilers.rb', line 2

def assembly
  @assembly
end

#registrationsObject (readonly)

Returns the value of attribute registrations.



2
3
4
# File 'lib/propshaft/compilers.rb', line 2

def registrations
  @registrations
end

Instance Method Details

#any?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/propshaft/compilers.rb', line 14

def any?
  registrations.any?
end

#compilable?(asset) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/propshaft/compilers.rb', line 18

def compilable?(asset)
  registrations[asset.content_type.to_s].present?
end

#compile(asset) ⇒ Object



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

def compile(asset)
  if relevant_registrations = registrations[asset.content_type.to_s]
    asset.content.dup.tap do |input|
      relevant_registrations.each do |compiler|
        input.replace compiler.new(assembly).compile(asset, input)
      end
    end
  else
    asset.content
  end
end

#referenced_by(asset) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/propshaft/compilers.rb', line 34

def referenced_by(asset)
  Set.new.tap do |references|
    if relevant_registrations = registrations[asset.content_type.to_s]
      relevant_registrations.each do |compiler|
        references.merge compiler.new(assembly).referenced_by(asset)
      end
    end
  end
end

#register(mime_type, klass) ⇒ Object



9
10
11
12
# File 'lib/propshaft/compilers.rb', line 9

def register(mime_type, klass)
  registrations[mime_type] ||= []
  registrations[mime_type] << klass
end