Class: Assette::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/assette/config.rb

Defined Under Namespace

Classes: Builder

Constant Summary collapse

MULTIPLES =
%w{file_path asset_host}.freeze
SINGLES =
%w{asset_dir templates_path template_format cache_method 
template_preloader template_partial asset_version_file uglifier
minify less sass}.freeze
BLOCKS =
%w{after_compile cachebuster_string}.freeze
OPTIONS =
begin
  arr = MULTIPLES.collect do |m|
    "#{m}s"
  end
  arr += SINGLES
  arr += BLOCKS
end.freeze
DEFAULTS =
{
  :asset_dir => 'assets',
  :asset_version_file => 'assets/version',
  :asset_hosts => [],
  :file_paths => %w{public},
  :templates_path => 'app/templates',
  :template_format => 'AT.t[{*path*}] = {*template*};',
  :after_compile => Proc.new {},
  :cachebuster_string => Proc.new {Git.open('.').log.first.sha[0...8] rescue Time.now.strftime("%y%m%d_%H%M%S")},
  :uglifier => {:copyright => false, :mangle => false},
  :less => {},
  :sass => {}
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Config

Returns a new instance of Config.



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/assette/config.rb', line 50

def initialize args = {}
  args = DEFAULTS.merge(args||{})
  
  args.each do |k,v|
    begin
      var = v.dup.freeze
    rescue TypeError
      var = v
    end
    instance_variable_set "@#{k}", var
  end
end

Instance Attribute Details

#compilingObject

Returns the value of attribute compiling.



33
34
35
# File 'lib/assette/config.rb', line 33

def compiling
  @compiling
end

#envObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/assette/config.rb', line 63

def env
  return @env if @env
  
  case
  when defined?(ENVIRONMENT)
    ENVIRONMENT
  when defined?(Merb)
    Merb.env
  when defined?(Rails)
    Rails.env
  else
    ENV['RACK_ENV'] || ENV['RAILS_ENV']
  end
end

#minifyingObject

Returns the value of attribute minifying.



33
34
35
# File 'lib/assette/config.rb', line 33

def minifying
  @minifying
end

#shaObject

Returns the value of attribute sha.



33
34
35
# File 'lib/assette/config.rb', line 33

def sha
  @sha
end

Class Method Details

.load(p) ⇒ Object



211
212
213
214
215
216
# File 'lib/assette/config.rb', line 211

def load p
  b = Builder.new(p)
  b.__run__
  
  new b.to_hash
end

Instance Method Details

#asset_host(i) ⇒ Object



133
134
135
136
# File 'lib/assette/config.rb', line 133

def asset_host i
  return '' unless asset_hosts?
  @asset_hosts[i % @asset_hosts.size]
end

#asset_hosts?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/assette/config.rb', line 109

def asset_hosts?
  !@asset_hosts.empty?
end

#build_targetObject



105
106
107
# File 'lib/assette/config.rb', line 105

def build_target
  @asset_dir
end

#compiled_path(str) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/assette/config.rb', line 138

def compiled_path str
  str = str.dup
  if sha
    case cache_method.to_s
    when 'path'
      str = File.join(sha,str)
    when 'param'
      str << (str.include?('?') ? '&' : '?')
      
      str << 'v=' << sha
    else
      warn('No cache compile method set (param or path)')
    end
  end
  
  str
end

#compiling?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/assette/config.rb', line 113

def compiling?
  !!compiling
end

#dev?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/assette/config.rb', line 86

def dev?
  !prod?
end

#env?(e) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/assette/config.rb', line 78

def env? e
  env == e
end

#find_file_from_relative_path(path) ⇒ Object



90
91
92
93
94
95
# File 'lib/assette/config.rb', line 90

def find_file_from_relative_path path
  a = Assette.config.file_paths.find do |pp|
    File.exist?(File.join(pp,path))
  end
  File.join(a,path)
end

#find_target_from_relative_path(path) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/assette/config.rb', line 97

def find_target_from_relative_path path
  Assette.config.file_paths.find do |p|
    Assette::Reader.possible_targets(File.join(p,path)).find do |pp|
      File.exist?(pp)
    end
  end
end

#minify?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/assette/config.rb', line 117

def minify?
  !!minify
end

#minifying?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/assette/config.rb', line 121

def minifying?
  !!(@minifying && minify?)
end

#prod?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/assette/config.rb', line 82

def prod?
  env == 'production'
end