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, #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) ⇒ 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, &)
  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
33
34
35
# 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
  when "hour"
    sig(args)
    code_hour
  else
    super
  end
end

.code_hourObject



37
38
39
# File 'lib/code/object/date.rb', line 37

def self.code_hour
  code_today.code_hour
end

.code_nowObject



41
42
43
44
# File 'lib/code/object/date.rb', line 41

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

.code_todayObject



46
47
48
49
# File 'lib/code/object/date.rb', line 46

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

.code_tomorrowObject



51
52
53
54
# File 'lib/code/object/date.rb', line 51

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

.code_yesterdayObject



56
57
58
59
# File 'lib/code/object/date.rb', line 56

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

Instance Method Details

#call(**args) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/code/object/date.rb', line 61

def call(**args)
  operator = args.fetch(:operator, nil)

  case operator.to_s
  when "hour"
    sig(args)
    code_hour
  else
    super
  end
end

#code_hourObject



73
74
75
# File 'lib/code/object/date.rb', line 73

def code_hour
  Integer.new(0)
end