Class: Code::Object::Duration

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

Instance Attribute Summary

Attributes inherited from Code::Object

#raw

Instance Method Summary collapse

Methods inherited from Code::Object

#<=>, #==, as_json, #as_json, call, #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_json, #to_json, to_s, truthy?, #truthy?, |

Constructor Details

#initialize(*args, **_kargs) ⇒ Duration

Returns a new instance of Duration.



6
7
8
9
10
11
12
13
# File 'lib/code/object/duration.rb', line 6

def initialize(*args, **_kargs, &)
  raw = args.first || 0.seconds
  raw = raw.raw if raw.is_an?(Object)
  raw = raw.iso8601 if raw.is_an?(::ActiveSupport::Duration)
  @raw = ::ActiveSupport::Duration.parse(raw.to_s)
rescue ::ActiveSupport::Duration::ISO8601Parser::ParsingError
  raise Error, "#{raw.inspect} is not a valid duration"
end

Instance Method Details

#call(**args) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/code/object/duration.rb', line 15

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

  case operator.to_s
  when "ago"
    sig(args)
    code_ago
  when "from_now"
    sig(args)
    code_from_now
  else
    super
  end
end

#code_agoObject



30
31
32
# File 'lib/code/object/duration.rb', line 30

def code_ago
  Time.new(raw.ago)
end

#code_from_nowObject



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

def code_from_now
  Time.new(raw.from_now)
end