Skip to content

Commit

Permalink
Eliminate warnings when ruby is in verbose/warning mode.
Browse files Browse the repository at this point in the history
Eliminates these warnings:

    “…/lib/timecop/timecop.rb:110: warning: instance variable @safe_mode not initialized”
    “…lib/timecop/time_stack_item.rb:45: warning: instance variable @travel_offset not initialized”
    “…lib/timecop/time_stack_item.rb:49: warning: instance variable @scaling_factor not initialized”
    “lib/timecop/time_stack_item.rb:140: warning: mismatched indentations at 'end' with 'class' at 1”
    “…lib/timecop/time_extensions.rb:74: warning: method redefined; discarding old now.”

For the method redefinition warning, could have used `undef :now’, but seems cleaner instead to alias :now_without_mock_time to be the old :now, rather than coding up an equivalent. (Same pattern as used in Time in the same file)
  • Loading branch information
ronen committed Jan 9, 2015
1 parent 5fe7dd8 commit 65ea0cc
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 6 deletions.
6 changes: 2 additions & 4 deletions lib/timecop/time_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,12 @@ def mock_time
mocked_time_stack_item.nil? ? nil : mocked_time_stack_item.datetime(self)
end

def now_without_mock_time
Time.now_without_mock_time.to_datetime
end

def now_with_mock_time
mock_time || now_without_mock_time
end

alias_method :now_without_mock_time, :now

alias_method :now, :now_with_mock_time
end
end
3 changes: 2 additions & 1 deletion lib/timecop/time_stack_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class TimeStackItem #:nodoc:

def initialize(mock_type, *args)
raise "Unknown mock_type #{mock_type}" unless [:freeze, :travel, :scale].include?(mock_type)
@travel_offset = @scaling_factor = nil
@scaling_factor = args.shift if mock_type == :scale
@mock_type = mock_type
@time = parse_time(*args)
Expand Down Expand Up @@ -137,4 +138,4 @@ def time_klass
Time.respond_to?(:zone) && Time.zone ? Time.zone : Time
end
end
end
end
2 changes: 1 addition & 1 deletion lib/timecop/timecop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def safe_mode=(safe)
end

def safe_mode?
false || @safe_mode
@safe_mode ||= false
end

private
Expand Down
2 changes: 2 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
require 'bundler/setup'
require 'minitest/autorun'

$VERBOSE = true # enable ruby warnings

begin
require 'mocha/setup'
rescue LoadError
Expand Down

0 comments on commit 65ea0cc

Please sign in to comment.