Module: LetItGo

Defined in:
lib/let_it_go.rb,
lib/let_it_go.rb,
lib/let_it_go/report.rb,
lib/let_it_go/version.rb,
lib/let_it_go/wtf_parser.rb,
lib/let_it_go/caller_line.rb,
lib/let_it_go/method_call.rb,
lib/let_it_go/middleware/olaf.rb

Defined Under Namespace

Modules: Middleware Classes: CallerLine, MethodCall, Report, WTFParser

Constant Summary collapse

VERSION =
"0.0.3"

Class Method Summary collapse

Class Method Details

.increment(key) ⇒ Object



98
99
100
# File 'lib/let_it_go.rb', line 98

def self.increment(key)
  store(key, 1)
end

.method_hash_for_klass(klass) ⇒ Object



18
19
20
# File 'lib/let_it_go.rb', line 18

def self.method_hash_for_klass(klass)
  @watching[klass]
end

.recordObject Also known as: cant_hold_it_back_anymore, do_you_want_to_build_a_snowman, turn_away_and_slam_the_door, the_cold_never_bothered_me_anyway, let_it_go

Main method, wrap code you want to check for frozen violations in a ‘let_it_go` block.

By default it will try to parse source of the method call to determine if a string literal or variable was used. We only care about string literals.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/let_it_go.rb', line 32

def self.record
  @mutex.synchronize do
    Thread.current[:let_it_go_recording]    = :on
    Thread.current[:let_it_go_records]      = {} # nil => never checked, 0 => checked, no string literals, positive => checked, positive literals detected
  end
  yield
  records = Thread.current[:let_it_go_records]
  report  = Report.new(records)
  return report
ensure
  @mutex.synchronize do
    Thread.current[:let_it_go_recording]    = nil
    Thread.current[:let_it_go_records]      = nil
  end
end

.record_exists?(key) ⇒ Boolean

Prevent looking

Returns:

  • (Boolean)


86
87
88
# File 'lib/let_it_go.rb', line 86

def self.record_exists?(key)
  Thread.current[:let_it_go_records][key]
end

.recording?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/let_it_go.rb', line 56

def self.recording?
  Thread.current[:let_it_go_recording] == :on
end

.store(key, increment = 0) ⇒ Object

Records when a method has been called without passing in a frozen object



91
92
93
94
95
96
# File 'lib/let_it_go.rb', line 91

def self.store(key, increment = 0)
  @mutex.synchronize do
    Thread.current[:let_it_go_records][key] ||= 0
    Thread.current[:let_it_go_records][key] += increment
  end
end

.watch_frozen(klass, method_name, positions:) ⇒ Object

Call to begin watching method for frozen violations



62
63
64
65
# File 'lib/let_it_go.rb', line 62

def self.watch_frozen(klass, method_name, positions:)
  @watching[klass] ||= {}
  @watching[klass][method_name] = positions
end

.watched_method_was_called(meth) ⇒ Object

If we are tracking it

If it has positive counter
  Increment Counter
If not
  do nothing

else we are not tracking it

If it has a frozen string literal
  Set counter to 1
If it does not
  Set counter to


77
78
79
80
81
82
# File 'lib/let_it_go.rb', line 77

def self.watched_method_was_called(meth)
  unless method = Thread.current[:let_it_go_records][meth.key]
    Thread.current[:let_it_go_records][meth.key] = method = meth
  end
  method.call_count += 1 if method.optimizable?
end

.watching_klassesObject



14
15
16
# File 'lib/let_it_go.rb', line 14

def self.watching_klasses
  @watching.keys
end

.watching_positions(klass, method) ⇒ Object



23
24
25
# File 'lib/let_it_go.rb', line 23

def self.watching_positions(klass, method)
  @watching[klass] && @watching[klass][method]
end