Class: Extruder::FileSet

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

Overview

This class contains an array of FileWrappers which can be searched with a glob.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(files, glob = '**/*') ⇒ FileSet

Returns a new instance of FileSet.



21
22
23
24
# File 'lib/extruder/file_set.rb', line 21

def initialize(files, glob='**/*')
  @files = files
  self.glob = glob
end

Instance Attribute Details

#filesArray[FileWrapper]

Returns an array of FileWrapped files.

Returns:

  • (Array[FileWrapper])

    an array of FileWrapped files



13
14
15
# File 'lib/extruder/file_set.rb', line 13

def files
  @files
end

#globString

Returns a glob that a set of input files are matched against.

Returns:

  • (String)

    a glob that a set of input files are matched against



16
17
18
# File 'lib/extruder/file_set.rb', line 16

def glob
  @glob
end

#patternString (readonly)

Returns regular expression pattern to match against input files.

Returns:

  • (String)

    regular expression pattern to match against input files



19
20
21
# File 'lib/extruder/file_set.rb', line 19

def pattern
  @pattern
end

Instance Method Details

#build_pattern(glob) ⇒ Object

Build a regular expression pattern from a file glob that can be used to narrow a selection of files from a Extruder.



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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/extruder/file_set.rb', line 33

def build_pattern(glob)

  scanner = StringScanner.new(glob)

  output, pos = "", 0

  # keep scanning until end of String
  until scanner.eos?

    # look for **/, *, {...}, or the end of the string
    new_chars = scanner.scan_until %r{
        \*\*/
      | /\*\*/
      | \*
      | \{[^\}]*\}
      | $
    }x

    # get the new part of the string up to the match
    before = new_chars[0, new_chars.size - scanner.matched_size]

    # get the match and new position
    match = scanner.matched
    pos = scanner.pos

    # add any literal characters to the output
    output << Regexp.escape(before) if before

    output << case match
    when "/**/"
      # /**/ matches either a "/" followed by any number
      # of characters or a single "/"
      "(/.*|/)"
    when "**/"
      # **/ matches the beginning of the path or
      # any number of characters followed by a "/"
      "(^|.*/)"
    when "*"
      # * matches any number of non-"/" characters
      "[^/]*"
    when /\{.*\}/
      # {...} is split over "," and glued back together
      # as an or condition
      "(" + match[1...-1].gsub(",", "|") + ")"
    else String
      # otherwise, we've grabbed until the end
      match
    end
  end

  if glob.include?("/")
    # if the pattern includes a /, it must match the
    # entire input, not just the end.
    @pattern = Regexp.new("^#{output}$", "i")
  else
    # anchor the pattern either at the beginning of the
    # path or at any "/" character
    @pattern = Regexp.new("(^|/)#{output}$", "i")
  end
end

#createObject

Write all files in this Extruder::FileSet to their output_root.



134
135
136
137
138
# File 'lib/extruder/file_set.rb', line 134

def create
  will_save.each do |file|
    file.create
  end
end

#eligible_filesArray<FileWrapper>

A list of eligible files based on the current glob

Returns:



100
101
102
103
104
# File 'lib/extruder/file_set.rb', line 100

def eligible_files
  files.select do |file|
    file.path =~ @pattern
  end
end

#extract_eligibleArray<FileWrapper>

Remove eligible files from this Extruder::FileSet and return them

Returns:



112
113
114
115
116
# File 'lib/extruder/file_set.rb', line 112

def extract_eligible
  keep = eligible_files
  @files -= keep
  keep
end

#will_saveArray<FileWrapper>

Select files in this Extruder::FileSet which will be saved to disk if #create is run. The save flag is set true for any file that passed through a filter.

Returns:



126
127
128
# File 'lib/extruder/file_set.rb', line 126

def will_save
  files.select { |file| file.save }
end