Class: Cask::CaskLoader::FromPathLoader

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

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

Returns a new instance of FromPathLoader



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

def initialize(path)
  path = Pathname(path).expand_path

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

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path



45
46
47
# File 'brew/Library/Homebrew/cask/cask_loader.rb', line 45

def path
  @path
end

#tokenObject (readonly)

Returns the value of attribute token



45
46
47
# File 'brew/Library/Homebrew/cask/cask_loader.rb', line 45

def token
  @token
end

Class Method Details

.can_load?(ref) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
43
# File 'brew/Library/Homebrew/cask/cask_loader.rb', line 40

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

Instance Method Details

#loadObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'brew/Library/Homebrew/cask/cask_loader.rb', line 54

def load
  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 = IO.read(path)

  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
    raise CaskUnreadableError.new(token, e.message)
  end
end