Class: Gin::AssetManifest

Inherits:
Object
  • Object
show all
Defined in:
lib/gin/asset_manifest.rb

Defined Under Namespace

Classes: Asset

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filepath, render_dir, asset_globs) ⇒ AssetManifest

Returns a new instance of AssetManifest.


55
56
57
58
59
60
61
62
63
# File 'lib/gin/asset_manifest.rb', line 55

def initialize filepath, render_dir, asset_globs
  @staged = []
  @assets = {}
  @filepath = filepath
  @render_dir = render_dir
  @asset_globs = asset_globs

  load_file! if File.file?(@filepath)
end

Instance Attribute Details

#asset_globsObject

Returns the value of attribute asset_globs


52
53
54
# File 'lib/gin/asset_manifest.rb', line 52

def asset_globs
  @asset_globs
end

#assetsObject (readonly)

Returns the value of attribute assets


51
52
53
# File 'lib/gin/asset_manifest.rb', line 51

def assets
  @assets
end

#filepathObject

Returns the value of attribute filepath


51
52
53
# File 'lib/gin/asset_manifest.rb', line 51

def filepath
  @filepath
end

#render_dirObject

Returns the value of attribute render_dir


52
53
54
# File 'lib/gin/asset_manifest.rb', line 52

def render_dir
  @render_dir
end

Instance Method Details

#asset_outdated?(asset_file, checked = []) ⇒ Boolean

Returns:

  • (Boolean)

113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/gin/asset_manifest.rb', line 113

def asset_outdated? asset_file, checked=[]
  # Check for circular dependencies
  return false if checked.include?(asset_file)
  checked << asset_file

  asset = @assets[asset_file]
  return true if !asset ||
    asset.target_file && !asset.target_file.start_with?(@render_dir) ||
    asset.outdated?

  asset.dependencies.any? do |path|
    return true if asset_outdated?(path, checked)
  end
end

#commit!Object


101
102
103
104
105
# File 'lib/gin/asset_manifest.rb', line 101

def commit!
  until @staged.empty?
    set(*@staged.shift)
  end
end

#delete(asset_file) ⇒ Object


96
97
98
# File 'lib/gin/asset_manifest.rb', line 96

def delete asset_file
  @assets.delete asset_file.to_s
end

#load_file!Object


170
171
172
173
174
175
176
177
# File 'lib/gin/asset_manifest.rb', line 170

def load_file!
  yaml = YAML.load_file(@filepath)
  @assets.clear

  yaml.each do |path, info|
    @assets[path] = Asset.new path, info
  end
end

#outdated?Boolean

Returns:

  • (Boolean)

108
109
110
# File 'lib/gin/asset_manifest.rb', line 108

def outdated?
  source_changed? || @assets.keys.any?{|f| asset_outdated?(f) }
end

#save_file!Object


165
166
167
# File 'lib/gin/asset_manifest.rb', line 165

def save_file!
  File.open(@filepath, 'w'){|f| f.write self.to_hash.to_yaml }
end

#set(asset_file, target_file, dependencies = []) ⇒ Object


66
67
68
69
70
71
72
73
74
75
76
# File 'lib/gin/asset_manifest.rb', line 66

def set asset_file, target_file, dependencies=[]
  squash_similar_targets(target_file)

  asset = @assets[asset_file] =
    Asset.new(asset_file, :target_file => target_file)

  Array(dependencies).each do |path|
    @assets[path] ||= Asset.new(path)
    asset.dependencies << path
  end
end

#source_changed?Boolean

Returns:

  • (Boolean)

129
130
131
# File 'lib/gin/asset_manifest.rb', line 129

def source_changed?
  source_files.sort != @assets.keys.sort
end

#source_dirsObject


142
143
144
# File 'lib/gin/asset_manifest.rb', line 142

def source_dirs
  Dir.glob(@asset_globs).map{|pa| pa[-1] == ?/ ? pa[0..-2] : pa }.uniq
end

#source_filesObject


134
135
136
137
138
139
# File 'lib/gin/asset_manifest.rb', line 134

def source_files
  globs = @asset_globs.map{|gl|
            (gl =~ /\.(\*|\w+)$/) ? gl : File.join(gl, '**', '*') }

  Dir.glob(globs).reject{|path| !File.file?(path) }.uniq
end

#squash_similar_targets(target_file) ⇒ Object


79
80
81
82
83
84
85
86
87
88
# File 'lib/gin/asset_manifest.rb', line 79

def squash_similar_targets target_file
  matcher = Regexp.escape( target_file.sub(/-\w+\.(\w+)$/, '') )
  matcher = Regexp.new "#{matcher}-\\w+\\.#{$1}"

  @assets.each do |name, asset|
    if asset.target_file && matcher =~ asset.target_file
      asset.target_file = nil
    end
  end
end

#stage(asset_file, target_file, dependencies = []) ⇒ Object


91
92
93
# File 'lib/gin/asset_manifest.rb', line 91

def stage asset_file, target_file, dependencies=[]
  @staged << [asset_file, target_file, dependencies]
end

#to_hashObject


147
148
149
150
151
152
153
154
155
# File 'lib/gin/asset_manifest.rb', line 147

def to_hash
  assets_hash = {}

  @assets.each do |path, asset|
    assets_hash[path] = asset.to_hash
  end

  assets_hash
end