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 collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Code::Object

#<=>, #==, code_and_operator, #code_and_operator, #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_or_operator, #code_or_operator, code_self, #code_self, code_to_string, #code_to_string, falsy?, #falsy?, #hash, inspect, maybe, multi_fetch, #multi_fetch, repeat, #sig, sig, #to_json, to_s, truthy?, #truthy?, |

Constructor Details

#initialize(time) ⇒ Time

Returns a new instance of Time.



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

def initialize(time)
  ::Time.zone ||= DEFAULT_ZONE
  time = time.raw if time.is_a?(Time)
  time = time.to_s if time.is_a?(::Time)
  @raw = ::Time.zone.parse(time)
end

Instance Attribute Details

#rawObject (readonly)

Returns the value of attribute raw.



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

def raw
  @raw
end

Class Method Details

.call(**args) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/code/object/time.rb', line 21

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
  else
    super
  end
end

.code_nowObject



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

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

.code_tomorrowObject



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

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

.nameObject



17
18
19
# File 'lib/code/object/time.rb', line 17

def self.name
  "Time"
end

Instance Method Details

#as_jsonObject



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

def as_json(...)
  raw.as_json(...)
end

#call(**args) ⇒ Object



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

def call(**args)
  operator = args.fetch(:operator, nil)
  arguments = args.fetch(:arguments, [])
  value = arguments.first&.value

  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:



69
70
71
72
# File 'lib/code/object/time.rb', line 69

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

#code_before?(other = nil) ⇒ Boolean

Returns:



74
75
76
77
# File 'lib/code/object/time.rb', line 74

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

#code_future?Boolean

Returns:



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

def code_future?
  code_after?
end

#code_past?Boolean

Returns:



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

def code_past?
  code_before?
end

#inspectObject



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

def inspect
  to_s
end

#to_sObject



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

def to_s
  raw.to_s
end