Module: Tins::DateTimeDummy
- Defined in:
- lib/tins/date_time_dummy.rb
Overview
A module that provides dummy functionality for DateTime class
This module allows setting a fixed date and time that will be returned by DateTime.now instead of the actual current time. This is useful for testing purposes where consistent timestamps are required.
Class Method Summary collapse
-
.included(modul) ⇒ Object
The included method is a hook that gets called when this module is included in another class or module.
Class Method Details
.included(modul) ⇒ Object
The included method is a hook that gets called when this module is included in another class or module.
It sets up date time freezing functionality by extending the including class/module with special date time handling methods. The method modifies the including class/module’s singleton class to provide dummy date time capabilities.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/tins/date_time_dummy.rb', line 19 def self.included(modul) class << modul alias really_now now remove_method :now rescue nil # Sets the dummy value for datetime handling. # # @param value [DateTime, String] the datetime value to set as dummy def dummy=(value) if value.respond_to?(:to_str) value = DateTime.parse(value.to_str) elsif value.respond_to?(:to_datetime) value = value.to_datetime end @dummy = value end # The dummy method provides a way to set and restore a dummy value # within a block. # # @param value [Object] the dummy value to set, or nil to get the # current dummy value # # @yield [void] yields control to the block if a value is provided # # @return [Object] the current dummy value if no value parameter is # provided # @return [Object] the result of the block if a value parameter is # provided def dummy(value = nil) if value.nil? if defined?(@dummy) @dummy end else begin old_dummy = @dummy self.dummy = value yield ensure self.dummy = old_dummy end end end # The now method returns the current time, using a dummy time if one # has been set. If no dummy time is set, it delegates to the actual # time retrieval method. # # @return [Time] the current time or a mocked time if dummy is set def now if dummy dummy.dup elsif caller.first =~ /`now`/ really_now else really_now end end end super end |