Class: Datebox::Relative
- Inherits:
-
Object
- Object
- Datebox::Relative
- Defined in:
- lib/datebox/relative.rb
Instance Method Summary collapse
- #day_before ⇒ Object
- #last(period) ⇒ Object
- #last_month ⇒ Object
- #last_week(last_weekday = "Sunday") ⇒ Object
- #last_weekdays_between(start_day, end_day) ⇒ Object
-
#last_weeks_weekdays_as!(*days) ⇒ Object
this one returns array!.
- #last_year ⇒ Object
- #month_to_date ⇒ Object
- #same_day ⇒ Object
- #to(relative_to) ⇒ Object
- #year_to_date ⇒ Object
Instance Method Details
#day_before ⇒ Object
30 31 32 33 |
# File 'lib/datebox/relative.rb', line 30 def day_before @period_proc = Proc.new {|relative_to| Period.new(relative_to - 1, relative_to - 1) } self end |
#last(period) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/datebox/relative.rb', line 12 def last(period) periods = [:day, :week, :week_ms, :week_ss, :month, :year] # week monday-sunday & week sunday-saturday raise "Expected one of: #{periods}" unless periods.include?(period) case period when :day then day_before when :week then last_week when :week_ms then last_week when :week_ss then last_week("Saturday") when :month then last_month when :year then last_year end end |
#last_month ⇒ Object
61 62 63 64 65 66 67 68 |
# File 'lib/datebox/relative.rb', line 61 def last_month @period_proc = Proc.new do |relative_to| previous_month_start = Date.parse("#{relative_to.prev_month.strftime('%Y-%m')}-01") previous_month_end = previous_month_start.next_month - 1 Period.new(previous_month_start, previous_month_end) end self end |
#last_week(last_weekday = "Sunday") ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/datebox/relative.rb', line 35 def last_week(last_weekday = "Sunday") @period_proc = Proc.new do |relative_to| end_date = (relative_to.downto relative_to - 6).to_a.find { |d| d.strftime("%A") == last_weekday } Period.new(end_date - 6, end_date) end self end |
#last_weekdays_between(start_day, end_day) ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/datebox/relative.rb', line 43 def last_weekdays_between(start_day, end_day) @period_proc = Proc.new do |relative_to| end_date = (relative_to.downto relative_to - 6).to_a.find { |d| d.strftime("%A") == end_day } start_date = (end_date - 7 .. end_date).to_a.find { |d| d.strftime("%A") == start_day } Period.new(start_date, end_date) end self end |
#last_weeks_weekdays_as!(*days) ⇒ Object
this one returns array!
52 53 54 55 56 57 58 59 |
# File 'lib/datebox/relative.rb', line 52 def last_weeks_weekdays_as!(*days) #this one returns array! @period_proc = Proc.new do |relative_to| days.map do |p| (relative_to.downto relative_to - 6).to_a.find { |d| d.strftime("%A") == p } end end self end |
#last_year ⇒ Object
78 79 80 81 82 83 84 85 |
# File 'lib/datebox/relative.rb', line 78 def last_year @period_proc = Proc.new do |relative_to| previous_year_start = Date.parse("#{relative_to.prev_year.year}-01-01") previous_year_end = previous_year_start.next_year - 1 Period.new(previous_year_start, previous_year_end) end self end |
#month_to_date ⇒ Object
70 71 72 73 74 75 76 |
# File 'lib/datebox/relative.rb', line 70 def month_to_date @period_proc = Proc.new do |relative_to| month_start = Date.parse("#{relative_to.strftime('%Y-%m')}-01") Period.new(month_start, relative_to) end self end |
#same_day ⇒ Object
25 26 27 28 |
# File 'lib/datebox/relative.rb', line 25 def same_day @period_proc = Proc.new {|relative_to| Period.new(relative_to, relative_to) } self end |
#to(relative_to) ⇒ Object
7 8 9 10 |
# File 'lib/datebox/relative.rb', line 7 def to(relative_to) relative_to = (relative_to.is_a?(Date) ? relative_to : Date.parse(relative_to)) @period_proc.call relative_to end |
#year_to_date ⇒ Object
87 88 89 90 91 92 93 |
# File 'lib/datebox/relative.rb', line 87 def year_to_date @period_proc = Proc.new do |relative_to| year_start = Date.parse("#{relative_to.year}-01-01") Period.new(year_start, relative_to) end self end |