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_apart(difference) ⇒ Object
- .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
- .method_missing(m, *args, &block) ⇒ Object
- .month_to_date ⇒ Object
- .same_day ⇒ Object
- .same_month ⇒ Object
- .same_week(options = {}) ⇒ 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_apart(difference) ⇒ Object
48 49 50 |
# File 'lib/datebox/relative.rb', line 48 def day_apart(difference) new Proc.new {|relative_to| Period.new(relative_to + difference, relative_to + difference) }, __method__.to_s end |
.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
126 127 128 129 130 131 132 |
# File 'lib/datebox/relative.rb', line 126 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
67 68 69 70 71 72 73 74 75 |
# File 'lib/datebox/relative.rb', line 67 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
77 78 79 80 81 82 83 84 |
# File 'lib/datebox/relative.rb', line 77 def last_week( = {}) last_weekday = [:last_weekday] || ['last_weekday'] || 'Sunday' new Proc.new { |relative_to| relative_to -= 1 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
86 87 88 89 90 91 92 |
# File 'lib/datebox/relative.rb', line 86 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!
94 95 96 97 98 99 100 |
# File 'lib/datebox/relative.rb', line 94 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
149 150 151 152 153 154 155 |
# File 'lib/datebox/relative.rb', line 149 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 |
.method_missing(m, *args, &block) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/datebox/relative.rb', line 52 def method_missing(m, *args, &block) if (m.to_s =~ /^day_ago_\d+$/) or (m.to_s =~ /^day_in_\d+$/) days = m.to_s.split('_').last.to_i if m.to_s =~ /^day_ago_\d+$/ return new Proc.new {|relative_to| Period.new(relative_to - days, relative_to - days) }, m elsif m.to_s =~ /^day_in_\d+$/ return new Proc.new {|relative_to| Period.new(relative_to + days, relative_to + days) }, m end relative.send(:period_name, m) return relative else super end end |
.month_to_date ⇒ Object
134 135 136 137 138 139 |
# File 'lib/datebox/relative.rb', line 134 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 |
.same_month ⇒ Object
141 142 143 144 145 146 147 |
# File 'lib/datebox/relative.rb', line 141 def same_month new Proc.new { |relative_to| same_month_start = Date.parse("#{relative_to.strftime('%Y-%m')}-01") same_month_end = same_month_start.next_month - 1 Period.new(same_month_start, same_month_end) }, __method__.to_s end |
.same_week(options = {}) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/datebox/relative.rb', line 114 def same_week( = {}) 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, end_date + 7) end }, __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
102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/datebox/relative.rb', line 102 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
157 158 159 160 161 162 |
# File 'lib/datebox/relative.rb', line 157 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 |