Class: Cask::CaskLoader::FromPathLoader Private

Inherits:
FromContentLoader show all
Defined in:
Library/Homebrew/cask/cask_loader.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Loads a cask from a path.

Direct Known Subclasses

FromTapPathLoader, FromURILoader, NullLoader

Instance Attribute Summary collapse

Attributes inherited from FromContentLoader

#content

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ FromPathLoader

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

rubocop:disable Lint/MissingSuper


56
57
58
59
60
61
# File 'Library/Homebrew/cask/cask_loader.rb', line 56

def initialize(path) # rubocop:disable Lint/MissingSuper
  path = Pathname(path).expand_path

  @token = path.basename(".rb").to_s
  @path = path
end

Instance Attribute Details

#pathObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


54
55
56
# File 'Library/Homebrew/cask/cask_loader.rb', line 54

def path
  @path
end

#tokenObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


54
55
56
# File 'Library/Homebrew/cask/cask_loader.rb', line 54

def token
  @token
end

Class Method Details

.can_load?(ref) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)

49
50
51
52
# File 'Library/Homebrew/cask/cask_loader.rb', line 49

def self.can_load?(ref)
  path = Pathname(ref)
  path.extname == ".rb" && path.expand_path.exist?
end

Instance Method Details

#load(config:) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'Library/Homebrew/cask/cask_loader.rb', line 63

def load(config:)
  raise CaskUnavailableError.new(token, "'#{path}' does not exist.")  unless path.exist?
  raise CaskUnavailableError.new(token, "'#{path}' is not readable.") unless path.readable?
  raise CaskUnavailableError.new(token, "'#{path}' is not a file.")   unless path.file?

  @content = path.read(encoding: "UTF-8")
  @config = config

  begin
    instance_eval(content, path).tap do |cask|
      raise CaskUnreadableError.new(token, "'#{path}' does not contain a cask.") unless cask.is_a?(Cask)
    end
  rescue NameError, ArgumentError, ScriptError => e
    error = CaskUnreadableError.new(token, e.message)
    error.set_backtrace e.backtrace
    raise error
  end
end