Class: AssetLibrary

Inherits:
Object
  • Object
show all
Defined in:
lib/asset_library.rb,
lib/asset_library/util.rb,
lib/asset_library/asset.rb,
lib/asset_library/helpers.rb,
lib/asset_library/compiler.rb,
lib/asset_library/asset_module.rb,
lib/asset_library/compiler/base.rb,
lib/asset_library/compiler/closure.rb,
lib/asset_library/compiler/default.rb

Defined Under Namespace

Modules: Compiler, Helpers, Util Classes: Asset, AssetModule

Constant Summary collapse

ConfigurationError =
Class.new(RuntimeError)

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.app_rootObject

Root of your application.

Paths of external programs (if required) are resolved relative to this path.



31
32
33
# File 'lib/asset_library.rb', line 31

def app_root
  @app_root
end

.rootObject

Root directory of your output files.

Output files are resolved relative to this path.



38
39
40
# File 'lib/asset_library.rb', line 38

def root
  @root
end

Class Method Details

.asset_module(key) ⇒ Object



82
83
84
85
86
87
# File 'lib/asset_library.rb', line 82

def asset_module(key)
  module_config = config[:modules][key.to_sym]
  if module_config
    AssetModule.new(key, module_config)
  end
end

.cacheObject



40
41
42
43
# File 'lib/asset_library.rb', line 40

def cache
  return true if @cache.nil?
  @cache
end

.cache=(cache) ⇒ Object



45
46
47
48
# File 'lib/asset_library.rb', line 45

def cache=(cache)
  reset!
  @cache = cache
end

.cache_varsObject



50
51
52
53
# File 'lib/asset_library.rb', line 50

def cache_vars
  # We store cache_vars even if not caching--this is our "globals"
  @cache_vars ||= {}
end

.compiler(asset_module) ⇒ Object



89
90
91
92
93
# File 'lib/asset_library.rb', line 89

def compiler(asset_module)
  type = asset_module.compiler_type
  config = self.config[:compilers][type] || {}
  compilers[type] ||= Compiler.create(type, config)
end

.compilersObject



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

def compilers
  @compilers ||= {}
end

.configObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/asset_library.rb', line 55

def config
  return @config if cache && @config
  ret = if File.exist?(config_path)
    yaml = YAML.load_file(config_path) || {}
    Util::symbolize_hash_keys(yaml)
  else
    {}
  end
  if !ret[:modules] && ret.values.any?{|value| value.is_a?(Hash) && value[:files]}
    warn <<-EOS.gsub(/^ *\|/, '')
      |  WARNING: Your asset library configuration follows a
      |  deprecated format.  Please move all your asset modules
      |  under a "modules:" key, as described in the
      |  documentation.
    EOS
    ret = { :modules => ret }
  end
  ret[:modules] ||= {}
  ret[:compilers] ||= {}
  @config = cache ? ret : nil
  ret
end

.config_pathObject



17
18
19
# File 'lib/asset_library.rb', line 17

def config_path
  @config_path
end

.config_path=(config_path) ⇒ Object



21
22
23
# File 'lib/asset_library.rb', line 21

def config_path=(config_path)
  @config_path = config_path
end

.delete_all_cachesObject



107
108
109
110
111
112
# File 'lib/asset_library.rb', line 107

def delete_all_caches
  asset_modules = config[:modules].keys.collect{|k| AssetLibrary.asset_module(k)}
  asset_modules.each do |m|
    FileUtils.rm_f(m.cache_asset.absolute_path)
  end
end

.reset!Object



114
115
116
117
118
119
# File 'lib/asset_library.rb', line 114

def reset!
  @config = nil
  @cache_vars = nil
  @compilers = nil
  Compiler.reset!
end

.write_all_cachesObject



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/asset_library.rb', line 95

def write_all_caches
  config[:modules].keys.each do |key|
    m = asset_module(key)
    c = compiler(m)
    c.add_asset_module(m)
  end

  compilers.values.each do |compiler|
    compiler.write_all_caches
  end
end