Class: RailsWayback::ViewSource

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_wayback/view_source.rb

Overview

Materialises the view/asset directories of a given git ref into the refs cache under tmp/, so a controller can prepend those directories as view paths and render the historical templates.

Only the directories listed in configuration.view_paths and configuration.asset_paths are pulled from the ref. Nothing is executed, and the developer's real files are never mutated.

Constant Summary collapse

MATERIALIZATION_VERSION =

Bumped whenever the materialization format changes so stale cached refs from earlier gem versions get rebuilt instead of being reused.

CacheInventory::MATERIALIZATION_VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration: RailsWayback.configuration, git: RailsWayback::Git.new) ⇒ ViewSource



26
27
28
29
30
# File 'lib/rails_wayback/view_source.rb', line 26

def initialize(configuration: RailsWayback.configuration, git: RailsWayback::Git.new)
  @configuration = configuration
  @git = git
  @cache_inventory = CacheInventory.new(configuration: configuration)
end

Instance Attribute Details

#configuration ⇒ Object (readonly)

Returns the value of attribute configuration.



24
25
26
# File 'lib/rails_wayback/view_source.rb', line 24

def configuration
  @configuration
end

#git ⇒ Object (readonly)

Returns the value of attribute git.



24
25
26
# File 'lib/rails_wayback/view_source.rb', line 24

def git
  @git
end

Instance Method Details

#cache_snapshot ⇒ Object



69
70
71
# File 'lib/rails_wayback/view_source.rb', line 69

def cache_snapshot
  with_cache_lock(File::LOCK_SH) { cache_inventory.snapshot }
end

#cleanup! ⇒ Object



62
63
64
65
66
67
# File 'lib/rails_wayback/view_source.rb', line 62

def cleanup!
  with_cache_lock(File::LOCK_EX) do
    FileUtils.rm_rf(configuration.refs_cache_path)
    FileUtils.rm_rf(configuration.cache_root_path.join("locks"))
  end
end

#materialize(ref) ⇒ Object



32
33
34
35
# File 'lib/rails_wayback/view_source.rb', line 32

def materialize(ref)
  sha = git.resolve_ref(ref)
  with_cache_lock(File::LOCK_SH) { materialize_locked(sha).first }
end

#prune!(preserve: []) ⇒ Object



73
74
75
# File 'lib/rails_wayback/view_source.rb', line 73

def prune!(preserve: [])
  with_cache_lock(File::LOCK_EX) { cache_inventory.prune!(preserve: preserve) }
end

#view_root_for(ref) ⇒ Object



37
38
39
# File 'lib/rails_wayback/view_source.rb', line 37

def view_root_for(ref)
  view_roots(materialize(ref))
end

#with_materialization(ref) ⇒ Object

Keeps a shared cache lease while the caller uses any files from the materialization. Pruning and cleanup take an exclusive lease, so they wait until every active user has finished before removing files.



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rails_wayback/view_source.rb', line 44

def with_materialization(ref)
  sha = git.resolve_ref(ref)
  materialized = false

  with_cache_lock(File::LOCK_SH) do
    target, materialized = materialize_locked(sha)
    yield target
  end
ensure
  automatic_prune if materialized
end

#with_view_roots(ref) ⇒ Object



56
57
58
59
60
# File 'lib/rails_wayback/view_source.rb', line 56

def with_view_roots(ref)
  with_materialization(ref) do |target|
    yield view_roots(target), target
  end
end