Class: Code::Object::Time

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

Constant Summary collapse

DEFAULT_ZONE =
"Etc/UTC"

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) ⇒ Time

Returns a new instance of Time.



8
9
10
11
# File 'lib/code/object/time.rb', line 8

def initialize(*args, **_kargs, &)
  ::Time.zone ||= DEFAULT_ZONE
  @raw = ::Time.zone.parse(args.first.to_s) || ::Time.zone.now
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/time.rb', line 13

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

  case code_operator.to_s
  when "now"
    sig(args)
    code_now
  when "tomorrow"
    sig(args)
    code_tomorrow
  when "yesterday"
    sig(args)
    code_yesterday
  when "hour"
    sig(args)
    code_hour
  else
    super
  end
end

.code_hourObject



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

def self.code_hour
  code_now.code_hour
end

.code_nowObject



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

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

.code_tomorrowObject



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

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

.code_yesterdayObject



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

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

Instance Method Details

#call(**args) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/code/object/time.rb', line 53

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 "after?"
    sig(args) { Time.maybe }
    code_after?(code_value)
  when "before?"
    sig(args) { Time.maybe }
    code_before?(code_value)
  when "past?"
    sig(args)
    code_past?
  when "future?"
    sig(args)
    code_future?
  when "hour"
    sig(args)
    code_hour
  when "format"
    sig(args) { String }
    code_format(code_value)
  else
    super
  end
end

#code_after?(other = nil) ⇒ Boolean

Returns:



82
83
84
85
86
87
# File 'lib/code/object/time.rb', line 82

def code_after?(other = nil)
  code_other = other.to_code
  code_other = Time.code_now if code_other.nothing?

  Boolean.new(raw.after?(code_other.raw))
end

#code_before?(other = nil) ⇒ Boolean

Returns:



89
90
91
92
93
94
# File 'lib/code/object/time.rb', line 89

def code_before?(other = nil)
  code_other = other.to_code
  code_other = Time.code_now if code_other.nothing?

  Boolean.new(raw.before?(code_other.raw))
end

#code_format(format) ⇒ Object



108
109
110
111
112
# File 'lib/code/object/time.rb', line 108

def code_format(format)
  code_format = format.to_code

  String.new(raw.strftime(code_format.raw))
end

#code_future?Boolean

Returns:



100
101
102
# File 'lib/code/object/time.rb', line 100

def code_future?
  code_after?
end

#code_hourObject



104
105
106
# File 'lib/code/object/time.rb', line 104

def code_hour
  Integer.new(raw.hour)
end

#code_past?Boolean

Returns:



96
97
98
# File 'lib/code/object/time.rb', line 96

def code_past?
  code_before?
end