Class: AssetLibrary::AssetModule

Inherits:
Object
  • Object
show all
Defined in:
lib/asset_library/asset_module.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ AssetModule

Returns a new instance of AssetModule.



7
8
9
# File 'lib/asset_library/asset_module.rb', line 7

def initialize(config)
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



5
6
7
# File 'lib/asset_library/asset_module.rb', line 5

def config
  @config
end

Instance Method Details

#assets(format = nil) ⇒ Object

Returns an Array of Assets to include.

Arguments:

extra_suffix: if set, finds files with the given extra suffix


15
16
17
18
19
20
21
# File 'lib/asset_library/asset_module.rb', line 15

def assets(format = nil)
  if format
    assets_with_format(format)
  else
    assets_with_extra_suffix(nil)
  end
end

#assets_with_extra_suffix(extra_suffix) ⇒ Object

Returns an Array of Assets to include.

Arguments:

extra_suffix: if set, finds files with the given extra suffix


27
28
29
30
31
32
33
34
35
36
37
# File 'lib/asset_library/asset_module.rb', line 27

def assets_with_extra_suffix(extra_suffix)
  return nil unless config

  GlobFu.find(
    config[:files],
    :suffix => config[:suffix],
    :extra_suffix => extra_suffix,
    :root => File.join(*([AssetLibrary.root, config[:base]].compact)),
    :optional_suffix => config[:optional_suffix]
  ).collect { |f| Asset.new(f) }
end

#assets_with_format(format) ⇒ Object

Returns an Array of Assets to include.

Calls assets_with_extra_suffix for each suffix in the given format

Arguments:

format: format specified in the config


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

def assets_with_format(format)
  return nil unless config

  extra_suffixes = config[:formats][format.to_sym]
  extra_suffixes.inject([]) { |r, s| r.concat(assets_with_extra_suffix(s)) }
end

#cache_asset(format = nil) ⇒ Object

Returns an Asset representing the cache file



66
67
68
69
# File 'lib/asset_library/asset_module.rb', line 66

def cache_asset(format = nil)
  extra = format ? ".#{format}" : ''
  Asset.new(File.join(AssetLibrary.root, config[:base], "#{config[:cache]}#{extra}.#{config[:suffix]}"))
end

#contents(format = nil) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/asset_library/asset_module.rb', line 52

def contents(format = nil)
  s = StringIO.new

  assets(format).each do |asset|
    File.open(asset.absolute_path, 'r') do |infile|
      s.write(infile.read)
    end
  end
  s.rewind

  s
end

#write_all_cachesObject



77
78
79
80
81
82
# File 'lib/asset_library/asset_module.rb', line 77

def write_all_caches
  write_cache
  (config[:formats] || {}).keys.each do |format|
    write_cache(format)
  end
end

#write_cache(format = nil) ⇒ Object



71
72
73
74
75
# File 'lib/asset_library/asset_module.rb', line 71

def write_cache(format = nil)
  File.open(cache_asset(format).absolute_path, 'w') do |outfile|
    outfile.write(contents(format).read)
  end
end