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, #inspect, maybe, #multi_fetch, multi_fetch, repeat, sig, #sig, #succ, to_json, #to_json, #to_s, to_s, truthy?, #truthy?, |
Constructor Details
#initialize(*args, **_kargs, &_block) ⇒ Time
Returns a new instance of Time.
8
9
10
11
12
13
|
# File 'lib/code/object/time.rb', line 8
def initialize(*args, **_kargs, &_block)
::Time.zone ||= DEFAULT_ZONE
raw = args.first.presence || ::Time.zone.now
raw = raw.raw if raw.is_an?(Object)
@raw = ::Time.zone.parse(raw.to_s)
end
|
Class Method Details
.call(**args) ⇒ Object
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/code/object/time.rb', line 15
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
else
super
end
end
|
43
44
45
46
|
# File 'lib/code/object/time.rb', line 43
def self.code_now
::Time.zone ||= DEFAULT_ZONE
new(::Time.zone.now)
end
|
.code_tomorrow ⇒ Object
33
34
35
36
|
# File 'lib/code/object/time.rb', line 33
def self.code_tomorrow
::Time.zone ||= DEFAULT_ZONE
new(::Time.zone.tomorrow)
end
|
.code_yesterday ⇒ Object
38
39
40
41
|
# File 'lib/code/object/time.rb', line 38
def self.code_yesterday
::Time.zone ||= DEFAULT_ZONE
new(::Time.zone.yesterday)
end
|
Instance Method Details
#call(**args) ⇒ Object
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/code/object/time.rb', line 48
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?
else
super
end
end
|
#code_after?(other = nil) ⇒ Boolean
71
72
73
74
|
# File 'lib/code/object/time.rb', line 71
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
76
77
78
79
|
# File 'lib/code/object/time.rb', line 76
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
85
86
87
|
# File 'lib/code/object/time.rb', line 85
def code_future?
code_after?
end
|
81
82
83
|
# File 'lib/code/object/time.rb', line 81
def code_past?
code_before?
end
|