class Epuber::Compiler::FileFinders::Imaginary
Attributes
@return [DirEntry]
Public Class Methods
Source
# File lib/epuber/compiler/file_finders/imaginary.rb, line 59 def initialize(source_path) super @root = DirEntry.new('/') make_dir_p(File.expand_path(source_path)) end
Calls superclass method
Epuber::Compiler::FileFinders::Abstract::new
Source
# File lib/epuber/compiler/file_finders/imaginary.rb, line 111 def path_parts(path) path.split(File::SEPARATOR).reject(&:empty?) end
Public Instance Methods
Source
# File lib/epuber/compiler/file_finders/imaginary.rb, line 99 def __core_file?(path) components = self.class.path_parts(path) current = root components.each do |dir| current = current.respond_to?(:[]) ? current[dir] : nil end current.is_a?(FileEntry) end
Source
# File lib/epuber/compiler/file_finders/imaginary.rb, line 92 def __core_find_files_from_pattern(pattern) parts = self.class.path_parts(pattern) found_entries = find_recurser(root, parts).flatten file_entries = found_entries.reject { |entry| entry.is_a?(DirEntry) } file_entries.map(&:absolute_path) end
Source
# File lib/epuber/compiler/file_finders/imaginary.rb, line 84 def add_file(file_path) file_path = File.expand_path(file_path, source_path) *path, file_name = self.class.path_parts(file_path) make_dir_p(path)[file_name] = FileEntry.new(file_name, file_path) end
@param
Source
# File lib/epuber/compiler/file_finders/imaginary.rb, line 70 def make_dir_p(path) components = path.is_a?(Array) ? path : self.class.path_parts(path) current = root components.each do |dir| entry = current[dir] current[dir] = entry = DirEntry.new(dir) if entry.nil? current = entry end current end
@param [String | Array<String>] path path to folder to create
@return [DirEntry] dir entry for given path
Private Instance Methods
Source
# File lib/epuber/compiler/file_finders/imaginary.rb, line 157 def directories_under(dir) children = dir.entries.values.select { |f| f.is_a?(DirEntry) } (Array(dir) + children + children.map { |c| directories_under(c) }).flatten.uniq end
@param [DirEntry] dir
@return [Array<DirEntry>]
Source
# File lib/epuber/compiler/file_finders/imaginary.rb, line 118 def find_recurser(dir, parts) return [] unless dir.respond_to? :[] pattern, *parts = parts return [] if pattern.nil? matches = case pattern when '**' case parts when ['*'] parts = [] # end recursion directories_under(dir).map do |d| d.entries.select do |f| (f.is_a?(FileEntry) || f.is_a?(DirEntry)) && f.name.match(/\A(?!\.)/) end end.flatten.uniq when [] parts = [] # end recursion dir.entries.values.flatten.uniq else directories_under(dir) end else regex_body = pattern_to_regex(pattern) dir.entries.select { |k, _v| /\A#{regex_body}\Z/ =~ k }.values end if parts.empty? # we're done recursing matches else matches.map { |entry| find_recurser(entry, parts) } end end
Source
# File lib/epuber/compiler/file_finders/imaginary.rb, line 162 def pattern_to_regex(pattern) pattern.gsub('.', '\.') .gsub('?', '.') .gsub('*', '.*') .gsub('(', '\(') .gsub(')', '\)') .gsub(/\{(.*?)\}/) do "(#{Regexp.last_match[1].gsub(',', '|')})" end .gsub(/\A\./, '(?!\.).') end