Class: Bibliothecary::FileInfo

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

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.



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

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.



5
6
7
# File 'lib/bibliothecary/file_info.rb', line 5

def folder_path
  @folder_path
end

#full_pathObject (readonly)

Returns the value of attribute full_path.



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

def full_path
  @full_path
end

#package_managerObject

Returns the value of attribute package_manager.



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

def package_manager
  @package_manager
end

#relative_pathObject (readonly)

Returns the value of attribute relative_path.



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

def relative_path
  @relative_path
end

Instance Method Details

#contentsObject



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

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
        File.open(@full_path).read
      end
    end
end