Class: Datebox::Relative
- Inherits:
-
Object
- Object
- Datebox::Relative
- Defined in:
- lib/datebox/relative.rb
Instance Attribute Summary collapse
-
#period_name ⇒ Object
readonly
Returns the value of attribute period_name.
Class Method Summary collapse
- .day_before ⇒ Object
- .last(period, options = {}) ⇒ Object
- .last_month ⇒ Object
- .last_n_days(options = {}) ⇒ Object
- .last_week(options = {}) ⇒ 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_date(period, options = {}) ⇒ Object
- .week_to_date(options = {}) ⇒ Object
- .year_to_date ⇒ Object
Instance Method Summary collapse
-
#initialize(proc, name = nil) ⇒ Relative
constructor
A new instance of Relative.
- #to(relative_to) ⇒ Object
Constructor Details
#initialize(proc, name = nil) ⇒ Relative
Returns a new instance of Relative.
7 8 9 10 |
# File 'lib/datebox/relative.rb', line 7 def initialize(proc, name = nil) @period_proc = proc @period_name = name end |
Instance Attribute Details
#period_name ⇒ Object (readonly)
Returns the value of attribute period_name.
5 6 7 |
# File 'lib/datebox/relative.rb', line 5 def period_name @period_name end |
Class Method Details
.day_before ⇒ Object
44 45 46 |
# File 'lib/datebox/relative.rb', line 44 def day_before new Proc.new {|relative_to| Period.new(relative_to - 1, relative_to - 1) }, __method__.to_s end |
.last(period, options = {}) ⇒ Object
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/datebox/relative.rb', line 19 def last(period, = {}) raise "Expected one of: #{Period::PREDEFINED}" unless Period::PREDEFINED.include?(period.to_sym) case period.to_sym when :day then day_before when :n_days then last_n_days() when :week then last_week() when :month then last_month when :year then last_year end end |
.last_month ⇒ Object
94 95 96 97 98 99 100 |
# File 'lib/datebox/relative.rb', line 94 def last_month new Proc.new { |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) }, __method__.to_s end |
.last_n_days(options = {}) ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/datebox/relative.rb', line 48 def last_n_days( = {}) days = ([:days] || ['days']).to_i inclusive = ([:exclusive] || ['exclusive']) ? false : true # inclusive by default days = 1 if days.nil? || days <= 0 # days should always > 0 since it only return last x days proc = inclusive ? Proc.new {|relative_to| Period.new(relative_to - days + 1, relative_to) } : Proc.new {|relative_to| Period.new(relative_to - days, relative_to - 1) } new proc, __method__.to_s end |
.last_week(options = {}) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/datebox/relative.rb', line 58 def last_week( = {}) last_weekday = [:last_weekday] || ['last_weekday'] || 'Sunday' new Proc.new { |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) }, __method__.to_s end |
.last_weekdays_between(start_day, end_day) ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/datebox/relative.rb', line 66 def last_weekdays_between(start_day, end_day) new Proc.new { |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) }, __method__.to_s end |
.last_weeks_weekdays_as!(*days) ⇒ Object
this one returns array!
74 75 76 77 78 79 80 |
# File 'lib/datebox/relative.rb', line 74 def last_weeks_weekdays_as!(*days) #this one returns array! new Proc.new { |relative_to| days.map do |p| (relative_to.downto relative_to - 6).to_a.find { |d| d.strftime("%A") == p } end }, __method__.to_s end |
.last_year ⇒ Object
109 110 111 112 113 114 115 |
# File 'lib/datebox/relative.rb', line 109 def last_year new Proc.new { |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) }, __method__.to_s end |
.month_to_date ⇒ Object
102 103 104 105 106 107 |
# File 'lib/datebox/relative.rb', line 102 def month_to_date new Proc.new { |relative_to| month_start = Date.parse("#{relative_to.strftime('%Y-%m')}-01") Period.new(month_start, relative_to) }, __method__.to_s end |
.same_day ⇒ Object
40 41 42 |
# File 'lib/datebox/relative.rb', line 40 def same_day new Proc.new {|relative_to| Period.new(relative_to, relative_to) }, __method__.to_s end |
.to_date(period, options = {}) ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/datebox/relative.rb', line 30 def to_date(period, = {}) raise "Expected one of: #{Period::PREDEFINED}" unless Period::PREDEFINED.include?(period.to_sym) raise "Current doesn't make sense for parameter: #{period}" if [:day, :n_days].include?(period) case period.to_sym when :week then week_to_date() when :month then month_to_date when :year then year_to_date end end |
.week_to_date(options = {}) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/datebox/relative.rb', line 82 def week_to_date( = {}) last_weekday = [:last_weekday] || ['last_weekday'] || 'Sunday' new Proc.new { |relative_to| if relative_to.strftime("%A") == last_weekday Period.new(relative_to - 6, relative_to) else end_date = (relative_to.downto relative_to - 6).to_a.find { |d| d.strftime("%A") == last_weekday } Period.new(end_date + 1, relative_to) end }, __method__.to_s end |
.year_to_date ⇒ Object
117 118 119 120 121 122 |
# File 'lib/datebox/relative.rb', line 117 def year_to_date new Proc.new { |relative_to| year_start = Date.parse("#{relative_to.year}-01-01") Period.new(year_start, relative_to) }, __method__.to_s end |
Instance Method Details
#to(relative_to) ⇒ Object
12 13 14 15 |
# File 'lib/datebox/relative.rb', line 12 def to(relative_to) relative_to = (relative_to.is_a?(Date) ? relative_to : Date.parse(relative_to)) @period_proc.call relative_to end |