Module: Rush::Commands
- Included in:
- Array, Dir, File, SearchResults
- Defined in:
- lib/rush/commands.rb
Overview
The commands module contains operations against Rush::File entries, and is mixed in to Rush::Entry and Array. This means you can run these commands against a single file, a dir full of files, or an arbitrary list of files.
Examples:
box['/etc/hosts'].search /localhost/ # single file
box['/etc/'].search /localhost/ # entire directory
box['/etc/**/*.conf'].search /localhost/ # arbitrary list
Class Method Summary collapse
Instance Method Summary collapse
-
#entries ⇒ Object
The entries command must return an array of Rush::Entry items.
-
#line_count ⇒ Object
Count the number of lines in the contained files.
-
#replace_contents!(pattern, with_text) ⇒ Object
Search and replace file contents.
-
#search(pattern) ⇒ Object
Search file contents for a regular expression.
Class Method Details
.included(base) ⇒ Object
11 12 13 14 |
# File 'lib/rush/commands.rb', line 11 def self.included(base) base.extend(Rush::ExternalCommands) Rush::ExternalCommands::COMMANDS_TO_ADD.each { |command| base.add_command(command) } end |
Instance Method Details
#entries ⇒ Object
The entries command must return an array of Rush::Entry items. This varies by class that it is mixed in to.
18 19 20 |
# File 'lib/rush/commands.rb', line 18 def entries raise "must define me in class mixed in to for command use" end |
#line_count ⇒ Object
Count the number of lines in the contained files.
42 43 44 45 46 47 |
# File 'lib/rush/commands.rb', line 42 def line_count entries.inject(0) do |count, entry| count += entry.lines.size if !entry.dir? count end end |
#replace_contents!(pattern, with_text) ⇒ Object
Search and replace file contents.
35 36 37 38 39 |
# File 'lib/rush/commands.rb', line 35 def replace_contents!(pattern, with_text) entries.each do |entry| entry.replace_contents!(pattern, with_text) unless entry.dir? end end |
#search(pattern) ⇒ Object
Search file contents for a regular expression. A Rush::SearchResults object is returned.
24 25 26 27 28 29 30 31 32 |
# File 'lib/rush/commands.rb', line 24 def search(pattern) results = Rush::SearchResults.new(pattern) entries.each do |entry| if !entry.dir? and matches = entry.search(pattern) results.add(entry, matches) end end results end |