class Epuber::Compiler::FileFinders::Imaginary

Attributes

root[R]

@return [DirEntry]

Public Class Methods

new(source_path) click to toggle source
# File lib/epuber/compiler/file_finders/imaginary.rb, line 53
def initialize(source_path)
  super
  @root = DirEntry.new('/')

  make_dir_p(File.expand_path(source_path))
end

Private Class Methods

path_parts(path) click to toggle source
# File lib/epuber/compiler/file_finders/imaginary.rb, line 109
def self.path_parts(path)
  path.split(File::SEPARATOR).reject(&:empty?)
end

Public Instance Methods

__core_file?(path) click to toggle source
# File lib/epuber/compiler/file_finders/imaginary.rb, line 96
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
__core_find_files_from_pattern(pattern) click to toggle source
# File lib/epuber/compiler/file_finders/imaginary.rb, line 87
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 do |item|
    item.absolute_path
  end
end
add_file(file_path) click to toggle source

@param

# File lib/epuber/compiler/file_finders/imaginary.rb, line 78
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
make_dir_p(path) click to toggle source

@param [String | Array<String>] path path to folder to create

@return [DirEntry] dir entry for given path

# File lib/epuber/compiler/file_finders/imaginary.rb, line 64
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

Private Instance Methods

directories_under(dir) click to toggle source

@param [DirEntry] dir

@return [Array<DirEntry>]

# File lib/epuber/compiler/file_finders/imaginary.rb, line 152
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
find_recurser(dir, parts) click to toggle source
# File lib/epuber/compiler/file_finders/imaginary.rb, line 113
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.reject { |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
pattern_to_regex(pattern) click to toggle source
# File lib/epuber/compiler/file_finders/imaginary.rb, line 157
def pattern_to_regex(pattern)
  pattern.gsub('.', '\.')
    .gsub('?', '.')
    .gsub('*', '.*')
    .gsub('(', '\(')
    .gsub(')', '\)')
    .gsub(/\{(.*?)\}/) do
      "(#{Regexp.last_match[1].gsub(',', '|')})"
    end
    .gsub(/\A\./, '(?!\.).')
end