Class: Code::Object::Date

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

Constant Summary

Constants inherited from Code::Object

NUMBER_CLASSES

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_code, 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
# File 'lib/code/object/date.rb', line 6

def initialize(*args, **_kargs, &)
  @raw = ::Date.parse(args.map(&:to_s).join("-"))
rescue ::Date::Error
  @raw = ::Date.current
end

Class Method Details

.call(**args) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/code/object/date.rb', line 12

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

  case code_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



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

def self.code_hour
  code_today.code_hour
end

.code_nowObject



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

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

.code_todayObject



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

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

.code_tomorrowObject



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

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

.code_yesterdayObject



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

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

Instance Method Details

#call(**args) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/code/object/date.rb', line 60

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

  case code_operator.to_s
  when "hour"
    sig(args)
    code_hour
  when "hours"
    sig(args)
    code_hours
  when "minute"
    sig(args)
    code_minute
  when "minutes"
    sig(args)
    code_minutes
  when "second"
    sig(args)
    code_second
  when "seconds"
    sig(args)
    code_seconds
  else
    super
  end
end

#code_hourObject



87
88
89
# File 'lib/code/object/date.rb', line 87

def code_hour
  Integer.new(0)
end

#code_hoursObject



91
92
93
# File 'lib/code/object/date.rb', line 91

def code_hours
  Integer.new(0)
end

#code_minuteObject



95
96
97
# File 'lib/code/object/date.rb', line 95

def code_minute
  Integer.new(0)
end

#code_minutesObject



99
100
101
# File 'lib/code/object/date.rb', line 99

def code_minutes
  Integer.new(0)
end

#code_secondObject



103
104
105
# File 'lib/code/object/date.rb', line 103

def code_second
  Integer.new(0)
end

#code_secondsObject



107
108
109
# File 'lib/code/object/date.rb', line 107

def code_seconds
  Integer.new(0)
end