Class: Code::Object::Duration

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Code::Object

#<=>, #==, call, #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(duration) ⇒ Duration

Returns a new instance of Duration.



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

def initialize(duration)
  duration = duration.raw if duration.is_a?(Duration)
  @raw = duration
end

Instance Attribute Details

#rawObject (readonly)

Returns the value of attribute raw.



6
7
8
# File 'lib/code/object/duration.rb', line 6

def raw
  @raw
end

Class Method Details

.nameObject



13
14
15
# File 'lib/code/object/duration.rb', line 13

def self.name
  "Duration"
end

Instance Method Details

#as_jsonObject



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

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

#call(**args) ⇒ Object



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

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



32
33
34
# File 'lib/code/object/duration.rb', line 32

def code_ago
  Time.new(raw.ago)
end

#code_from_nowObject



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

def code_from_now
  Time.new(raw.from_now)
end

#inspectObject



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

def inspect
  to_s
end

#to_sObject



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

def to_s
  raw.to_s
end