Class: Code::Object::Date

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

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, #call, 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) ⇒ Date

Returns a new instance of Date.



6
7
8
9
10
11
# 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)
rescue ::Date::Error
  raise Error, "#{raw.inspect} is an invalid date"
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/date.rb', line 13

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

.code_nowObject



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

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

.code_todayObject



39
40
41
42
# File 'lib/code/object/date.rb', line 39

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

.code_tomorrowObject



44
45
46
47
# File 'lib/code/object/date.rb', line 44

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

.code_yesterdayObject



49
50
51
52
# File 'lib/code/object/date.rb', line 49

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