Class: Rpatch::Pattern

Inherits:
Object
  • Object
show all
Defined in:
lib/rpatch/hunk.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line) ⇒ Pattern

Returns a new instance of Pattern.



8
9
10
# File 'lib/rpatch/hunk.rb', line 8

def initialize(line)
  @text = line.chomp
end

Instance Attribute Details

#textObject (readonly)

Returns the value of attribute text.



6
7
8
# File 'lib/rpatch/hunk.rb', line 6

def text
  @text
end

Instance Method Details

#is_add?Boolean

Returns:

  • (Boolean)


24
25
26
27
28
# File 'lib/rpatch/hunk.rb', line 24

def is_add?
  @is_add ||= begin
    @text =~ /^\+/
  end
end

#is_in_after?Boolean

Returns:

  • (Boolean)


18
19
20
21
22
# File 'lib/rpatch/hunk.rb', line 18

def is_in_after?
  @is_in_after ||= begin
    @text =~ /^( |\+|\/ |\/\+)/
  end
end

#is_in_before?Boolean

Returns:

  • (Boolean)


12
13
14
15
16
# File 'lib/rpatch/hunk.rb', line 12

def is_in_before?
  @is_in_before ||= begin
    @text =~ /^( |-|\/ |\/-|\? |\?-|\?\/ |\?\/-)/
  end
end

#is_pattern?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/rpatch/hunk.rb', line 30

def is_pattern?
  @is_pattern ||= pattern.is_a?(Regexp)
end

#match(message) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rpatch/hunk.rb', line 50

def match(message)
  return nil unless message
  message = message.chomp
  match = nil
  while true
    if is_pattern?
      # When match with regexp, do not twick message
      if pattern.match(message)
        match = 1
      end
    else
      message = message.strip.gsub(/\s+/, ' ')
      if pattern == message
        match = 1
      end
    end

    if match
      break
    elsif message.empty?
      match = 0
      break
    # compare without leading "#"
    elsif message.start_with? "#"
      while message.start_with? "#"
        message = message[1..-1]
        message = message.strip unless is_pattern?
      end
    else
      break
    end
  end
  match
end

#patternObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rpatch/hunk.rb', line 34

def pattern
  @pattern ||= begin
    if text =~ /^\/( |-|\+)/
      Regexp.new(text[2..-1].strip)
    elsif text =~ /^\?\/( |-|\+)/
      Regexp.new(text[3..-1].strip)
    elsif text =~ /^( |-|\+)/
      pattern = text[1..-1].strip.gsub(/\s+/, ' ')
    elsif text =~ /^\?( |-|\+)/
      pattern = text[2..-1].strip.gsub(/\s+/, ' ')
    else
      raise PatchFormatError.new("-0---Unknown pattern in diffs: #{text.inspect}")
    end
  end
end