Class: DurationRange::Time
- Inherits:
-
Object
- Object
- DurationRange::Time
show all
- Includes:
- CommonFunctions
- Defined in:
- lib/duration_range/time.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#initialize(today: ::Date.today, with_localtime: false) ⇒ Time
constructor
-
#last_month(as: :hash, count: 1) ⇒ Object
-
#last_week(as: :hash, count: 1) ⇒ Object
-
#month(month, as: :hash) ⇒ Object
-
#months(*months, as: :hash) ⇒ Object
-
#next_month(as: :hash, count: 1) ⇒ Object
-
#next_week(as: :hash, count: 1) ⇒ Object
-
#this_month(as: :hash, reference_date: today) ⇒ Object
-
#this_week(as: :hash, reference_date: today) ⇒ Object
-
#tidy_with_time(as:, begin_date:, end_date:) ⇒ Object
:reek:TooManyStatements :reek:ControlParameter.
-
#to_time(date) ⇒ Time
#modes, #month_as_date, #months_as_date, #this_month_as_date, #tidy_with_date
Constructor Details
#initialize(today: ::Date.today, with_localtime: false) ⇒ Time
14
15
16
17
|
# File 'lib/duration_range/time.rb', line 14
def initialize(today: ::Date.today, with_localtime: false)
@today = today
@with_localtime = with_localtime
end
|
Instance Attribute Details
#today ⇒ Object
Returns the value of attribute today.
18
19
20
|
# File 'lib/duration_range/time.rb', line 18
def today
@today
end
|
#with_localtime ⇒ Object
Returns the value of attribute with_localtime.
18
19
20
|
# File 'lib/duration_range/time.rb', line 18
def with_localtime
@with_localtime
end
|
Instance Method Details
#last_month(as: :hash, count: 1) ⇒ Object
95
96
97
|
# File 'lib/duration_range/time.rb', line 95
def last_month(as: :hash, count: 1)
this_month(as: as, reference_date: today.months_ago(count))
end
|
#last_week(as: :hash, count: 1) ⇒ Object
124
125
126
|
# File 'lib/duration_range/time.rb', line 124
def last_week(as: :hash, count: 1)
this_week(as: as, reference_date: today.weeks_ago(count))
end
|
#month(month, as: :hash) ⇒ Object
53
54
55
56
57
|
# File 'lib/duration_range/time.rb', line 53
def month(month, as: :hash)
dates = month_as_date(month, as: :hash)
tidy_with_time(as: as, begin_date: dates[:begin], end_date: dates[:end] + 1)
end
|
#months(*months, as: :hash) ⇒ Object
64
65
66
67
68
|
# File 'lib/duration_range/time.rb', line 64
def months(*months, as: :hash)
dates = months_as_date(*months, as: :hash)
tidy_with_time(as: as, begin_date: dates[:begin], end_date: dates[:end] + 1)
end
|
#next_month(as: :hash, count: 1) ⇒ Object
86
87
88
|
# File 'lib/duration_range/time.rb', line 86
def next_month(as: :hash, count: 1)
this_month(as: as, reference_date: today.months_since(count))
end
|
#next_week(as: :hash, count: 1) ⇒ Object
115
116
117
|
# File 'lib/duration_range/time.rb', line 115
def next_week(as: :hash, count: 1)
this_week(as: as, reference_date: today.weeks_since(count))
end
|
#this_month(as: :hash, reference_date: today) ⇒ Object
75
76
77
78
79
|
# File 'lib/duration_range/time.rb', line 75
def this_month(as: :hash, reference_date: today)
dates = this_month_as_date(as: :hash, reference_date: reference_date)
tidy_with_time(as: as, begin_date: dates[:begin], end_date: dates[:end] + 1)
end
|
#this_week(as: :hash, reference_date: today) ⇒ Object
104
105
106
107
108
|
# File 'lib/duration_range/time.rb', line 104
def this_week(as: :hash, reference_date: today)
dates = super(as: :hash, reference_date: reference_date)
tidy_with_time(as: as, begin_date: dates[:begin], end_date: dates[:end] + 1)
end
|
#tidy_with_time(as:, begin_date:, end_date:) ⇒ Object
:reek:TooManyStatements :reek:ControlParameter
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/duration_range/time.rb', line 27
def tidy_with_time(as:, begin_date:, end_date:)
case as
when :array
tidy_with_date(as: :array, begin_date: begin_date, end_date: end_date).map { |date| to_time(date) }
when :hash
{
begin: to_time(begin_date),
end: to_time(end_date)
}
end
end
|
#to_time(date) ⇒ Time
44
45
46
|
# File 'lib/duration_range/time.rb', line 44
def to_time(date)
with_localtime ? date.to_time : date.to_time(:utc)
end
|