Class: DistributedCache::Manifest

Inherits:
Object
  • Object
show all
Includes:
Fields
Defined in:
lib/distributed_cache/manifest.rb

Defined Under Namespace

Modules: Fieldst

Constant Summary collapse

DEFAULT_DATE_FORMAT =
'%Y%m%d'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, cache_name, cache_version) ⇒ Manifest

Returns a new instance of Manifest.



16
17
18
19
20
21
22
23
24
25
# File 'lib/distributed_cache/manifest.rb', line 16

def initialize(config, cache_name, cache_version)
  @config = config
  @date = {
    CACHE_NAME => cache_name,
    CACHE_VERSION => cache_version,
    DATE => date,
    FILES => []
  }
  load
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



3
4
5
# File 'lib/distributed_cache/manifest.rb', line 3

def config
  @config
end

Instance Method Details

#[](key) ⇒ Object



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

def [](key)
  @data[key]
end

#[]=(key, value) ⇒ Object



35
36
37
# File 'lib/distributed_cache/manifest.rb', line 35

def []=(key, value)
  @data[key] = value
end

#bundle_dirObject



61
62
63
64
65
# File 'lib/distributed_cache/manifest.rb', line 61

def bundle_dir
  "#{config.bundle_dir}/#{cache_path}".tap do |d|
    FileUtils.mkdir_p(d) unless File.exists?(d)
  end
end

#cache_dirObject



67
68
69
70
71
# File 'lib/distributed_cache/manifest.rb', line 67

def cache_dir
  "#{config.cache_dir}/#{cache_name}".tap do |d|
    FileUtils.mkdir_p(d) unless File.exists?(d)
  end
end

#cache_pathObject

this will be used as the remote file path and the local bundle directory



49
50
51
# File 'lib/distributed_cache/manifest.rb', line 49

def cache_path
  "#{cache_name}/#{date}/#{cache_version}"
end

#dateObject



57
58
59
# File 'lib/distributed_cache/manifest.rb', line 57

def date
  Time.now.strftime date_format
end

#date_formatObject



53
54
55
# File 'lib/distributed_cache/manifest.rb', line 53

def date_format
  @data[DATE_FORMAT] || DEFAULT_DATE_FORMAT
end

#filesObject



27
28
29
# File 'lib/distributed_cache/manifest.rb', line 27

def files
  data[FILES]
end

#full?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/distributed_cache/manifest.rb', line 44

def full?
  ! partial?
end

#latest_bundle_dirObject



77
78
79
# File 'lib/distributed_cache/manifest.rb', line 77

def latest_bundle_dir
  "#{config.bundle_dir}/#{cache_name}/latest"
end

#manifest_fileObject



73
74
75
# File 'lib/distributed_cache/manifest.rb', line 73

def manifest_file
  "#{bundle_dir}/manifest.yml"
end

#partial?Boolean

if the manifest file exists we only need todo a partial update of the cache

Returns:

  • (Boolean)


40
41
42
# File 'lib/distributed_cache/manifest.rb', line 40

def partial?
  File.exists? manifest_file
end

#saveObject



81
82
83
84
85
# File 'lib/distributed_cache/manifest.rb', line 81

def save
  File.open(manifest_file, 'w+') do |f|
    f.write @data.to_yaml
  end
end

#update_latest_bundle_dirObject



87
88
89
90
# File 'lib/distributed_cache/manifest.rb', line 87

def update_latest_bundle_dir
  File.unlink(latest_bundle_dir) if File.exists?(latest_bundle_dir)
  FileUtils.symlink bundle_dir, latest_bundle_dir
end