Class: Code::Object::Date
Instance Attribute Summary
Attributes inherited from Code::Object
#raw
Class Method Summary
collapse
Instance Method Summary
collapse
#<=>, #==, #call, #code_and_operator, code_and_operator, #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_string, #code_to_string, falsy?, #falsy?, #hash, inspect, maybe, multi_fetch, #multi_fetch, repeat, #sig, sig, #to_json, to_s, truthy?, #truthy?, |
Constructor Details
#initialize(*args, **_kargs, &_block) ⇒ Date
Returns a new instance of Date.
6
7
8
9
10
11
12
|
# File 'lib/code/object/date.rb', line 6
def initialize(*args, **_kargs, &_block)
raw = args.map(&:to_s).join("-").presence || ::Date.current.to_s
@raw = ::Date.parse(raw)
super
rescue ::Date::Error
raise Error, "#{raw.inspect} is an invalid date"
end
|
Class Method Details
.call(**args) ⇒ Object
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/code/object/date.rb', line 18
def self.call(**args)
operator = args.fetch(:operator, nil)
case operator.to_s
when "tomorrow"
sig(args)
code_tomorrow
when "yesterday"
sig(args)
code_yesterday
when "today"
sig(args)
code_today
when "now"
sig(args)
code_now
else
super
end
end
|
39
40
41
42
|
# File 'lib/code/object/date.rb', line 39
def self.code_now
::Time.zone ||= Time::DEFAULT_ZONE
new(::Time.zone.now.beginning_of_day)
end
|
.code_today ⇒ Object
44
45
46
47
|
# File 'lib/code/object/date.rb', line 44
def self.code_today
::Time.zone ||= Time::DEFAULT_ZONE
new(::Time.zone.now.beginning_of_day)
end
|
.code_tomorrow ⇒ Object
49
50
51
52
|
# File 'lib/code/object/date.rb', line 49
def self.code_tomorrow
::Time.zone ||= Time::DEFAULT_ZONE
new(::Time.zone.tomorrow.beginning_of_day)
end
|
.code_yesterday ⇒ Object
54
55
56
57
|
# File 'lib/code/object/date.rb', line 54
def self.code_yesterday
::Time.zone ||= Time::DEFAULT_ZONE
new(::Time.zone.yesterday.beginning_of_day)
end
|
14
15
16
|
# File 'lib/code/object/date.rb', line 14
def self.name
"Date"
end
|
Instance Method Details
67
68
69
|
# File 'lib/code/object/date.rb', line 67
def as_json(...)
raw.as_json(...)
end
|
59
60
61
|
# File 'lib/code/object/date.rb', line 59
def inspect
to_s
end
|
63
64
65
|
# File 'lib/code/object/date.rb', line 63
def to_s
raw.to_s
end
|