Class: TinyMCE::Rails::JsonManifest

Inherits:
AssetManifest show all
Defined in:
lib/tinymce/rails/asset_manifest/json_manifest.rb

Instance Attribute Summary

Attributes inherited from AssetManifest

#file

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AssetManifest

#asset_path, #each, load, #to_s

Constructor Details

#initialize(file) ⇒ JsonManifest

Returns a new instance of JsonManifest.



13
14
15
16
# File 'lib/tinymce/rails/asset_manifest/json_manifest.rb', line 13

def initialize(file)
  @file = file
  @manifest = JSON.parse(File.read(file))
end

Class Method Details

.try(manifest_path, pattern = nil) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/tinymce/rails/asset_manifest/json_manifest.rb', line 4

def self.try(manifest_path, pattern=nil)
  if pattern
    paths = Dir[File.join(manifest_path, pattern)]
    new(paths.first) if paths.any?
  elsif File.file?(manifest_path)
    new(manifest_path)
  end
end

Instance Method Details

#append(logical_path, file) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/tinymce/rails/asset_manifest/json_manifest.rb', line 18

def append(logical_path, file)
  stat = File.stat(file)

  assets[logical_path] = logical_path
  files[logical_path] = {
    "logical_path" => logical_path,
    "mtime"        => stat.mtime.iso8601,
    "size"         => stat.size,
    "digest"       => nil
  }
end

#assetsObject



45
46
47
# File 'lib/tinymce/rails/asset_manifest/json_manifest.rb', line 45

def assets
  @manifest["assets"]
end

#dumpObject



53
54
55
# File 'lib/tinymce/rails/asset_manifest/json_manifest.rb', line 53

def dump
  JSON.generate(@manifest)
end

#filesObject



49
50
51
# File 'lib/tinymce/rails/asset_manifest/json_manifest.rb', line 49

def files
  @manifest["files"]
end

#remove(logical_path) ⇒ Object



30
31
32
33
34
# File 'lib/tinymce/rails/asset_manifest/json_manifest.rb', line 30

def remove(logical_path)
  if digested = assets.delete(logical_path)
    files.delete(digested)
  end
end

#remove_digest(logical_path) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/tinymce/rails/asset_manifest/json_manifest.rb', line 36

def remove_digest(logical_path)
  asset_path(logical_path) do |digested, logical_path|
    assets[logical_path] = logical_path
    files[logical_path] = files.delete(digested).tap { |f| f["digest"] = nil }

    yield digested, logical_path if block_given?
  end
end

#writeObject



57
58
59
# File 'lib/tinymce/rails/asset_manifest/json_manifest.rb', line 57

def write
  File.open(@file, "wb") { |f| f.write(dump) }
end