Module: Tins::TimeDummy

Defined in:
lib/tins/time_dummy.rb

Overview

A module that provides time dummy functionality for testing and development purposes.

This module allows setting a fake current time that can be used in tests or development environments where you want to control the time returned by Time.now.

Class Method Summary collapse

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 time freezing functionality by extending the including class/module with special time handling methods. The method modifies the including class/module’s singleton class to provide dummy time capabilities.



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
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/tins/time_dummy.rb', line 21

def self.included(modul)
  class << modul
    alias really_new new
    alias really_now now

    remove_method :now rescue nil
    remove_method :new rescue nil

    # Sets the dummy time value for time freezing functionality.
    #
    # This method allows setting a specific time value that will be used
    # as the frozen time when time freezing is enabled.
    #
    # @param value [Time, String] the time value to set as dummy
    def dummy=(value)
      if value.respond_to?(:to_str)
        value = Time.parse(value.to_str)
      elsif value.respond_to?(:to_time)
        value = value.to_time
      end
      @dummy = value
    end

    # The dummy method manages a dummy value for testing purposes.
    #
    # @param value [Object] the dummy value to set, or nil to get the current value
    # @return [Object] the current dummy value when value is nil
    # @yield [] executes the block with the dummy value set
    # @return [Object] the return value of the block if a block is given
    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 new method creates a new time instance, either by duplicating
    # the dummy time or calling the real creation method.
    #
    # @param a [ Array ] the arguments to pass to the real creation
    # method
    # @param kw [ Hash ] the keyword arguments to pass to the real
    # creation method
    #
    # @return [ Time ] the newly created time instance
    def new(*a, **kw)
      if dummy
        dummy.dup
      elsif caller.first =~ /`now`/
        really_now(**kw)
      else
        really_new(*a, **kw)
      end
    end

    # The now method returns a new instance of the current class.
    #
    # @return [Object] a new instance of the class this method is called on
    def now
      new
    end
  end
  super
end