Class: JsonManifest

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ JsonManifest

Returns a new instance of JsonManifest.



64
65
66
67
# File 'lib/baidu_ueditor_rails/asset_manifest.rb', line 64

def initialize(file)
  @file = file
  @manifest = MultiJson.load(File.read(file))
end

Class Method Details

.try(manifest_path) ⇒ Object



59
60
61
62
# File 'lib/baidu_ueditor_rails/asset_manifest.rb', line 59

def self.try(manifest_path)
  paths = Dir[File.join(manifest_path, "manifest*.json")]
  new(paths.first) if paths.any?
end

Instance Method Details

#append(logical_path, file) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/baidu_ueditor_rails/asset_manifest.rb', line 69

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

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

#dumpObject



105
106
107
# File 'lib/baidu_ueditor_rails/asset_manifest.rb', line 105

def dump
  MultiJson.dump(@manifest)
end

#each(pattern) ⇒ Object



95
96
97
98
99
# File 'lib/baidu_ueditor_rails/asset_manifest.rb', line 95

def each(pattern)
  @manifest["assets"].each_key do |asset|
    yield asset if asset =~ pattern
  end
end

#remove(logical_path) ⇒ Object



81
82
83
84
85
# File 'lib/baidu_ueditor_rails/asset_manifest.rb', line 81

def remove(logical_path)
  if digested = @manifest["assets"].delete(logical_path)
    @manifest["files"].delete(digested)
  end
end

#remove_digest(logical_path) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/baidu_ueditor_rails/asset_manifest.rb', line 87

def remove_digest(logical_path)
  if digested = @manifest["assets"][logical_path]
    @manifest["assets"][logical_path] = logical_path
    @manifest["files"][logical_path] = @manifest["files"].delete(digested).tap { |f| f["digest"] = nil }
    yield digested, logical_path if block_given?
  end
end

#to_sObject



101
102
103
# File 'lib/baidu_ueditor_rails/asset_manifest.rb', line 101

def to_s
  dump
end

#writeObject



109
110
111
# File 'lib/baidu_ueditor_rails/asset_manifest.rb', line 109

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