Class: Motion::Duration
- Inherits:
-
Object
- Object
- Motion::Duration
- Defined in:
- lib/project/motion-duration.rb
Constant Summary collapse
- UNITS =
[:seconds, :minutes, :hours, :days, :weeks]
- MULTIPLES =
{:seconds => 1, :minutes => 60, :hours => 3600, :days => 86400, :weeks => 604800, :second => 1, :minute => 60, :hour => 3600, :day => 86400, :week => 604800}
Instance Attribute Summary collapse
-
#days ⇒ Object
readonly
Returns the value of attribute days.
-
#hours ⇒ Object
readonly
Returns the value of attribute hours.
-
#minutes ⇒ Object
readonly
Returns the value of attribute minutes.
-
#seconds ⇒ Object
readonly
Returns the value of attribute seconds.
-
#total ⇒ Object
(also: #to_i)
readonly
Returns the value of attribute total.
-
#weeks ⇒ Object
readonly
Returns the value of attribute weeks.
Instance Method Summary collapse
- #%(other) ⇒ Object
- #*(other) ⇒ Object
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #/(other) ⇒ Object
-
#<=>(other) ⇒ Object
Compare this duration to another (or objects that respond to #to_i).
-
#blank? ⇒ Boolean
True if total is 0.
-
#initialize(args = 0) ⇒ Duration
constructor
Initialize a duration.
- #negative? ⇒ Boolean
-
#present? ⇒ Boolean
True if total different than 0.
- #total_days ⇒ Object
- #total_hours ⇒ Object
-
#total_minutes ⇒ Object
if/when Android ever gets define_method, we can restore this logic and dump the following 3 methods hipbyte.myjetbrains.com/youtrack/issue/RM-806 %w(minutes hours days).each do |meth| define_method(“total_#meth”) { @total / MULTIPLES } end.
Constructor Details
#initialize(args = 0) ⇒ Duration
Initialize a duration. ‘args’ can be a hash or anything else. If a hash is passed, it will be scanned for a key=>value pair of time units such as those listed in the Duration::UNITS array or Duration::MULTIPLES hash.
If anything else except a hash is passed, #to_i is invoked on that object and expects that it return the number of seconds desired for the duration.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/project/motion-duration.rb', line 24 def initialize(args = 0) if args.kind_of?(Hash) @seconds = 0 MULTIPLES.each do |unit, multiple| unit = unit.to_sym @seconds += args[unit].to_i * multiple if args.key?(unit) end # elsif args.kind_of?(String) and args[0] == 'P' # @seconds = ISO8601::Duration.new(args).to_seconds else @seconds = args.to_i end calculate! end |
Instance Attribute Details
#days ⇒ Object (readonly)
Returns the value of attribute days.
16 17 18 |
# File 'lib/project/motion-duration.rb', line 16 def days @days end |
#hours ⇒ Object (readonly)
Returns the value of attribute hours.
16 17 18 |
# File 'lib/project/motion-duration.rb', line 16 def hours @hours end |
#minutes ⇒ Object (readonly)
Returns the value of attribute minutes.
16 17 18 |
# File 'lib/project/motion-duration.rb', line 16 def minutes @minutes end |
#seconds ⇒ Object (readonly)
Returns the value of attribute seconds.
16 17 18 |
# File 'lib/project/motion-duration.rb', line 16 def seconds @seconds end |
#total ⇒ Object (readonly) Also known as: to_i
Returns the value of attribute total.
16 17 18 |
# File 'lib/project/motion-duration.rb', line 16 def total @total end |
#weeks ⇒ Object (readonly)
Returns the value of attribute weeks.
16 17 18 |
# File 'lib/project/motion-duration.rb', line 16 def weeks @weeks end |
Instance Method Details
#%(other) ⇒ Object
62 63 64 |
# File 'lib/project/motion-duration.rb', line 62 def %(other) Duration.new(@total % other.to_i) end |
#*(other) ⇒ Object
54 55 56 |
# File 'lib/project/motion-duration.rb', line 54 def *(other) Duration.new(@total * other.to_i) end |
#+(other) ⇒ Object
46 47 48 |
# File 'lib/project/motion-duration.rb', line 46 def +(other) Duration.new(@total + other.to_i) end |
#-(other) ⇒ Object
50 51 52 |
# File 'lib/project/motion-duration.rb', line 50 def -(other) Duration.new(@total - other.to_i) end |
#/(other) ⇒ Object
58 59 60 |
# File 'lib/project/motion-duration.rb', line 58 def /(other) Duration.new(@total / other.to_i) end |
#<=>(other) ⇒ Object
Compare this duration to another (or objects that respond to #to_i)
41 42 43 44 |
# File 'lib/project/motion-duration.rb', line 41 def <=>(other) return false unless other.is_a?(Duration) (@total <=> other.to_i).duration_comparable_bool end |
#blank? ⇒ Boolean
89 90 91 |
# File 'lib/project/motion-duration.rb', line 89 def blank? @total == 0 end |
#negative? ⇒ Boolean
98 99 100 |
# File 'lib/project/motion-duration.rb', line 98 def negative? @negative end |
#present? ⇒ Boolean
94 95 96 |
# File 'lib/project/motion-duration.rb', line 94 def present? !blank? end |
#total_days ⇒ Object
82 83 84 |
# File 'lib/project/motion-duration.rb', line 82 def total_days @total / MULTIPLES[:days] end |
#total_hours ⇒ Object
78 79 80 |
# File 'lib/project/motion-duration.rb', line 78 def total_hours @total / MULTIPLES[:hours] end |
#total_minutes ⇒ Object
if/when Android ever gets define_method, we can restore this logic and dump the following 3 methods hipbyte.myjetbrains.com/youtrack/issue/RM-806 %w(minutes hours days).each do |meth|
define_method("total_#{meth}") { @total / MULTIPLES[meth.to_sym] }
end
74 75 76 |
# File 'lib/project/motion-duration.rb', line 74 def total_minutes @total / MULTIPLES[:minutes] end |