Class: Code::Object::Duration
Constant Summary
Constants inherited
from Code::Object
NUMBER_CLASSES
Instance Attribute Summary
Attributes inherited from Code::Object
#raw
Instance Method Summary
collapse
#<=>, #==, 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_code, 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
14
15
16
17
|
# File 'lib/code/object/duration.rb', line 6
def initialize(*args, **_kargs, &)
@raw =
if args.first.is_an?(::ActiveSupport::Duration)
args.first
elsif args.first.is_a?(Duration)
args.first.raw
else
::ActiveSupport::Duration.parse(args.first.to_s)
end
rescue ::ActiveSupport::Duration::ISO8601Parser::ParsingError
@raw = 0.seconds
end
|
Instance Method Details
#call(**args) ⇒ Object
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/code/object/duration.rb', line 19
def call(**args)
code_operator = args.fetch(:operator, nil).to_code
case code_operator.to_s
when "ago"
sig(args)
code_ago
when "from_now"
sig(args)
code_from_now
else
super
end
end
|
34
35
36
|
# File 'lib/code/object/duration.rb', line 34
def code_ago
Time.new(raw.ago)
end
|
#code_from_now ⇒ Object
38
39
40
|
# File 'lib/code/object/duration.rb', line 38
def code_from_now
Time.new(raw.from_now)
end
|