Class: Kontena::StacksCache::CachedStack
- Inherits:
-
Object
- Object
- Kontena::StacksCache::CachedStack
- Defined in:
- lib/kontena/stacks_cache.rb
Instance Attribute Summary collapse
-
#stack ⇒ Object
Returns the value of attribute stack.
-
#version ⇒ Object
Returns the value of attribute version.
Instance Method Summary collapse
- #cached? ⇒ Boolean
- #delete ⇒ Object
-
#initialize(stack, version = nil) ⇒ CachedStack
constructor
A new instance of CachedStack.
- #load ⇒ Object
- #path ⇒ Object
- #read ⇒ Object
- #write(content) ⇒ Object
Constructor Details
#initialize(stack, version = nil) ⇒ CachedStack
Returns a new instance of CachedStack.
13 14 15 16 17 18 19 |
# File 'lib/kontena/stacks_cache.rb', line 13 def initialize(stack, version = nil) unless version stack, version = stack.split(':', 2) end @stack = stack @version = version end |
Instance Attribute Details
#stack ⇒ Object
Returns the value of attribute stack.
10 11 12 |
# File 'lib/kontena/stacks_cache.rb', line 10 def stack @stack end |
#version ⇒ Object
Returns the value of attribute version.
11 12 13 |
# File 'lib/kontena/stacks_cache.rb', line 11 def version @version end |
Instance Method Details
#cached? ⇒ Boolean
42 43 44 45 |
# File 'lib/kontena/stacks_cache.rb', line 42 def cached? return false unless version File.exist?(path) end |
#delete ⇒ Object
38 39 40 |
# File 'lib/kontena/stacks_cache.rb', line 38 def delete File.unlink(path) end |
#load ⇒ Object
25 26 27 |
# File 'lib/kontena/stacks_cache.rb', line 25 def load YAML.safe_load(read) end |
#path ⇒ Object
47 48 49 50 51 |
# File 'lib/kontena/stacks_cache.rb', line 47 def path path = File.(File.join(base_path, "#{stack}-#{version}.yml")) raise "Path traversal attempted" unless path.start_with?(base_path) path end |
#read ⇒ Object
21 22 23 |
# File 'lib/kontena/stacks_cache.rb', line 21 def read File.read(path) end |
#write(content) ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/kontena/stacks_cache.rb', line 29 def write(content) raise ArgumentError, "Stack name and version required" unless @stack && @version unless File.directory?(File.dirname(path)) require 'fileutils' FileUtils.mkdir_p(File.dirname(path)) end File.write(path, content) end |