Class: Code::Object::Duration
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
Class Method Summary collapse
Instance Method Summary collapse
- #call(**args) ⇒ Object
- #code_ago ⇒ Object
- #code_from_now ⇒ Object
-
#initialize(*args, **_kargs, &_block) ⇒ Duration
constructor
A new instance of Duration.
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 |