Module: CommonMob::PatchHelper

Defined in:
lib/common_mob/patch.rb

Instance Method Summary collapse

Instance Method Details

#patch_file(to_patch) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/common_mob/patch.rb', line 36

def patch_file(to_patch)
  log "what the very hell?"
  if args.src?
    src = args.src.pathname.read
  elsif args.string?
    src = args.string
  end

  patch_string(to_patch.read, src, {
    :comment => args.comment,
    :key => args.key,
  })
end

#patch_marker_re(comment, key, switch) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/common_mob/patch.rb', line 3

def patch_marker_re(comment,key,switch)
  %[
    #{Regexp.escape(comment.to_s)}
    \\s+
    angry-mob
    \\s+
    #{Regexp.escape(key.to_s)}
    \\s+
    #{Regexp.escape(switch.to_s)}
    $
  ]
end

#patch_string(to_patch, src, options = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/common_mob/patch.rb', line 16

def patch_string(to_patch, src, options={})
  comment = options[:comment] || '#'
  key     = options[:key]

  content = "#{comment} angry-mob #{key} start\n#{src}" \
    "\n#{comment} angry-mob #{key} end"

  pattern = %r[
    #{patch_marker_re(comment,key,'start')}
    .*
    #{patch_marker_re(comment,key,'end')}
  ]mx

  if to_patch[pattern]
    to_patch.gsub(pattern,content)
  else
    to_patch + "\n" + content
  end
end