Class: GrepLogic

Inherits:
BaseLogic show all
Defined in:
lib/cureutils/logic/grep_logic.rb

Overview

Singleton class for cure grep Managing the method for matching and printing.

Defined Under Namespace

Modules: ColorMode

Instance Method Summary collapse

Methods inherited from BaseLogic

#source_err, #source_input, #source_output

Constructor Details

#initializeGrepLogic

Returns a new instance of GrepLogic.



14
15
16
17
# File 'lib/cureutils/logic/grep_logic.rb', line 14

def initialize
  super
  @match_method = 'match'
end

Instance Method Details

#option_colorize(colorize) ⇒ Object



24
25
26
# File 'lib/cureutils/logic/grep_logic.rb', line 24

def option_colorize(colorize)
  @str_color = (colorize ? ColorMode::RED : ColorMode::NONE)
end

#option_only(only_flag) ⇒ Object



19
20
21
22
# File 'lib/cureutils/logic/grep_logic.rb', line 19

def option_only(only_flag)
  @only_matched = only_flag
  @match_method = (only_flag ? 'scan' : 'match')
end

#pattern(pat, is_exregex = false) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/cureutils/logic/grep_logic.rb', line 28

def pattern(pat, is_exregex = false)
  if is_exregex
    @pattern = /#{pat}/
    return
  end
  @pattern = /#{Common.pregex2regex(pat)}/
end


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/cureutils/logic/grep_logic.rb', line 36

def print_results
  exit_status = 1
  @in.each do |line|
    matched_strs = line.send(@match_method, @pattern)
    next unless matched_strs

    exit_status = 0
    if @only_matched
      matched_strs.each { |str| @out.puts str.send(@str_color) }
    else
      @out.puts line.gsub(@pattern, '\0'.send(@str_color))
    end
  end
  exit_status
end