Class: Fetchers::Local

Inherits:
Object
  • Object
show all
Defined in:
lib/fetchers/local.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target) ⇒ Local

Returns a new instance of Local.



46
47
48
# File 'lib/fetchers/local.rb', line 46

def initialize(target)
  @target = target
end

Class Method Details

.resolve(target) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/fetchers/local.rb', line 10

def self.resolve(target)
  local_path = if target.is_a?(String)
                 resolve_from_string(target)
               elsif target.is_a?(Hash)
                 resolve_from_hash(target)
               end

  if local_path
    new(local_path)
  end
end

.resolve_from_hash(target) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/fetchers/local.rb', line 22

def self.resolve_from_hash(target)
  if target.key?(:path)
    local_path = target[:path]
    if target.key?(:cwd)
      local_path = File.expand_path(local_path, target[:cwd])
    end
    local_path
  end
end

.resolve_from_string(target) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/fetchers/local.rb', line 32

def self.resolve_from_string(target)
  # Support "urls" in the form of file://
  if target.start_with?('file://')
    target = target.gsub(%r{^file://}, '')
  else
    # support for windows paths
    target = target.tr('\\', '/')
  end

  if File.exist?(target)
    target
  end
end

Instance Method Details

#archive_pathObject



54
55
56
# File 'lib/fetchers/local.rb', line 54

def archive_path
  @target
end

#cache_keyObject



62
63
64
# File 'lib/fetchers/local.rb', line 62

def cache_key
  sha256.to_s
end

#fetch(_path) ⇒ Object



50
51
52
# File 'lib/fetchers/local.rb', line 50

def fetch(_path)
  archive_path
end

#resolved_sourceObject



71
72
73
74
75
# File 'lib/fetchers/local.rb', line 71

def resolved_source
  h = { path: @target }
  h[:sha256] = sha256 if sha256
  h
end

#sha256Object



66
67
68
69
# File 'lib/fetchers/local.rb', line 66

def sha256
  return nil if File.directory?(@target)
  @archive_shasum ||= Digest::SHA256.hexdigest File.read(@target)
end

#writable?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/fetchers/local.rb', line 58

def writable?
  File.directory?(@target)
end