Class: Extruder::Main

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

Overview

The main Extruder class, this is where everything gets tied together.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMain

Initalize matches and inputs on instantiation.



50
51
52
53
# File 'lib/extruder/main.rb', line 50

def initialize
  @matches = []
  @inputs = {}
end

Instance Attribute Details

#inputsHash[String, String]

Returns the directory paths for the input files and their matching globs.

Returns:

  • (Hash[String, String])

    the directory paths for the input files and their matching globs.



37
38
39
# File 'lib/extruder/main.rb', line 37

def inputs
  @inputs
end

#matches[{:glob=>"",:filters=>[Filter,Filter]}]

Returns this Extruder’s matches and the filters they contain.

Returns:

  • ([{:glob=>"",:filters=>[Filter,Filter]}])

    this Extruder’s matches and the filters they contain.



41
42
43
# File 'lib/extruder/main.rb', line 41

def matches
  @matches
end

#output_rootString

Returns this Extruder’s output directory.

Returns:

  • (String)

    this Extruder’s output directory



44
45
46
# File 'lib/extruder/main.rb', line 44

def output_root
  @output_root
end

Instance Method Details

#add_filter(filter) ⇒ Object

Add a filter to the current match.

Parameters:



94
95
96
97
98
# File 'lib/extruder/main.rb', line 94

def add_filter(filter)
  # add a match that accepts everything if none are specified
  add_match('**/*') if @matches.empty?
  @matches.last[:filters].push(filter)
end

#add_input(root, pattern = '**/*') ⇒ Object

Add an input directory, optionally filtering which files within the input directory are included.

Parameters:

  • root (String)

    the input root directory; required

  • pattern (String) (defaults to: '**/*')

    a pattern to match within root; optional; defaults to “*/



74
75
76
# File 'lib/extruder/main.rb', line 74

def add_input(root, pattern='**/*')
  @inputs[root] = pattern
end

#add_match(glob) ⇒ Object

Add a match and initialize it’s filters to an array.

Parameters:

  • glob (String)

    the file glob to match with



84
85
86
# File 'lib/extruder/main.rb', line 84

def add_match(glob)
  @matches.push({:glob=>glob,:filters=>[]})
end

#input_filesArray[FileWrapper]

Return a sorted array of FileWrappers representing all files in the input paths.

Returns:



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/extruder/main.rb', line 107

def input_files

  if inputs.empty?
    raise Extruder::Error, "You cannot get input files without " \
                           "first providing an input root"
  end

  result = []

  @inputs.each do |root, glob|
    expanded_root = File.expand_path(root)
    files = Dir[File.join(expanded_root, glob)].select { |f| File.file?(f) }

    files.each do |file|
      relative_path = file.sub(%r{^#{Regexp.escape(expanded_root)}/}, '')
      result << FileWrapper.new(expanded_root, relative_path)
    end
  end

  result.sort
end

#resultFileSet

Create a FileSet from #input_files, iterate through matches, running the filters on the files they contain.

Returns:



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/extruder/main.rb', line 136

def result
  # create a fileset to process filters with
  fileset = FileSet.new(input_files)

  matches.each do |match|

    # set match on fileset for filter
    fileset.glob = match[:glob]

    # get files for filter (and remove them from fileset)
    files = fileset.extract_eligible

    # add the results of this match to the fileset
    fileset.files += match[:filters].map do |filter|

      # hand filter its files
      filter.files = files

      # tell filter where to save them
      filter.output_root = output_root

      # process files and return them for another loop
      files = filter.process
    end.flatten
  end

  fileset
end