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 included from Concerns::Shared

#raw

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Code::Object

#code_new, code_new, maybe, name, #name, repeat, |

Methods included from Concerns::Shared

#<=>, #==, #as_json, #code_and, #code_as_json, #code_deep_duplicate, #code_different, #code_duplicate, #code_equal_equal, #code_equal_equal_equal, #code_exclamation_point, #code_exclusive_range, #code_falsy?, #code_fetch, code_fetch, code_get, #code_get, #code_inclusive_range, #code_inspect, #code_name, #code_or, #code_self, #code_set, code_set, #code_to_boolean, #code_to_class, #code_to_date, #code_to_decimal, #code_to_dictionary, #code_to_duration, #code_to_integer, #code_to_json, #code_to_list, #code_to_nothing, #code_to_parameter, #code_to_range, #code_to_string, #code_to_time, #code_truthy?, #eql?, #falsy?, #hash, #inspect, #multi_fetch, #nothing?, #sig, #succ, #to_code, #to_json, #to_s, #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
35
36
37
# 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 "current"
    sig(args)
    code_current
  when "now"
    sig(args)
    code_now
  when "hour"
    sig(args)
    code_hour
  else
    super
  end
end

.code_currentObject



53
54
55
56
# File 'lib/code/object/date.rb', line 53

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

.code_hourObject



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

def self.code_hour
  code_today.code_hour
end

.code_nowObject



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

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

.code_todayObject



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

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

.code_tomorrowObject



58
59
60
61
# File 'lib/code/object/date.rb', line 58

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

.code_yesterdayObject



63
64
65
66
# File 'lib/code/object/date.rb', line 63

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

Instance Method Details

#call(**args) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/code/object/date.rb', line 68

def call(**args)
  code_operator = args.fetch(:operator, nil).to_code
  code_arguments = args.fetch(:arguments, []).to_code
  code_value = code_arguments.code_first

  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
  when "change"
    sig(args) do
      {
        year: (String | Integer).maybe,
        month: (String | Integer).maybe,
        day: (String | Integer).maybe
      }
    end

    if code_value.nothing?
      code_change
    else
      code_change(
        year: code_value.code_get(:year),
        month: code_value.code_get(:month),
        day: code_value.code_get(:day)
      )
    end
  else
    super
  end
end

#code_change(year: nil, month: nil, day: nil) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/code/object/date.rb', line 139

def code_change(year: nil, month: nil, day: nil)
  code_year = year.to_code
  code_month = month.to_code
  code_day = day.to_code

  raw.change(
    **{
      year: code_year.raw,
      month: code_month.raw,
      day: code_day.raw
    }.compact
  )

  self
end

#code_hourObject



115
116
117
# File 'lib/code/object/date.rb', line 115

def code_hour
  Integer.new(0)
end

#code_hoursObject



119
120
121
# File 'lib/code/object/date.rb', line 119

def code_hours
  Integer.new(0)
end

#code_minuteObject



123
124
125
# File 'lib/code/object/date.rb', line 123

def code_minute
  Integer.new(0)
end

#code_minutesObject



127
128
129
# File 'lib/code/object/date.rb', line 127

def code_minutes
  Integer.new(0)
end

#code_secondObject



131
132
133
# File 'lib/code/object/date.rb', line 131

def code_second
  Integer.new(0)
end

#code_secondsObject



135
136
137
# File 'lib/code/object/date.rb', line 135

def code_seconds
  Integer.new(0)
end