Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for 'in:' kwarg in Time.new and Time.now #432

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions lib/timecop/time_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,34 @@

class Time #:nodoc:
class << self
def mock_time
def mock_time(*args)
mocked_time_stack_item = Timecop.top_stack_item
mocked_time_stack_item.nil? ? nil : mocked_time_stack_item.time(self)
return nil if mocked_time_stack_item.nil?
time = mocked_time_stack_item.time(self)
if args.size == 1 && args[0].is_a?(Hash) && !args[0][:in].nil?
time = time.localtime(args[0][:in])
end
time
end

alias_method :now_without_mock_time, :now

def now_with_mock_time
mock_time || now_without_mock_time
def now_with_mock_time(*args)
mock_time(*args) || now_without_mock_time(*args)
end

alias_method :now, :now_with_mock_time

ruby2_keywords :now_with_mock_time if Module.private_method_defined?(:ruby2_keywords)

alias_method :new_without_mock_time, :new

def new_with_mock_time(*args)
args.size <= 0 ? now : new_without_mock_time(*args)
if args.size == 0 || args.size == 1 && args[0].is_a?(Hash) && !args[0].compact.empty?
now(*args)
else
new_without_mock_time(*args)
end
end

ruby2_keywords :new_with_mock_time if Module.private_method_defined?(:ruby2_keywords)
Expand Down