Class: FileDep

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

Overview

File dependencies for commands

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(glob, type) ⇒ FileDep

Returns a new instance of FileDep.



7
8
9
10
11
12
13
14
15
# File 'lib/file_dep.rb', line 7

def initialize(glob, type)
  # Can be a string like "main.c" or "src/*.c", or an array like
  # ["src/main.c", "src/lib/*.c"]
  @glob = glob
  # :each or :all
  @type = type
  # SingleFile, or MultiFile
  @file_obj = nil
end

Instance Attribute Details

#typeObject

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#each_file(&fn) ⇒ Object

Run fn for each file, passing the file name to fn



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/file_dep.rb', line 18

def each_file(&fn)
  # Check if already a file object collected
  if !@file_obj.nil?
    if @type == :each
      # @file_obj = []SingleFile
      @file_obj.each do |file_o|
        fn.call(file_o.path)
      end
    else
      # @file_obj = MultiFiles
      @file_obj.each do |file|
        fn.call(file)
      end
    end

    return
  end

  # Collect the file object
  
  # initialize files as an empty array. This will contain full paths to
  # files, which well then be put into @file_obj as either a []SingleFile
  # or MultiFiles
  files = []
  
  globs = nil
  if @glob.respond_to? :each
    # array
    globs = @glob
  else
    # string
    globs = [@glob]
  end

  globs.each do |glob|
    Dir[glob].each do |file|
      fn.call(file)
      files << file
    end
  end

  # set the @file_obj
  if @type == :each
    @file_obj = files.map { |file| SingleFile.new file }
  else
    @file_obj = MultiFiles.new files
  end
end

#filesObject

retuns the file/files object(s) will return either []SingleFile or MultiFiles



69
70
71
72
73
74
75
76
# File 'lib/file_dep.rb', line 69

def files
  if @file_obj.nil?
    # Collect file_obj first
    self.each_file { |f| }
  end

  return @file_obj
end