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}.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 {},
  :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.



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

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



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

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



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

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

Instance Method Details

#asset_host(i) ⇒ Object



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

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

#asset_hosts?Boolean

Returns:

  • (Boolean)


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

def asset_hosts?
  !@asset_hosts.empty?
end

#build_targetObject



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

def build_target
  @asset_dir
end

#compiled_path(str) ⇒ Object



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

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)


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

def compiling?
  !!compiling
end

#dev?Boolean

Returns:

  • (Boolean)


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

def dev?
  !prod?
end

#env?(e) ⇒ Boolean

Returns:

  • (Boolean)


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

def env? e
  env == e
end

#find_file_from_relative_path(path) ⇒ Object



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

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



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

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)


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

def minify?
  !!minify
end

#minifying?Boolean

Returns:

  • (Boolean)


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

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

#prod?Boolean

Returns:

  • (Boolean)


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

def prod?
  env == 'production'
end