Module: TypoFixer::TypoTracker

Defined in:
lib/typofixer/typotracker.rb

Overview

typoを検出するデバッガ。Kernelにincludeすればプログラム全体を監視する

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(funcname, *args, &blk) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/typofixer/typotracker.rb', line 112

def method_missing funcname, *args, &blk
  # システムで予約されたものはキャッチしない
  if [:to_ary, :to_path, :to_str, :to_hash, :to_int, :to_io, :to_regexp, :to_proc].include? funcname
    super
  end

  begin
    super
  rescue NameError => e # レシーバが存在しない場合
    typo_without_receiver e, funcname, *args, &blk
  rescue NoMethodError => e # レシーバが存在する場合
    typo_with_receiver e, funcname, *args, &blk
  end
end

Instance Method Details

#typo(e, methods, funcname, *args, &blk) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/typofixer/typotracker.rb', line 33

def typo e, methods, funcname, *args, &blk
  typo_cases.each do |_case|
    begin
      return _case.call e, methods, funcname, *args, &blk
    rescue Exception
    end
  end

  raise e
end

#typo_casesObject



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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/typofixer/typotracker.rb', line 52

def typo_cases
  return @typo_cases if @typo_cases
  (@typo_cases = []).tap do |typo_cases|
    @typo_cases << proc do |e, methods, funcname, *args, &blk|
      most = []
      seq = funcname.to_s.split //
      seq.length.times do |i|
        t = seq.clone
        t[i] = '.'
        that = methods.grep /^#{t.join}$/
        most += that.reject { |item| most.include? item }
      end

      if most.one?
        typo_hook e, most.first, funcname, *args, &blk
      else
        raise e
      end
    end
      

    @typo_cases << proc do |e, methods, funcname, *args, &blk|
      most = []
      seq = funcname.to_s.split //
      seq.length.times do |i|
        t = seq.clone
        t[i] = '.'
        t[i+1] = '.'
        that = methods.grep /^#{t.join}$/
        most += that.reject { |item| most.include? item }
      end

      if most.one?
        typo_hook e, most.first, funcname, *args, &blk
      else
        raise e
      end
    end

    @typo_cases << proc do |e, methods, funcname, *args, &blk|
      most = []
      seq = funcname.to_s.split //
      seq.length.times do |i|
        t = seq.clone
        t.delete_at i
        t.delete_at i
        t[i] = '.{0,4}'
        that = methods.grep /^#{t.join}$/
        most += that.reject { |item| most.include? item }
      end

      if most.one?
        typo_hook e, most.first, funcname, *args, &blk
      else
        raise e
      end
    end
  end
end

#typo_hook(e, ifn, fn, *as, &b) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/typofixer/typotracker.rb', line 23

def typo_hook e, ifn, fn, *as, &b
  if typoout
    typoout.write "もしかして: #{ifn}\n"
    $@.each do |at|
      typoout.write "\t#{at}\n"
    end
  end
  send ifn, *as, &b
end

#typo_with_receiver(e, fn, *as, &b) ⇒ Object



48
49
50
# File 'lib/typofixer/typotracker.rb', line 48

def typo_with_receiver e, fn, *as, &b
  typo(e, methods, fn, *as, &b)
end

#typo_without_receiver(e, fn, *as, &b) ⇒ Object



44
45
46
# File 'lib/typofixer/typotracker.rb', line 44

def typo_without_receiver e, fn, *as, &b
  typo(e, methods + private_methods, fn, *as, &b)
end

#typoinObject



7
8
9
# File 'lib/typofixer/typotracker.rb', line 7

def typoin
  $stdin
end

#typooutObject



11
12
13
# File 'lib/typofixer/typotracker.rb', line 11

def typoout
  $stdout
end