Class: DaruLite::DateOffset
- Defined in:
- lib/daru_lite/date_time/offsets.rb
Overview
Generic class for generating date offsets.
Direct Known Subclasses
Instance Method Summary collapse
-
#+(other) ⇒ Object
Offset a DateTime forward.
-
#-(other) ⇒ Object
Offset a DateTime backward.
- #-@ ⇒ Object
-
#initialize(opts = {}) ⇒ DateOffset
constructor
A DaruLite::DateOffset object is created by a passing certain options to the constructor, which determine the kind of offset the object will support.
Constructor Details
#initialize(opts = {}) ⇒ DateOffset
A DaruLite::DateOffset object is created by a passing certain options to the constructor, which determine the kind of offset the object will support.
You can pass one of the following options followed by their number to the DateOffset constructor:
-
:secs - Create a seconds offset
-
:mins - Create a minutes offset
-
:hours - Create an hours offset
-
:days - Create a days offset
-
:weeks - Create a weeks offset
-
:months - Create a months offset
-
:years - Create a years offset
Additionaly, passing the ‘:n` option will apply the offset that many times.
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/daru_lite/date_time/offsets.rb', line 36 def initialize(opts = {}) n = opts[:n] || 1 Offsets::LIST.each do |key, klass| if opts.key?(key) @offset = klass.new(n * opts[key]) break end end @offset = Offsets::Day.new(7 * n * opts[:weeks]) if opts[:weeks] end |
Instance Method Details
#+(other) ⇒ Object
Offset a DateTime forward.
51 52 53 |
# File 'lib/daru_lite/date_time/offsets.rb', line 51 def +(other) @offset + other end |
#-(other) ⇒ Object
Offset a DateTime backward.
58 59 60 |
# File 'lib/daru_lite/date_time/offsets.rb', line 58 def -(other) @offset - other end |
#-@ ⇒ Object
62 63 64 |
# File 'lib/daru_lite/date_time/offsets.rb', line 62 def -@ NegativeDateOffset.new(self) end |