Class: UnifiedAssets::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/unified_assets/builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(files, unified_file_name, options = {}) ⇒ Builder

Returns a new instance of Builder.



11
12
13
14
15
16
17
18
# File 'lib/unified_assets/builder.rb', line 11

def initialize(files, unified_file_name, options = {})
  @options             = options
  @files               = Array(files)
  @unified_file_name   = unified_file_name
  @unified_file_mtime  = File.stat(unified_file_name).mtime if File.exists?(unified_file_name)
  @minified_file_name  = Minifier.minified_name(unified_file_name) if minifying?
  @minified_file_mtime = File.stat(minified_file_name).mtime if minifying? && File.exists?(minified_file_name)
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



4
5
6
# File 'lib/unified_assets/builder.rb', line 4

def files
  @files
end

#minified_file_mtimeObject (readonly)

Returns the value of attribute minified_file_mtime.



8
9
10
# File 'lib/unified_assets/builder.rb', line 8

def minified_file_mtime
  @minified_file_mtime
end

#minified_file_nameObject (readonly)

Returns the value of attribute minified_file_name.



7
8
9
# File 'lib/unified_assets/builder.rb', line 7

def minified_file_name
  @minified_file_name
end

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/unified_assets/builder.rb', line 9

def options
  @options
end

#unified_file_mtimeObject (readonly)

Returns the value of attribute unified_file_mtime.



6
7
8
# File 'lib/unified_assets/builder.rb', line 6

def unified_file_mtime
  @unified_file_mtime
end

#unified_file_nameObject (readonly)

Returns the value of attribute unified_file_name.



5
6
7
# File 'lib/unified_assets/builder.rb', line 5

def unified_file_name
  @unified_file_name
end

Instance Method Details

#buildObject



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/unified_assets/builder.rb', line 54

def build
  if rebuild?
    debug("**** REGENERATING #{unified_file_name} BECAUSE #{rebuild_reason} ****", options)
    File.open(unified_file_name, 'w') do |uf|
      files.each do |filename|
        debug("**** RENDERING #{filename}")
        uf << IO.read(filename)
      end
    end
    Minifier.minify(unified_file_name) if minifying?
  end
end

#debug(msg, options = {}) ⇒ Object



67
68
69
# File 'lib/unified_assets/builder.rb', line 67

def debug(msg, options = {})
  puts msg if options[:debug]
end

#minified_file_missing?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/unified_assets/builder.rb', line 24

def minified_file_missing?
  minifying? && minified_file_mtime.nil?
end

#minifying?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/unified_assets/builder.rb', line 28

def minifying?
  !!options[:minify] && Minifier.enabled?(unified_file_name)
end

#rebuild?Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/unified_assets/builder.rb', line 32

def rebuild?
  if @rebuild_because.nil?
    @rebuild_because = []
    @rebuild_because << "UNIFIED FILE IS MISSING" if unified_file_missing?
    @rebuild_because << "MINIFIED FILE IS MISSING" if minified_file_missing?
    @rebuild_because << "MINIFIED FILE IS OUT OF DATE" if minifying? && !minified_file_missing? && !unified_file_missing? && (minified_file_mtime < unified_file_mtime)
    unless unified_file_missing?
      files.each do |name|
        file_mtime = File.stat(name).mtime
        if (file_mtime > unified_file_mtime)
          @rebuild_because << "#{name} IS OUT OF DATE"
        end
      end
    end
  end
  !@rebuild_because.empty?
end

#rebuild_reasonObject



50
51
52
# File 'lib/unified_assets/builder.rb', line 50

def rebuild_reason
  @rebuild_because.join(', ') if rebuild?
end

#unified_file_missing?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/unified_assets/builder.rb', line 20

def unified_file_missing?
  unified_file_mtime.nil?
end