Skip to content

Commit

Permalink
Avoid Kernel#send()
Browse files Browse the repository at this point in the history
Previously all Timecop instance methods were private and we would call
them from the class methods using send(). This commit achieves a similar
effect (making the instance methods inaccessible to users) by making
`instance` private.

Using standard method calls instead of send should be slightly faster as
it reduces method lookups.
  • Loading branch information
jhawthorn authored and joshuacronemeyer committed Feb 28, 2022
1 parent 138bc93 commit 8d82f67
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions lib/timecop/timecop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class Timecop
include Singleton

class << self
private :instance

# Allows you to run a block of code and "fake" a time throughout the execution of that block.
# This is particularly useful for writing test methods where the passage of time is critical to the business
# logic being tested. For example:
Expand Down Expand Up @@ -74,33 +76,33 @@ def scale(*args, &block)
end

def baseline
instance.send(:baseline)
instance.baseline
end

def baseline=(baseline)
instance.send(:baseline=, baseline)
instance.baseline = baseline
end

# Reverts back to system's Time.now, Date.today and DateTime.now (if it exists) permamently when
# no block argument is given, or temporarily reverts back to the system's time temporarily for
# the given block.
def return(&block)
if block_given?
instance.send(:return, &block)
instance.return(&block)
else
instance.send(:unmock!)
instance.unmock!
nil
end
end
alias :unfreeze :return

def return_to_baseline
instance.send(:return_to_baseline)
instance.return_to_baseline
Time.now
end

def top_stack_item #:nodoc:
instance.send(:stack).last
instance.stack.last
end

def safe_mode=(safe)
Expand All @@ -112,27 +114,25 @@ def safe_mode?
end

def thread_safe=(t)
instance.send(:thread_safe=, t)
instance.thread_safe = t
end

def thread_safe
instance.send(:thread_safe)
instance.thread_safe
end

# Returns whether or not Timecop is currently frozen/travelled
def frozen?
!instance.send(:stack).empty?
!instance.stack.empty?
end

private
def send_travel(mock_type, *args, &block)
val = instance.send(:travel, mock_type, *args, &block)
val = instance.travel(mock_type, *args, &block)
block_given? ? val : Time.now
end
end

private

def baseline=(b)
set_baseline(b)
stack << TimeStackItem.new(:travel, b)
Expand Down

0 comments on commit 8d82f67

Please sign in to comment.