summaryrefslogtreecommitdiff
path: root/spec/ruby/core/time/fixtures/classes.rb
blob: a648171b324796cc359d154bd0a7872767e0d50b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
82
83
84
85
module TimeSpecs

  class SubTime < Time; end

  class MethodHolder
    class << self
      define_method(:now, &Time.method(:now))
      define_method(:new, &Time.method(:new))
    end
  end

  class Timezone
    def initialize(options)
      @offset = options[:offset]
    end

    def local_to_utc(t)
      t - @offset
    end

    def utc_to_local(t)
      t + @offset
    end
  end

  class TimezoneMethodCallRecorder < Timezone
    def initialize(options, &blk)
      super(options)
      @blk = blk
    end

    def local_to_utc(t)
      @blk.call(t)
      super
    end

    def utc_to_local(t)
      @blk.call(t)
      super
    end
  end

  class TimeLikeArgumentRecorder
    def self.result
      arguments = []

      zone = TimeSpecs::TimezoneMethodCallRecorder.new(offset: 0) do |obj|
        arguments << obj
      end

      # ensure timezone's methods are called at least once
      Time.new(2000, 1, 1, 12, 0, 0, zone)

      return arguments[0]
    end
  end

  class TimezoneWithAbbr < Timezone
    def initialize(options)
      super
      @abbr = options[:abbr]
    end

    def abbr(time)
      @abbr
    end
  end

  class TimezoneWithName < Timezone
    def initialize(options)
      super
      @name = options[:name]
    end

    def name
      @name
    end
  end

  class TimeWithFindTimezone < Time
    def self.find_timezone(name)
      TimezoneWithName.new(name: name.to_s, offset: 5*3600+30*60)
    end
  end
end