Class: SycTimeleap::TimeLeap

Inherits:
Object
  • Object
show all
Defined in:
lib/syctimeleap/time_leap.rb

Overview

Base class for date operations

Constant Summary collapse

WEEKDAYS =

Holds the weekdays of the week starts with sunday at index 0

{ "su" => [0, "sunday"], 
"mo" => [1, "monday"], 
"tu" => [2, "tuesday"],
"we" => [3, "wednesday"], 
"th" => [4, "thursday"], 
"fr" => [5, "friday"],
"sa" => [6, "saturday"] }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(date = Time.now.to_date) ⇒ TimeLeap

Creates a new Temp and initializes it with the current date if no date is provided



23
24
25
# File 'lib/syctimeleap/time_leap.rb', line 23

def initialize(date = Time.now.to_date)
  @now = date
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

Provides the date calculation methods dynamically



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/syctimeleap/time_leap.rb', line 28

def method_missing(name, *args)
  add_regex = %r{
    ^([ib])(?:n|ack)?
    (?:\.|_|-| )?
    (\d+)
    (?:\.|_|-| )?
    ([dwmy])
    (?:ays?|eeks?|onths?|ears?)?$
  }x

  weekday_regex = %r{
    ^(tod|tom|y)(?:a?y?|o?r?r?o?w?|e?s?t?e?r?d?a?y?)?$
  }xi
  
  next_weekday_regex = %r{
    ^(n|p)(?:e?x?t|r?e?v?i?o?u?s?)?
    (?:\.|_| |-)?
    (mo|tu|we|th|fr|sa|su)
    (?:n?|e?s?|d?n?e?s?|u?r?s?|i?|t?u?r?|n?)(?:d?a?y?)$ 
  }xi

  next_weekday_in_regex = %r{
    ^(mo|tu|we|th|fr|sa|su)
    (?:n?|e?s?|d?n?e?s?|u?r?s?|i?|t?u?r?|n?)(?:d?a?y?)(?:_?)
    (i|b)
    (?:n?|a?c?k?)(?:_?)
    (\d+)(?:_?)([dwmy])(?:a?y?s?|e?e?k?s?|o?n?t?h?s?|e?a?r?s?)$
  }xi

  return add($1, $2, $3)      if name =~ add_regex
  return weekday($1)          if name =~ weekday_regex
  return next_weekday($1, $2) if name =~ next_weekday_regex
  return next_weekday_in($1, $2, $3, $4) if name =~ next_weekday_in_regex
  super 
end

Instance Attribute Details

#nowObject

The base date that is used to calculate date operations



19
20
21
# File 'lib/syctimeleap/time_leap.rb', line 19

def now
  @now
end

Instance Method Details

#add(direction, count, distance) ⇒ Object

Determines whether to move back or forward in time and returns the distance in days, weeks, months or years Example: move(10, ‘d’) adds 10 days to :now



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/syctimeleap/time_leap.rb', line 67

def add(direction, count, distance)
  count = direction.downcase == 'b' ? -count.to_i : count.to_i

  case distance
  when 'd'
    @now + count
  when 'w'
    @now + count * 7
  when 'm'
    @now >> count
  when 'y'
    @now >> count * 12
  end

end

#next_weekday(direction, abbreviated_weekday) ⇒ Object

Returns the next or previous weekday Exampel: next_monday or n_mo Su Mo Tu We Th Fr Sa

0  1  2  3  4  5  6
   x                 Today
         *           Next Wednesday
                     @now + (3 - 1 + 7) % 7 = +2
            x        Today 
*                    Next Sunday
                     @now + (0 - 4 + 7) % 7 = +3
      x              Today
         *           Previous Wednesday
                     @now + (3 - 2 + 7) % -7 = -6
             x       Today
      *              Previous Tuesday
                     @now + (2 - 4 + 7) % -7 = -2


111
112
113
114
115
116
117
118
119
# File 'lib/syctimeleap/time_leap.rb', line 111

def next_weekday(direction, abbreviated_weekday)
  count = direction.downcase == 'n' ? 1 : -1

  weekday = WEEKDAYS[abbreviated_weekday.downcase]
  raise "No valid weekday: #{abbreviated_weekday}" if weekday.nil?

  offset = @now.send("#{weekday[1]}?") ? count * 7 : 0
  @now + (weekday[0] - @now.wday + 7) % (count * 7) + offset
end

#next_weekday_in(weekday_abbreviation, direction, count, distance) ⇒ Object

Returns the next weekday in n days, weeks, month or years Example: monday_in_3_weeks or mo_i_3_w



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/syctimeleap/time_leap.rb', line 123

def next_weekday_in(weekday_abbreviation, direction, count, distance)
  count = direction.downcase == 'b' ? -count.to_i : count.to_i
  weekday = WEEKDAYS[weekday_abbreviation.downcase]

  case distance.downcase
  when "d"
  when "w"
    (@now + count * 7) + weekday[0] - @now.wday  
  when "m"
    future = @now >> count
    future + weekday[0] - future.wday
  when "y"
    future = @now >> count * 12
    future + weekday[0] - future.wday
  end
end

#weekday(weekday) ⇒ Object

Returns yesterday, today or tomorrow



84
85
86
87
88
89
90
91
92
93
# File 'lib/syctimeleap/time_leap.rb', line 84

def weekday(weekday)
  case weekday
  when 'tod'
    @now
  when 'tom'
    @now + 1
  when 'y'
    @now - 1
  end
end