Class: DurationRange::Time

Inherits:
Object
  • Object
show all
Includes:
CommonFunctions
Defined in:
lib/duration_range/time.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CommonFunctions

#modes, #month_as_date, #months_as_date, #this_month_as_date, #tidy_with_date

Constructor Details

#initialize(today: ::Date.today, with_localtime: false) ⇒ Time

:reek:BooleanParameter

Parameters:

  • today (Date) (defaults to: ::Date.today)
  • localtime (boolish)


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

#todayObject (readonly)

Returns the value of attribute today.



18
19
20
# File 'lib/duration_range/time.rb', line 18

def today
  @today
end

#with_localtimeObject (readonly)

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

Parameters:

  • as (Symbol) (defaults to: :hash)
  • count (Integer) (defaults to: 1)

Returns:

  • (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

Parameters:

  • as (Symbol) (defaults to: :hash)
  • count (Integer) (defaults to: 1)

Returns:

  • (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

Parameters:

  • month (String)
  • as (Symbol) (defaults to: :hash)

Returns:

  • (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

Parameters:

  • months (Array)
  • as (Symbol) (defaults to: :hash)

Returns:

  • (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

Parameters:

  • as (Symbol) (defaults to: :hash)
  • count (Integer) (defaults to: 1)

Returns:

  • (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

Parameters:

  • as (Symbol) (defaults to: :hash)
  • count (Integer) (defaults to: 1)

Returns:

  • (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

Parameters:

  • as (Symbol) (defaults to: :hash)
  • reference_date (Date) (defaults to: today)

Returns:

  • (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

Parameters:

  • as (Symbol) (defaults to: :hash)
  • reference_date (Date) (defaults to: today)

Returns:

  • (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

Parameters:

  • as (Symbol)
  • begin_date (Date)
  • end_date (Date)

Returns:

  • (Object)


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

:reek:FeatureEnvy

Parameters:

Returns:



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