Class: Extruder::Main
- Inherits:
-
Object
- Object
- Extruder::Main
- Defined in:
- lib/extruder/main.rb
Overview
The main Extruder class, this is where everything gets tied together.
Instance Attribute Summary collapse
-
#inputs ⇒ Hash[String, String]
The directory paths for the input files and their matching globs.
-
#matches ⇒ [{:glob=>"",:filters=>[Filter,Filter]}]
This Extruder’s matches and the filters they contain.
-
#output_root ⇒ String
This Extruder’s output directory.
Instance Method Summary collapse
-
#add_filter(filter) ⇒ Object
Add a filter to the current match.
-
#add_input(root, pattern = '**/*') ⇒ Object
Add an input directory, optionally filtering which files within the input directory are included.
-
#add_match(glob) ⇒ Object
Add a match and initialize it’s filters to an array.
-
#initialize ⇒ Main
constructor
Initalize matches and inputs on instantiation.
-
#input_files ⇒ Array[FileWrapper]
Return a sorted array of FileWrappers representing all files in the input paths.
-
#result ⇒ FileSet
Create a FileSet from #input_files, iterate through matches, running the filters on the files they contain.
Constructor Details
#initialize ⇒ Main
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
#inputs ⇒ Hash[String, String]
Returns 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.
41 42 43 |
# File 'lib/extruder/main.rb', line 41 def matches @matches end |
#output_root ⇒ String
Returns 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.
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.
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.
84 85 86 |
# File 'lib/extruder/main.rb', line 84 def add_match(glob) @matches.push({:glob=>glob,:filters=>[]}) end |
#input_files ⇒ Array[FileWrapper]
Return a sorted array of FileWrappers representing all files in the input paths.
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| = File.(root) files = Dir[File.join(, glob)].select { |f| File.file?(f) } files.each do |file| relative_path = file.sub(%r{^#{Regexp.escape()}/}, '') result << FileWrapper.new(, relative_path) end end result.sort end |
#result ⇒ FileSet
Create a FileSet from #input_files, iterate through matches, running the filters on the files they contain.
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 |