Module: DurationRange::CommonFunctions
Instance Attribute Summary collapse
-
#today ⇒ Object
readonly
Returns the value of attribute today.
Instance Method Summary collapse
- #initialize(today: ::Date.today) ⇒ Object
- #last_month(as:, count: 1) ⇒ Object
- #last_week(as:, count: 1) ⇒ Object
- #modes ⇒ Array
- #month_as_date(month, as:) ⇒ Object
-
#months_as_date(*months, as:) ⇒ Object
:reek:NilCheck :reek:TooMenyStatements.
- #next_month(as:, count: 1) ⇒ Object
- #next_week(as:, count: 1) ⇒ Object
- #this_month_as_date(as:, reference_date: today) ⇒ Object
- #this_week(as:, reference_date: today) ⇒ Object
-
#tidy_with_date(as:, begin_date:, end_date:) ⇒ Object
:reek:UtilityFunction :reek:ControlParameter.
Instance Attribute Details
#today ⇒ Object (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
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
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
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 |
#modes ⇒ Array
14 15 16 |
# File 'lib/duration_range/common_functions.rb', line 14 def modes [:array, :hash] end |
#month_as_date(month, as:) ⇒ 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
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
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
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
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
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
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 |