Class: Code::Object::Time
Constant Summary
collapse
- DEFAULT_ZONE =
"Etc/UTC"
Instance Attribute Summary
Attributes inherited from Code::Object
#raw
Class Method Summary
collapse
Instance Method Summary
collapse
#<=>, #==, #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_json, #to_json, to_s, truthy?, #truthy?, |
Constructor Details
#initialize(*args, **_kargs) ⇒ Time
Returns a new instance of Time.
8
9
10
11
12
13
14
|
# File 'lib/code/object/time.rb', line 8
def initialize(*args, **_kargs, &)
::Time.zone ||= DEFAULT_ZONE
raw = args.first
raw = raw.raw if raw.is_an?(Object)
raw = raw.presence || ::Time.zone.now
@raw = ::Time.zone.parse(raw.to_s).presence || ::Time.zone.now
end
|
Class Method Details
.call(**args) ⇒ Object
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
# File 'lib/code/object/time.rb', line 16
def self.call(**args)
operator = args.fetch(:operator, nil)
case 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_hour ⇒ Object
37
38
39
|
# File 'lib/code/object/time.rb', line 37
def self.code_hour
code_now.code_hour
end
|
51
52
53
54
|
# File 'lib/code/object/time.rb', line 51
def self.code_now
::Time.zone ||= DEFAULT_ZONE
new(::Time.zone.now)
end
|
.code_tomorrow ⇒ Object
41
42
43
44
|
# File 'lib/code/object/time.rb', line 41
def self.code_tomorrow
::Time.zone ||= DEFAULT_ZONE
new(::Time.zone.tomorrow)
end
|
.code_yesterday ⇒ Object
46
47
48
49
|
# File 'lib/code/object/time.rb', line 46
def self.code_yesterday
::Time.zone ||= DEFAULT_ZONE
new(::Time.zone.yesterday)
end
|
Instance Method Details
#call(**args) ⇒ Object
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 56
def call(**args)
operator = args.fetch(:operator, nil)
arguments = args.fetch(:arguments, List.new)
value = arguments.code_first
case operator.to_s
when "after?"
sig(args) { Time.maybe }
code_after?(value)
when "before?"
sig(args) { Time.maybe }
code_before?(value)
when "past?"
sig(args)
code_past?
when "future?"
sig(args)
code_future?
when "hour"
sig(args)
code_hour
else
super
end
end
|
#code_after?(other = nil) ⇒ Boolean
82
83
84
85
|
# File 'lib/code/object/time.rb', line 82
def code_after?(other = nil)
other = Time.code_now if other.nil? || other.is_a?(Nothing)
Boolean.new(raw.after?(other.raw))
end
|
#code_before?(other = nil) ⇒ Boolean
87
88
89
90
|
# File 'lib/code/object/time.rb', line 87
def code_before?(other = nil)
other = Time.code_now if other.nil? || other.is_a?(Nothing)
Boolean.new(raw.before?(other.raw))
end
|
#code_future? ⇒ Boolean
96
97
98
|
# File 'lib/code/object/time.rb', line 96
def code_future?
code_after?
end
|
#code_hour ⇒ Object
100
101
102
|
# File 'lib/code/object/time.rb', line 100
def code_hour
Integer.new(raw.hour)
end
|
92
93
94
|
# File 'lib/code/object/time.rb', line 92
def code_past?
code_before?
end
|