Class: Code::Object::Duration
- Inherits:
-
Code::Object
- Object
- Code::Object
- Code::Object::Duration
- Defined in:
- lib/code/object/duration.rb
Constant Summary
Constants inherited from Code::Object
Instance Attribute Summary
Attributes included from Concerns::Shared
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.
Methods inherited from Code::Object
code_new, #code_new, maybe, #name, repeat, |
Methods included from Concerns::Shared
#<=>, #==, #as_json, #code_and, #code_as_json, #code_compare, #code_deep_duplicate, #code_different, #code_duplicate, #code_equal, #code_exclamation_mark, #code_exclusive_range, #code_falsy?, code_fetch, #code_fetch, code_get, #code_get, #code_greater, #code_greater_or_equal, #code_inclusive_range, #code_inspect, #code_less, #code_less_or_equal, #code_methods, #code_name, #code_nothing?, #code_or, #code_self, #code_set, code_set, #code_something?, #code_strict_different, #code_strict_equal, #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_truthy?, #eql?, #falsy?, #hash, #inspect, #multi_fetch, #nothing?, #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.
6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/code/object/duration.rb', line 6 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 |
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 |
#code_ago ⇒ Object
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 |