Class: Bibliothecary::FileInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/bibliothecary/file_info.rb

Overview

A representation of a file on the filesystem, with location information and package manager information if needed.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(folder_path, full_path, contents = nil) ⇒ FileInfo

If the FileInfo represents an actual file on disk, the contents can be nil and lazy-loaded; we allow contents to be passed in here to allow pulling them from somewhere other than the disk.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/bibliothecary/file_info.rb', line 29

def initialize(folder_path, full_path, contents = nil)
  # Note that cleanpath does NOT touch the filesystem,
  # leaving the lazy-load of file contents as the only
  # time we touch the filesystem.

  full_pathname = Pathname.new(full_path)
  @full_path = full_pathname.cleanpath.to_path

  if folder_path.nil?
    # this is the case where we e.g. have filenames from the GitHub API
    # and don't have a local folder
    @folder_path = nil
    @relative_path = @full_path
  else
    folder_pathname = Pathname.new(folder_path)
    @folder_path = folder_pathname.cleanpath.to_path
    @relative_path = full_pathname.relative_path_from(folder_pathname).cleanpath.to_path
  end

  @contents = contents

  @package_manager = nil
end

Instance Attribute Details

#folder_pathObject (readonly)

Returns the value of attribute folder_path.



7
8
9
# File 'lib/bibliothecary/file_info.rb', line 7

def folder_path
  @folder_path
end

#full_pathObject (readonly)

Returns the value of attribute full_path.



9
10
11
# File 'lib/bibliothecary/file_info.rb', line 9

def full_path
  @full_path
end

#package_managerObject

Returns the value of attribute package_manager.



10
11
12
# File 'lib/bibliothecary/file_info.rb', line 10

def package_manager
  @package_manager
end

#relative_pathObject (readonly)

Returns the value of attribute relative_path.



8
9
10
# File 'lib/bibliothecary/file_info.rb', line 8

def relative_path
  @relative_path
end

Instance Method Details

#contentsObject



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/bibliothecary/file_info.rb', line 12

def contents
  @contents ||=
    begin
      if @folder_path.nil?
        # if we have no folder_path then we aren't dealing with a
        # file that's actually on the filesystem
        nil
      else
        contents = Bibliothecary.utf8_string(File.open(@full_path).read)
      end
    end
end

#groupable?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/bibliothecary/file_info.rb', line 53

def groupable?
  @package_manager&.groupable?(self)
end