Class: TimeSlice::Duration
- Inherits:
-
ActiveSupport::Duration
- Object
- ActiveSupport::Duration
- TimeSlice::Duration
- Defined in:
- lib/time_slice/duration.rb
Constant Summary collapse
- MAP =
{ "s" => :seconds, "m" => :minutes, "h" => :hours, "d" => :days, "w" => :weeks, "mo" => :months, "y" => :years, }.freeze
Instance Attribute Summary collapse
-
#unit ⇒ Object
readonly
Returns the value of attribute unit.
Instance Method Summary collapse
-
#initialize(value) ⇒ Duration
constructor
A new instance of Duration.
- #period ⇒ Object
Constructor Details
#initialize(value) ⇒ Duration
Returns a new instance of Duration.
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/time_slice/duration.rb', line 47 def initialize(value) raise ArgumentError, "Fractional values are not allowed" if value && value.include?('.') if value.is_a?(String) && value.match(/([0-9]+)([a-z]+)/i).try(:length) == 3 && (@unit = MAP[$2]) value = $1.to_i super(value * PARTS_IN_SECONDS[@unit], @unit => value) else raise ArgumentError, "#{value} is not a valid duration" end end |
Instance Attribute Details
#unit ⇒ Object (readonly)
Returns the value of attribute unit.
45 46 47 |
# File 'lib/time_slice/duration.rb', line 45 def unit @unit end |
Instance Method Details
#period ⇒ Object
58 59 60 |
# File 'lib/time_slice/duration.rb', line 58 def period @period ||= "#{value / 1.send(@unit)}#{@unit.to_s[0]}" end |