Skip to content

Instantly share code, notes, and snippets.

@goncalvesjoao
Created June 20, 2016 12:34
Show Gist options
  • Save goncalvesjoao/7d1bcb5c3737da0e80dceb48a53efa7f to your computer and use it in GitHub Desktop.
Save goncalvesjoao/7d1bcb5c3737da0e80dceb48a53efa7f to your computer and use it in GitHub Desktop.
date time abstraction that enables tests to manipulate the current date
module DT
extend self
DEFAULT_TIME_ZONE = 'Lisbon'
def now
(mock_data(:now) || DateTime.now).utc
end
def datetime_for_query(date, time_zone = DEFAULT_TIME_ZONE)
I18n.l(date.in_time_zone(time_zone), format: '%Y-%m-%d %H:%M:%S.%6N')
end
def set_mock(key, data)
mocks[key] = data
end
protected ######################## PROTECTED ###########################
def mocks
@mocks ||= {}
end
def mock_data(key)
return nil if ENV['ENV'] != 'test'
mocks[key]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment