Class: TermUtils::FF::Query
- Inherits:
-
Object
- Object
- TermUtils::FF::Query
- Defined in:
- lib/term_utils/ff/query.rb
Overview
Represents a file system query.
Class Method Summary collapse
-
.accept_entry_name?(ctx, name) ⇒ Boolean
Tests whether a given entry should be accepted.
-
.search(ctx, parent_entry) ⇒ nil
Searches a directory.
Instance Method Summary collapse
-
#exec(path, &block) ⇒ Array<Entry>
Executes this one.
-
#ignore(regexp) ⇒ Query
Adds a Regexp to ignore.
-
#initialize ⇒ Query
constructor
Constructs a new Query.
- #initialize_copy(other) ⇒ Object
-
#max_depth(depth) ⇒ Query
Sets a maximum depth.
-
#min_depth(depth) ⇒ Query
Sets a minimum depth.
-
#sort(mode = :forward, &block) ⇒ Query
Sets the sorting mode.
Constructor Details
Class Method Details
.accept_entry_name?(ctx, name) ⇒ Boolean
Tests whether a given entry should be accepted.
135 136 137 138 139 140 141 142 |
# 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 |
.search(ctx, parent_entry) ⇒ nil
Searches a directory.
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# 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 |
Instance Method Details
#exec(path, &block) ⇒ Array<Entry>
Executes this one.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# 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) ⇒ Query
Adds a Regexp to ignore.
38 39 40 41 |
# File 'lib/term_utils/ff/query.rb', line 38 def ignore(regexp) @config.ignore_list << regexp self end |
#initialize_copy(other) ⇒ Object
30 31 32 33 |
# File 'lib/term_utils/ff/query.rb', line 30 def initialize_copy(other) @config = other.config.dup super end |
#max_depth(depth) ⇒ Query
Sets a maximum depth.
54 55 56 57 |
# File 'lib/term_utils/ff/query.rb', line 54 def max_depth(depth) @config.max_depth = depth self end |
#min_depth(depth) ⇒ Query
Sets a minimum depth.
46 47 48 49 |
# File 'lib/term_utils/ff/query.rb', line 46 def min_depth(depth) @config.min_depth = depth self end |
#sort(mode = :forward, &block) ⇒ Query
Sets the sorting mode.
62 63 64 65 66 |
# File 'lib/term_utils/ff/query.rb', line 62 def sort(mode = :forward, &block) @config.sorting_mode = mode @config.sorting_compare = block self end |