Class: Code::Object::Duration

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

Constant Summary collapse

CLASS_DOCUMENTATION =
{
  name: "Duration",
  description: "stores a length of time for shifting dates and times.",
  examples: ["1.day", "2.hours", "Duration.new(\"P1D\")"]
}.freeze
INSTANCE_FUNCTIONS =
{
  "ago" => {
    name: "ago",
    description: "returns the current time minus this duration.",
    examples: %w[1.day.ago 2.hours.ago 30.minutes.ago]
  },
  "from_now" => {
    name: "from_now",
    description: "returns the current time plus this duration.",
    examples: %w[1.day.from_now 2.hours.from_now 30.minutes.from_now]
  }
}.freeze

Constants inherited from Code::Object

CLASS_FUNCTIONS, NUMBER_CLASSES

Constants included from Concerns::Shared

Concerns::Shared::COMPOUND_ASSIGNMENT_OPERATORS, Concerns::Shared::OPERATOR_METHOD_ALIASES, Concerns::Shared::SHARED_OPERATORS

Instance Attribute Summary

Attributes included from Concerns::Shared

#functions, #raw

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Code::Object

class_documentation, class_functions, code_new, #code_new, documentation, documentation_for, documented_functions_for, function_documentation_for, function_documentation_registry_for, functions, inherited_function_documentation_for, instance_functions, maybe, #name, repeat, sorted_dictionary, |

Methods included from Concerns::Shared

#<=>, #==, #as_json, #blank?, #code_and, #code_as_json, #code_blank?, #code_class_functions, #code_compare, #code_deep_duplicate, #code_different, #code_documentable_functions, #code_documentation, #code_duplicate, #code_dynamic_call, #code_equal, #code_exclamation_mark, #code_exclusive_range, #code_false?, #code_falsy?, code_fetch, #code_fetch, #code_functions, code_get, #code_get, #code_greater, #code_greater_or_equal, #code_has_key?, #code_inclusive_range, #code_inspect, #code_instance_functions, #code_instance_of?, #code_is_a?, #code_itself, #code_less, #code_less_or_equal, #code_name, #code_nothing?, #code_or, #code_presence, #code_presence_in, #code_present?, #code_respond_to?, #code_same_object?, #code_self, #code_send, code_set, #code_set, #code_something?, #code_strict_different, #code_strict_equal, #code_tap, #code_then, #code_to_boolean, #code_to_class, #code_to_date, #code_to_decimal, #code_to_dictionary, #code_to_duration, #code_to_integer, #code_to_json, #code_to_list, #code_to_nothing, #code_to_parameter, #code_to_range, #code_to_string, #code_to_time, #code_true?, #code_truthy?, #eql?, #falsy?, #hash, #inspect, #multi_fetch, #nothing?, #present?, #sig, #something?, #succ, #to_code, #to_i, #to_json, #to_s, #truthy?

Constructor Details

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

Returns a new instance of Duration.



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/code/object/duration.rb', line 30

def initialize(*args, **_kargs, &_block)
  self.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
  self.raw = 0.seconds
end

Class Method Details

.function_documentation(scope) ⇒ Object



24
25
26
27
28
# File 'lib/code/object/duration.rb', line 24

def self.function_documentation(scope)
  return INSTANCE_FUNCTIONS if scope == :instance

  {}
end

Instance Method Details

#call(**args) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/code/object/duration.rb', line 43

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

#code_agoObject



58
59
60
# File 'lib/code/object/duration.rb', line 58

def code_ago
  Time.new(raw.ago)
end

#code_from_nowObject



62
63
64
# File 'lib/code/object/duration.rb', line 62

def code_from_now
  Time.new(raw.from_now)
end