Class: Code::Object::Time

Inherits:
Code::Object show all
Defined in:
lib/code/object/time.rb

Constant Summary collapse

DEFAULT_ZONE =
"Etc/UTC"

Constants inherited from Code::Object

NUMBER_CLASSES

Instance Attribute Summary

Attributes inherited from Code::Object

#raw

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Code::Object

#<=>, #==, #as_json, as_json, #code_and_operator, code_and_operator, code_as_json, #code_as_json, code_different, #code_different, code_equal_equal, #code_equal_equal, code_equal_equal_equal, #code_equal_equal_equal, code_exclamation_point, #code_exclamation_point, code_exclusive_range, #code_exclusive_range, code_inclusive_range, #code_inclusive_range, code_new, code_or_operator, #code_or_operator, code_self, #code_self, code_to_json, #code_to_json, falsy?, #falsy?, #hash, inspect, maybe, multi_fetch, #multi_fetch, #nothing?, nothing?, repeat, sig, #sig, #succ, #to_code, to_json, #to_json, to_s, truthy?, #truthy?, |

Constructor Details

#initialize(*args, **_kargs) ⇒ Time

Returns a new instance of Time.



8
9
10
11
# File 'lib/code/object/time.rb', line 8

def initialize(*args, **_kargs, &)
  ::Time.zone ||= DEFAULT_ZONE
  @raw = ::Time.zone.parse(args.first.to_s) || ::Time.zone.now
end

Class Method Details

.call(**args) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/code/object/time.rb', line 13

def self.call(**args)
  code_operator = args.fetch(:operator, nil).to_code

  case code_operator.to_s
  when "now"
    sig(args)
    code_now
  when "tomorrow"
    sig(args)
    code_tomorrow
  when "yesterday"
    sig(args)
    code_yesterday
  when "hour"
    sig(args)
    code_hour
  else
    super
  end
end

.code_hourObject



34
35
36
# File 'lib/code/object/time.rb', line 34

def self.code_hour
  code_now.code_hour
end

.code_nowObject



48
49
50
51
# File 'lib/code/object/time.rb', line 48

def self.code_now
  ::Time.zone ||= DEFAULT_ZONE
  new(::Time.zone.now)
end

.code_tomorrowObject



38
39
40
41
# File 'lib/code/object/time.rb', line 38

def self.code_tomorrow
  ::Time.zone ||= DEFAULT_ZONE
  new(::Time.zone.tomorrow)
end

.code_yesterdayObject



43
44
45
46
# File 'lib/code/object/time.rb', line 43

def self.code_yesterday
  ::Time.zone ||= DEFAULT_ZONE
  new(::Time.zone.yesterday)
end

Instance Method Details

#call(**args) ⇒ Object



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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/code/object/time.rb', line 53

def call(**args)
  code_operator = args.fetch(:operator, nil).to_code
  code_arguments = args.fetch(:arguments, []).to_code
  code_value = code_arguments.code_first

  case code_operator.to_s
  when "after?"
    sig(args) { Time.maybe }
    code_after?(code_value)
  when "before?"
    sig(args) { Time.maybe }
    code_before?(code_value)
  when "past?"
    sig(args)
    code_past?
  when "future?"
    sig(args)
    code_future?
  when "year"
    sig(args)
    code_year
  when "years"
    sig(args)
    code_years
  when "month"
    sig(args)
    code_month
  when "months"
    sig(args)
    code_months
  when "day"
    sig(args)
    code_day
  when "days"
    sig(args)
    code_days
  when "hour"
    sig(args)
    code_hour
  when "hours"
    sig(args)
    code_hours
  when "minute"
    sig(args)
    code_minute
  when "minutes"
    sig(args)
    code_minutes
  when "second"
    sig(args)
    code_second
  when "seconds"
    sig(args)
    code_seconds
  when "format"
    sig(args) { String }
    code_format(code_value)
  else
    super
  end
end

#code_after?(other = nil) ⇒ Boolean

Returns:



115
116
117
118
119
120
# File 'lib/code/object/time.rb', line 115

def code_after?(other = nil)
  code_other = other.to_code
  code_other = Time.code_now if code_other.nothing?

  Boolean.new(raw.after?(code_other.raw))
end

#code_before?(other = nil) ⇒ Boolean

Returns:



122
123
124
125
126
127
# File 'lib/code/object/time.rb', line 122

def code_before?(other = nil)
  code_other = other.to_code
  code_other = Time.code_now if code_other.nothing?

  Boolean.new(raw.before?(code_other.raw))
end

#code_dayObject



153
154
155
# File 'lib/code/object/time.rb', line 153

def code_day
  Integer.new(raw.day)
end

#code_daysObject



157
158
159
# File 'lib/code/object/time.rb', line 157

def code_days
  Integer.new(raw.day)
end

#code_format(format) ⇒ Object



185
186
187
188
189
# File 'lib/code/object/time.rb', line 185

def code_format(format)
  code_format = format.to_code

  String.new(raw.strftime(code_format.raw))
end

#code_future?Boolean

Returns:



133
134
135
# File 'lib/code/object/time.rb', line 133

def code_future?
  code_after?
end

#code_hourObject



161
162
163
# File 'lib/code/object/time.rb', line 161

def code_hour
  Integer.new(raw.hour)
end

#code_hoursObject



165
166
167
# File 'lib/code/object/time.rb', line 165

def code_hours
  Integer.new(raw.hour)
end

#code_minuteObject



169
170
171
# File 'lib/code/object/time.rb', line 169

def code_minute
  Integer.new(raw.min)
end

#code_minutesObject



173
174
175
# File 'lib/code/object/time.rb', line 173

def code_minutes
  Integer.new(raw.min)
end

#code_monthObject



145
146
147
# File 'lib/code/object/time.rb', line 145

def code_month
  Integer.new(raw.month)
end

#code_monthsObject



149
150
151
# File 'lib/code/object/time.rb', line 149

def code_months
  Integer.new(raw.month)
end

#code_past?Boolean

Returns:



129
130
131
# File 'lib/code/object/time.rb', line 129

def code_past?
  code_before?
end

#code_secondObject



177
178
179
# File 'lib/code/object/time.rb', line 177

def code_second
  Integer.new(raw.sec)
end

#code_secondsObject



181
182
183
# File 'lib/code/object/time.rb', line 181

def code_seconds
  Integer.new(raw.sec)
end

#code_yearObject



137
138
139
# File 'lib/code/object/time.rb', line 137

def code_year
  Integer.new(raw.year)
end

#code_yearsObject



141
142
143
# File 'lib/code/object/time.rb', line 141

def code_years
  Integer.new(raw.year)
end