Class: SportDb::Patcher

Inherits:
Object
  • Object
show all
Includes:
LogUtils::Logging
Defined in:
lib/sportdb/pretty_printer.rb

Instance Method Summary collapse

Constructor Details

#initialize(root, opts = {}) ⇒ Patcher

Returns a new instance of Patcher.



15
16
17
18
19
20
21
22
# File 'lib/sportdb/pretty_printer.rb', line 15

def initialize( root, opts={} )
  @root  = root
  @path  = opts[:path] || opts[:paths]    # pass in regex as string
  @names = opts[:name] || opts[:names]    # pass in regex as string

  @path_regex  = Regexp.new( @path )  if @path
  @names_regex = Regexp.new( @names ) if @names
end

Instance Method Details

#find_filesObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/sportdb/pretty_printer.rb', line 25

def find_files
  ## note: for now always filter by .txt extension
  files = Dir[ "#{@root}/**/*.txt" ]

  puts "before filter:"
  pp files

  files = files.select do |f|

    basename = File.basename( f )
    dirname  = File.dirname( f )   # note: add trailing slash
    dirname = "#{dirname}/"

    puts "  basename=>#{basename}<, dirname=>#{dirname}<"

    if @path_regex
      match_dirname =  @path_regex === dirname    # note: use === (triple) for true/false regex match
    else
      match_dirname = true
    end
    
    if @names_regex
      match_basename = @names_regex === basename
    else
      match_basename = true
    end

    match_basename && match_dirname
  end

  puts "after filter:"
  pp files

  files
end

#patch(save = false) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/sportdb/pretty_printer.rb', line 62

def patch( save=false)
  files = find_files
  change_logs = []

  files.each do |file|
    p = PrettyPrinter.from_file( file )
    new_text, change_log = p.patch
    
    next if change_log.empty?   ## no changes
    
    if save
      File.open( file, 'w' ) do |f|
        f.write new_text
      end
    end

    change_logs << [file, change_log]
  end

  change_logs  ## return change_logs or empty array
end