Module: DurationRange::CommonFunctions

Included in:
Date, Time
Defined in:
lib/duration_range/common_functions.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#todayObject (readonly)

Returns the value of attribute today.



9
10
11
# File 'lib/duration_range/common_functions.rb', line 9

def today
  @today
end

Instance Method Details

#initialize(today: ::Date.today) ⇒ Object

Parameters:

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


6
7
8
# File 'lib/duration_range/common_functions.rb', line 6

def initialize(today: ::Date.today)
  @today = today
end

#last_month(as:, count: 1) ⇒ Object

Parameters:

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

Returns:

  • (Object)


94
95
96
# File 'lib/duration_range/common_functions.rb', line 94

def last_month(as:, count: 1)
  this_month(as: as, reference_date: today.months_ago(count))
end

#last_week(as:, count: 1) ⇒ Object

Parameters:

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

Returns:

  • (Object)


124
125
126
# File 'lib/duration_range/common_functions.rb', line 124

def last_week(as:, count: 1)
  this_week(as: as, reference_date: today.weeks_ago(count))
end

#modesArray

Returns:

  • (Array)


14
15
16
# File 'lib/duration_range/common_functions.rb', line 14

def modes
  [:array, :hash]
end

#month_as_date(month, as:) ⇒ Object

Parameters:

  • month (String)
  • as (Symbol)

Returns:

  • (Object)


39
40
41
# File 'lib/duration_range/common_functions.rb', line 39

def month_as_date(month, as:)
  this_month_as_date(as: as, reference_date: ::Date.parse(month + '-01'))
end

#months_as_date(*months, as:) ⇒ Object

:reek:NilCheck :reek:TooMenyStatements

Parameters:

  • months (Array)
  • as (Symbol)

Returns:

  • (Object)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/duration_range/common_functions.rb', line 49

def months_as_date(*months, as:)
  min = nil
  max = nil
  months.each { |mon|
    dates = month_as_date(mon, as: :hash)
    begin_date = dates[:begin]
    end_date = dates[:end]

    if min.nil? || begin_date < min
      min = begin_date
    end
    if max.nil? || max < end_date
      max = end_date
    end
  }

  tidy_with_date(as: as, begin_date: min, end_date: max)
end

#next_month(as:, count: 1) ⇒ Object

Parameters:

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

Returns:

  • (Object)


85
86
87
# File 'lib/duration_range/common_functions.rb', line 85

def next_month(as:, count: 1)
  this_month(as: as, reference_date: today.months_since(count))
end

#next_week(as:, count: 1) ⇒ Object

Parameters:

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

Returns:

  • (Object)


115
116
117
# File 'lib/duration_range/common_functions.rb', line 115

def next_week(as:, count: 1)
  this_week(as: as, reference_date: today.weeks_since(count))
end

#this_month_as_date(as:, reference_date: today) ⇒ Object

Parameters:

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

Returns:

  • (Object)


73
74
75
76
77
78
# File 'lib/duration_range/common_functions.rb', line 73

def this_month_as_date(as:, reference_date: today)
  begin_date = reference_date.at_beginning_of_month
  end_date = reference_date.at_end_of_month

  tidy_with_date(as: as, begin_date: begin_date, end_date: end_date)
end

#this_week(as:, reference_date: today) ⇒ Object

Parameters:

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

Returns:

  • (Object)


103
104
105
106
107
108
# File 'lib/duration_range/common_functions.rb', line 103

def this_week(as:, reference_date: today)
  begin_date = reference_date.beginning_of_week
  end_date = reference_date.end_of_week

  tidy_with_date(as: as, begin_date: begin_date, end_date: end_date)
end

#tidy_with_date(as:, begin_date:, end_date:) ⇒ Object

:reek:UtilityFunction :reek:ControlParameter

Parameters:

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

Returns:

  • (Object)


25
26
27
28
29
30
31
32
# File 'lib/duration_range/common_functions.rb', line 25

def tidy_with_date(as:, begin_date:, end_date:)
  case as
  when :array
    (begin_date..end_date).to_a
  when :hash
    { begin: begin_date, end: end_date }
  end
end