class TermUtils::FF::Query
Represents a file system query.
Public Class Methods
accept_entry_name?(ctx, name)
click to toggle source
Tests whether a given entry should be accepted. @param ctx [Context] @param name [String] @return [Boolean]
# File lib/term_utils/ff/query.rb, line 135 def self.accept_entry_name?(ctx, name) ctx.config.ignore_list.each do |i| if i.match? name return false end end true end
new()
click to toggle source
Constructs a new Query
.
# File lib/term_utils/ff/query.rb, line 26 def initialize @config = TermUtils::FF::Config.new end
search(ctx, parent_entry)
click to toggle source
Searches a directory. @param ctx [Context] @param parent_entry [entry] @return [nil]
# File lib/term_utils/ff/query.rb, line 99 def self.search(ctx, parent_entry) entries = Dir.entries(parent_entry.path) unless ctx.config.sorting_mode.nil? if ctx.config.sorting_compare.nil? entries.sort! else entries.sort!(&ctx.config.sorting_compare) end if ctx.config.sorting_mode == :reverse entries.reverse! end end entries.each do |name| next if %w[. ..].include? name next unless accept_entry_name?(ctx, name) new_entry = TermUtils::FF::Entry.new new_entry.name = name new_entry.relative_path_comps = parent_entry.relative_path_comps.dup.push(name) new_entry.path = "#{ctx.base_path}/#{new_entry.relative_path_comps.join('/')}" if ctx.config.min_depth <= new_entry.depth new_entry.index = ctx.index_seq ctx.index_seq += 1 ctx.block.call(new_entry) if ctx.block ctx.result << new_entry end if File.directory?(new_entry.path) && (ctx.config.max_depth.nil? || ctx.config.max_depth > new_entry.depth) TermUtils::FF::Query.search(ctx, new_entry) end end end
Public Instance Methods
exec(path, &block)
click to toggle source
Executes this one. @param path [String] @return [Array<Entry>]
# File lib/term_utils/ff/query.rb, line 71 def exec(path, &block) ctx = Context.new ctx.config = @config.dup ctx.config.min_depth = 0 if ctx.config.min_depth.nil? ctx.base_path = path ctx.block = block ctx.index_seq = 0 ctx.result = [] first_entry = TermUtils::FF::Entry.new first_entry.name = path first_entry.relative_path_comps = [] first_entry.path = path if ctx.config.min_depth == 0 first_entry.index = ctx.index_seq ctx.index_seq += 1 ctx.block.call(first_entry) if ctx.block ctx.result << first_entry end if File.directory?(first_entry.path) && (ctx.config.max_depth.nil? || ctx.config.max_depth > 0) TermUtils::FF::Query.search(ctx, first_entry) end ctx.result end
ignore(regexp)
click to toggle source
Adds a Regexp to ignore. @param regexp [Regexp] @return [Query]
# File lib/term_utils/ff/query.rb, line 38 def ignore(regexp) @config.ignore_list << regexp self end
initialize_copy(other)
click to toggle source
Calls superclass method
# File lib/term_utils/ff/query.rb, line 30 def initialize_copy(other) @config = other.config.dup super end
max_depth(depth)
click to toggle source
Sets a maximum depth. @param depth [Integer] @return [Query]
# File lib/term_utils/ff/query.rb, line 54 def max_depth(depth) @config.max_depth = depth self end
min_depth(depth)
click to toggle source
Sets a minimum depth. @param depth [Integer] @return [Query]
# File lib/term_utils/ff/query.rb, line 46 def min_depth(depth) @config.min_depth = depth self end
sort(mode = :forward, &block)
click to toggle source
Sets the sorting mode. @param mode [Symbol] Either `:forward`, `:reverse` or `nil` (default). @return [Query]
# File lib/term_utils/ff/query.rb, line 62 def sort(mode = :forward, &block) @config.sorting_mode = mode @config.sorting_compare = block self end