Class: Code::Object::Time

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

Constant Summary collapse

DEFAULT_ZONE =
"Etc/UTC"

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

Returns a new instance of Time.



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

def initialize(*args, **_kargs, &_block)
  ::Time.zone ||= DEFAULT_ZONE
  raw = args.first
  raw = raw.raw if raw.is_an?(Object)
  raw = raw.presence || ::Time.zone.now
  @raw = ::Time.zone.parse(raw.to_s).presence || ::Time.zone.now
end

Class Method Details

.call(**args) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/code/object/time.rb', line 16

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

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

.code_nowObject



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

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

.code_tomorrowObject



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

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

.code_yesterdayObject



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

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

Instance Method Details

#call(**args) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/code/object/time.rb', line 49

def call(**args)
  operator = args.fetch(:operator, nil)
  arguments = args.fetch(:arguments, List.new)
  value = arguments.code_first

  case operator.to_s
  when "after?"
    sig(args) { Time.maybe }
    code_after?(value)
  when "before?"
    sig(args) { Time.maybe }
    code_before?(value)
  when "past?"
    sig(args)
    code_past?
  when "future?"
    sig(args)
    code_future?
  else
    super
  end
end

#code_after?(other = nil) ⇒ Boolean

Returns:



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

def code_after?(other = nil)
  other = Time.code_now if other.nil? || other.is_a?(Nothing)
  Boolean.new(raw.after?(other.raw))
end

#code_before?(other = nil) ⇒ Boolean

Returns:



77
78
79
80
# File 'lib/code/object/time.rb', line 77

def code_before?(other = nil)
  other = Time.code_now if other.nil? || other.is_a?(Nothing)
  Boolean.new(raw.before?(other.raw))
end

#code_future?Boolean

Returns:



86
87
88
# File 'lib/code/object/time.rb', line 86

def code_future?
  code_after?
end

#code_past?Boolean

Returns:



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

def code_past?
  code_before?
end