Module: Maid::NumericExtensions
- Included in:
- Numeric
- Defined in:
- lib/maid/numeric_extensions.rb
Overview
From github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/numeric/time.rb, with some modifications since active_support ruins Logger by overriding its functionality.
Instance Method Summary collapse
-
#ago(time = ::Time.now) ⇒ Object
(also: #until)
Reads best without arguments: 10.minutes.ago.
- #days ⇒ Object (also: #day)
- #fortnights ⇒ Object (also: #fortnight)
- #hours ⇒ Object (also: #hour)
- #minutes ⇒ Object (also: #minute)
-
#seconds ⇒ Object
(also: #second)
Enables the use of time calculations and declarations, like 45.minutes + 2.hours + 4.years.
-
#since(time = ::Time.now) ⇒ Object
(also: #from_now)
Reads best with argument: 10.minutes.since(time).
-
#since?(other_time) ⇒ Boolean
Reads well in a case like:.
- #weeks ⇒ Object (also: #week)
Instance Method Details
#ago(time = ::Time.now) ⇒ Object Also known as: until
Reads best without arguments: 10.minutes.ago
62 63 64 |
# File 'lib/maid/numeric_extensions.rb', line 62 def ago(time = ::Time.now) time - self end |
#days ⇒ Object Also known as: day
46 47 48 |
# File 'lib/maid/numeric_extensions.rb', line 46 def days self * 24.hours end |
#fortnights ⇒ Object Also known as: fortnight
56 57 58 |
# File 'lib/maid/numeric_extensions.rb', line 56 def fortnights self * 2.weeks end |
#hours ⇒ Object Also known as: hour
41 42 43 |
# File 'lib/maid/numeric_extensions.rb', line 41 def hours self * 3600 end |
#minutes ⇒ Object Also known as: minute
36 37 38 |
# File 'lib/maid/numeric_extensions.rb', line 36 def minutes self * 60 end |
#seconds ⇒ Object Also known as: second
Enables the use of time calculations and declarations, like 45.minutes + 2.hours + 4.years.
These methods use Time#advance for precise date calculations when using from_now, ago, etc. as well as adding or subtracting their results from a Time object. For example:
# equivalent to Time.now.advance(:months => 1)
1.month.from_now
# equivalent to Time.now.advance(:years => 2)
2.years.from_now
# equivalent to Time.now.advance(:months => 4, :years => 5)
(4.months + 5.years).from_now
While these methods provide precise calculation when used as in the examples above, care should be taken to note that this is not true if the result of ‘months’, ‘years’, etc is converted before use:
# equivalent to 30.days.to_i.from_now
1.month.to_i.from_now
# equivalent to 365.25.days.to_f.from_now
1.year.to_f.from_now
In such cases, Ruby’s core Date and Time should be used for precision date and time arithmetic
31 32 33 |
# File 'lib/maid/numeric_extensions.rb', line 31 def seconds self end |
#since(time = ::Time.now) ⇒ Object Also known as: from_now
Reads best with argument: 10.minutes.since(time)
70 71 72 |
# File 'lib/maid/numeric_extensions.rb', line 70 def since(time = ::Time.now) time + self end |
#since?(other_time) ⇒ Boolean
Reads well in a case like:
1.week.since? last_accessed('filename')
86 87 88 |
# File 'lib/maid/numeric_extensions.rb', line 86 def since?(other_time) other_time < self.ago end |
#weeks ⇒ Object Also known as: week
51 52 53 |
# File 'lib/maid/numeric_extensions.rb', line 51 def weeks self * 7.days end |