Sha256: 562a6c6bf6037895df560a53a6d7d3e044f6fc2a714694dbf4a09fb588116c38

Contents?: true

Size: 1011 Bytes

Versions: 23

Compression:

Stored size: 1011 Bytes

Contents

require 'time'

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

        remove_method :now rescue nil
        remove_method :new rescue nil

        attr_writer :dummy

        def dummy=(value)
          if value.respond_to?(:to_str)
            value = Time.parse(value.to_str)
          end
          @dummy = value
        end

        def dummy(value = nil)
          if value.nil?
            @dummy
          else
            begin
              old_dummy = @dummy
              self.dummy = value
              yield
            ensure
              self.dummy = old_dummy
            end
          end
        end

        def new(*a)
          if dummy
            dummy.dup
          elsif caller.first =~ /now/
            really_now
          else
            really_new(*a)
          end
        end

        def now
          new
        end
      end
      super
    end
  end
end

require 'tins/alias'

Version data entries

23 entries across 22 versions & 3 rubygems

Version Path
tdiary-5.1.2 vendor/bundle/ruby/2.6.0/gems/tins-0.13.2/lib/tins/time_dummy.rb
tdiary-5.1.1 vendor/bundle/ruby/2.6.0/gems/tins-0.13.2/lib/tins/time_dummy.rb
tdiary-5.0.8 vendor/bundle/gems/tins-0.13.2/lib/tins/time_dummy.rb
tdiary-5.0.8 vendor/bundle/ruby/2.5.0/gems/tins-0.13.2/lib/tins/time_dummy.rb
tins-1.0.1 lib/tins/time_dummy.rb
tins-1.0.0 lib/tins/time_dummy.rb
tins-0.13.2 lib/tins/time_dummy.rb
social_url_stats-0.0.1 vendor/ruby/1.9.1/gems/tins-0.13.1/lib/tins/time_dummy.rb
tins-0.13.1 lib/tins/time_dummy.rb
tins-0.13.0 lib/tins/time_dummy.rb
tins-0.12.0 lib/tins/time_dummy.rb
tins-0.11.0 lib/tins/time_dummy.rb
tins-0.10.0 lib/tins/time_dummy.rb
tins-0.9.0 lib/tins/time_dummy.rb
tins-0.8.4 lib/tins/time_dummy.rb
tins-0.8.3 lib/tins/time_dummy.rb
tins-0.8.2 lib/tins/time_dummy.rb
tins-0.8.0 lib/tins/time_dummy.rb
tins-0.7.4 lib/tins/time_dummy.rb
tins-0.7.3 lib/tins/time_dummy.rb