Class: ElmInstall::Cache
- Inherits:
-
Object
- Object
- ElmInstall::Cache
- Extended by:
- Forwardable
- Defined in:
- lib/elm_install/cache.rb
Overview
This class is responsible for maintaining a cache of all the repositories their versions and their dependencies.
By default the clones of the repositories live in the users home directory (~/.elm-install), this can be changed with the ‘directory` option.
Instance Method Summary collapse
-
#dependency(package, version, constraint) ⇒ Object
Adds a new dependency to the cache for a given package & version combination.
-
#directory ⇒ Object
Returns the directory where the cache is stored.
-
#ensure_package(package) ⇒ Object
Ensures that a package exists in the cache.
-
#ensure_version(package, version) ⇒ Object
Ensures that a package & version combination exists in the cache.
-
#initialize(options = {}) ⇒ Cache
constructor
Initializes a new cache with the given options.
-
#load ⇒ Object
Loads a cache from the json file.
-
#package?(package) ⇒ Boolean
Returns if there is a package in the cache (with at least one version).
-
#repository(path) ⇒ Object
Returns the Git repository of the given package in a ready to use state.
-
#repository_path(package) ⇒ Object
Returns the path to the repository of the given package.
-
#save ⇒ Object
Saves the cache into the json file.
Constructor Details
#initialize(options = {}) ⇒ Cache
Initializes a new cache with the given options.
16 17 18 19 20 |
# File 'lib/elm_install/cache.rb', line 16 def initialize( = {}) = @cache = {} load end |
Instance Method Details
#dependency(package, version, constraint) ⇒ Object
Adds a new dependency to the cache for a given package & version combination.
46 47 48 49 |
# File 'lib/elm_install/cache.rb', line 46 def dependency(package, version, constraint) ensure_package version @cache[package][version] << constraint end |
#directory ⇒ Object
Returns the directory where the cache is stored.
35 36 37 |
# File 'lib/elm_install/cache.rb', line 35 def directory [:directory] || File.join(Dir.home, '.elm-install') end |
#ensure_package(package) ⇒ Object
Ensures that a package exists in the cache.
58 59 60 |
# File 'lib/elm_install/cache.rb', line 58 def ensure_package(package) @cache[package] ||= {} end |
#ensure_version(package, version) ⇒ Object
Ensures that a package & version combination exists in the cache.
52 53 54 55 |
# File 'lib/elm_install/cache.rb', line 52 def ensure_version(package, version) ensure_package package @cache[package][version] ||= [] end |
#load ⇒ Object
Loads a cache from the json file.
28 29 30 31 32 |
# File 'lib/elm_install/cache.rb', line 28 def load @cache = JSON.parse(File.read(file)) rescue @cache = {} end |
#package?(package) ⇒ Boolean
Returns if there is a package in the cache (with at least one version).
40 41 42 |
# File 'lib/elm_install/cache.rb', line 40 def package?(package) @cache.key?(package) end |
#repository(path) ⇒ Object
Returns the Git repository of the given package in a ready to use state.
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/elm_install/cache.rb', line 68 def repository(path) repo_path = repository_path(path) if Dir.exist?(repo_path) repo = Git.open(repo_path) repo.reset_hard repo else Git.clone(path, repo_path) end end |
#repository_path(package) ⇒ Object
Returns the path to the repository of the given package.
63 64 65 |
# File 'lib/elm_install/cache.rb', line 63 def repository_path(package) File.join(directory, package) end |
#save ⇒ Object
Saves the cache into the json file.
23 24 25 |
# File 'lib/elm_install/cache.rb', line 23 def save File.binwrite(file, JSON.pretty_generate(@cache)) end |