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 included from Concerns::Shared

#raw

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Code::Object

#code_new, code_new, maybe, name, #name, repeat, |

Methods included from Concerns::Shared

#<=>, #==, #as_json, #code_and, #code_as_json, #code_deep_duplicate, #code_different, #code_duplicate, #code_equal_equal, #code_equal_equal_equal, #code_exclamation_point, #code_exclusive_range, #code_falsy?, #code_fetch, code_fetch, code_get, #code_get, #code_inclusive_range, #code_inspect, #code_name, #code_or, #code_self, #code_set, code_set, #code_to_boolean, #code_to_class, #code_to_date, #code_to_decimal, #code_to_dictionary, #code_to_duration, #code_to_integer, #code_to_json, #code_to_list, #code_to_nothing, #code_to_parameter, #code_to_range, #code_to_string, #code_to_time, #code_truthy?, #eql?, #falsy?, #hash, #inspect, #multi_fetch, #nothing?, #sig, #succ, #to_code, #to_json, #to_s, #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
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
# 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 "year"
    sig(args)
    code_year
  when "years"
    sig(args)
    code_years
  when "month"
    sig(args)
    code_month
  when "months"
    sig(args)
    code_months
  when "week"
    sig(args)
    code_week
  when "weeks"
    sig(args)
    code_weeks
  when "week_day"
    sig(args)
    code_week_day
  when "week_days"
    sig(args)
    code_week_days
  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_dayObject



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

def self.code_day
  code_now.code_day
end

.code_daysObject



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

def self.code_days
  code_now.code_days
end

.code_format(format) ⇒ Object



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

def self.code_format(format)
  code_now.code_format(format)
end

.code_hourObject



82
83
84
# File 'lib/code/object/time.rb', line 82

def self.code_hour
  code_now.code_hour
end

.code_hoursObject



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

def self.code_hours
  code_now.code_hours
end

.code_minuteObject



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

def self.code_minute
  code_now.code_minute
end

.code_minutesObject



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

def self.code_minutes
  code_now.code_minutes
end

.code_monthObject



109
110
111
# File 'lib/code/object/time.rb', line 109

def self.code_month
  code_now.code_month
end

.code_monthsObject



113
114
115
# File 'lib/code/object/time.rb', line 113

def self.code_months
  code_now.code_months
end

.code_nowObject



96
97
98
99
# File 'lib/code/object/time.rb', line 96

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

.code_secondObject



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

def self.code_second
  code_now.code_second
end

.code_secondsObject



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

def self.code_seconds
  code_now.code_seconds
end

.code_tomorrowObject



86
87
88
89
# File 'lib/code/object/time.rb', line 86

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

.code_weekObject



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

def self.code_week
  code_now.code_week
end

.code_week_dayObject



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

def self.code_week_day
  code_now.code_week_day
end

.code_week_daysObject



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

def self.code_week_days
  code_now.code_week_days
end

.code_weeksObject



121
122
123
# File 'lib/code/object/time.rb', line 121

def self.code_weeks
  code_now.code_weeks
end

.code_yearObject



101
102
103
# File 'lib/code/object/time.rb', line 101

def self.code_year
  code_now.code_year
end

.code_yearsObject



105
106
107
# File 'lib/code/object/time.rb', line 105

def self.code_years
  code_now.code_years
end

.code_yesterdayObject



91
92
93
94
# File 'lib/code/object/time.rb', line 91

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

Instance Method Details

#call(**args) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/code/object/time.rb', line 169

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 "week"
    sig(args)
    code_week
  when "weeks"
    sig(args)
    code_weeks
  when "week_day"
    sig(args)
    code_week_day
  when "week_days"
    sig(args)
    code_week_days
  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:



243
244
245
246
247
248
# File 'lib/code/object/time.rb', line 243

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:



250
251
252
253
254
255
# File 'lib/code/object/time.rb', line 250

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



297
298
299
# File 'lib/code/object/time.rb', line 297

def code_day
  Integer.new(raw.day)
end

#code_daysObject



301
302
303
# File 'lib/code/object/time.rb', line 301

def code_days
  Integer.new(raw.day)
end

#code_format(format) ⇒ Object



329
330
331
332
333
# File 'lib/code/object/time.rb', line 329

def code_format(format)
  code_format = format.to_code

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

#code_future?Boolean

Returns:



261
262
263
# File 'lib/code/object/time.rb', line 261

def code_future?
  code_after?
end

#code_hourObject



305
306
307
# File 'lib/code/object/time.rb', line 305

def code_hour
  Integer.new(raw.hour)
end

#code_hoursObject



309
310
311
# File 'lib/code/object/time.rb', line 309

def code_hours
  Integer.new(raw.hour)
end

#code_minuteObject



313
314
315
# File 'lib/code/object/time.rb', line 313

def code_minute
  Integer.new(raw.min)
end

#code_minutesObject



317
318
319
# File 'lib/code/object/time.rb', line 317

def code_minutes
  Integer.new(raw.min)
end

#code_monthObject



273
274
275
# File 'lib/code/object/time.rb', line 273

def code_month
  Integer.new(raw.month)
end

#code_monthsObject



277
278
279
# File 'lib/code/object/time.rb', line 277

def code_months
  Integer.new(raw.month)
end

#code_past?Boolean

Returns:



257
258
259
# File 'lib/code/object/time.rb', line 257

def code_past?
  code_before?
end

#code_secondObject



321
322
323
# File 'lib/code/object/time.rb', line 321

def code_second
  Integer.new(raw.sec)
end

#code_secondsObject



325
326
327
# File 'lib/code/object/time.rb', line 325

def code_seconds
  Integer.new(raw.sec)
end

#code_weekObject



281
282
283
# File 'lib/code/object/time.rb', line 281

def code_week
  Integer.new(raw.to_date.cweek)
end

#code_week_dayObject



289
290
291
# File 'lib/code/object/time.rb', line 289

def code_week_day
  Integer.new(raw.wday)
end

#code_week_daysObject



293
294
295
# File 'lib/code/object/time.rb', line 293

def code_week_days
  Integer.new(raw.wday)
end

#code_weeksObject



285
286
287
# File 'lib/code/object/time.rb', line 285

def code_weeks
  Integer.new(raw.to_date.cweek)
end

#code_yearObject



265
266
267
# File 'lib/code/object/time.rb', line 265

def code_year
  Integer.new(raw.year)
end

#code_yearsObject



269
270
271
# File 'lib/code/object/time.rb', line 269

def code_years
  Integer.new(raw.year)
end