Class: Mallard::Date

Inherits:
Date
  • Object
show all
Defined in:
lib/mallard/date.rb

Overview

Mallard::Date has a fairly simple naming convention: s = start, e = end d = day, w = week, m = month, q = quarter, y = year i = in t = this, l = last, [0-9] = N period ago. stmly and stm1y are the same thing. The only difference is that stmly is explicitly coded while stm1y is handled via method_missing and stmXy generally, methods return a Mallard::Date object. if the time period is spelt out rather than abbreviated (e. g. #lyear), then the method returns the value of just that period rather than a date object also, any method with an ‘i’ in it will return numeric rather than a date

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/mallard/date.rb', line 105

def method_missing(meth, *args, &block)
    if meth.to_s =~ /^s([0-9]+)m$/
        sXm($1.to_i)
    elsif meth.to_s =~ /^e([0-9]+)m$/
        eXm($1.to_i)
    elsif meth.to_s =~ /^s([0-9]+)w$/
        sXw($1.to_i)
    elsif meth.to_s =~ /^e([0-9]+)w$/
        eXw($1.to_i)
    elsif meth.to_s =~ /^di([0-9]+)m$/
        diXm($1.to_i)
    elsif meth.to_s =~ /^year([0-9]+)$/
        yearX($1.to_i)
    elsif meth.to_s =~ /^ditm([0-9]+)y$/
        ditmXy($1.to_i)
    elsif meth.to_s =~ /^stm([0-9]+)y$/
        stmXy($1.to_i)
    elsif meth.to_s =~ /^etm([0-9]+)y$/
        etmXy($1.to_i)
    else
        super
    end
end

Instance Method Details

#conv(meth) ⇒ Object



261
262
263
264
265
266
267
268
269
# File 'lib/mallard/date.rb', line 261

def conv (meth)
    if meth.class == Fixnum
        return self + meth
    elsif meth == meth.to_i.to_s
        return self + meth.to_i
    else
        return self.send(meth.to_sym)
    end
end

#dateObject

returns self



31
32
33
# File 'lib/mallard/date.rb', line 31

def date
    self
end

#dilmObject

days in last month



91
92
93
# File 'lib/mallard/date.rb', line 91

def dilm
    (Mallard::Date.new(year, month) - 1).day
end

#ditmObject

days in this month



16
17
18
# File 'lib/mallard/date.rb', line 16

def ditm    # for the uninitiated:  make a new date on the 1st of the month, add 1 month, subtract 1 day, return the day field
    ((Mallard::Date.new(year, month) >> 1) - 1).day
end

#ditmlyObject

days in this month last year



76
77
78
# File 'lib/mallard/date.rb', line 76

def ditmly
    ((Mallard::Date.new(year - 1, month) >> 1) - 1).day
end

#ditmXy(n) ⇒ Object

days in this month X years ago



184
185
186
# File 'lib/mallard/date.rb', line 184

def ditmXy (n)
    ((Mallard::Date.new(year - n, month) >> 1) - 1).day
end

#diXm(n) ⇒ Object

days in X months ago



174
175
176
# File 'lib/mallard/date.rb', line 174

def diXm (n)
    sXm(n).ditm
end

#elmObject

end last month



101
102
103
# File 'lib/mallard/date.rb', line 101

def elm
    Mallard::Date.new(year, month) - 1
end

#elwObject

end last week



51
52
53
# File 'lib/mallard/date.rb', line 51

def elw
    etw - 7
end

#etmObject

end of this month



26
27
28
# File 'lib/mallard/date.rb', line 26

def etm
    (Mallard::Date.new(year, month) >> 1) - 1
end

#etmlyObject

end this month last year



86
87
88
# File 'lib/mallard/date.rb', line 86

def etmly
    (Mallard::Date.new(year - 1, month) >> 1) - 1
end

#etmXy(n) ⇒ Object

end this month X years ago



194
195
196
# File 'lib/mallard/date.rb', line 194

def etmXy (n)
    (Mallard::Date.new(year - n, month) >> 1) - 1
end

#etqObject

end this quarter



61
62
63
# File 'lib/mallard/date.rb', line 61

def etq
    (Mallard::Date.new(year, quarter * 3) >> 1) - 1
end

#etwObject

end this week



41
42
43
# File 'lib/mallard/date.rb', line 41

def etw
    stw + 6
end

#etwlyObject

end this week last year



244
245
246
# File 'lib/mallard/date.rb', line 244

def etwly
    stwly + 6
end

#eXm(n) ⇒ Object

end X months ago



159
160
161
# File 'lib/mallard/date.rb', line 159

def eXm (n)
    (stm << (n - 1)) - 1
end

#eXw(n) ⇒ Object

end X weeks ago



169
170
171
# File 'lib/mallard/date.rb', line 169

def eXw (n)
    sXw(n) + 6
end

#jan1lyObject

jan 1st last year



204
205
206
# File 'lib/mallard/date.rb', line 204

def jan1ly
    Mallard::Date.new(lyear, 1, 1)
end

#jan1tyObject

jan 1st this year



199
200
201
# File 'lib/mallard/date.rb', line 199

def jan1ty
    Mallard::Date.new(year, 1, 1)
end

#lyearObject

last year



71
72
73
# File 'lib/mallard/date.rb', line 71

def lyear
    year - 1
end

#quarterObject

current quarter



66
67
68
# File 'lib/mallard/date.rb', line 66

def quarter
    (month - 1) / 3 + 1
end

#respond_to?(meth) ⇒ Boolean



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/mallard/date.rb', line 129

def respond_to?(meth)
    if meth.to_s =~ /^s([0-9]+)m$/
        true
    elsif meth.to_s =~ /^e([0-9]+)m$/
        true
    elsif meth.to_s =~ /^s([0-9]+)w$/
        true
    elsif meth.to_s =~ /^e([0-9]+)w$/
        true
    elsif meth.to_s =~ /^di([0-9]+)m$/
        true
    elsif meth.to_s =~ /^year([0-9]+)$/
        true
    elsif meth.to_s =~ /^ditm([0-9]+)y$/
        true
    elsif meth.to_s =~ /^stm([0-9]+)y$/
        true
    elsif meth.to_s =~ /^etm([0-9]+)y$/
        true
    else
        super
    end
end

#slmObject

start last month



96
97
98
# File 'lib/mallard/date.rb', line 96

def slm
    Mallard::Date.new(year, month) << 1
end

#slwObject

start last week



46
47
48
# File 'lib/mallard/date.rb', line 46

def slw
    stw - 7
end

#slyObject

start last year



234
235
236
# File 'lib/mallard/date.rb', line 234

def sly
    Mallard::Date.new(wlyear, 1, 1) + (7 - Mallard::Date.new(wlyear, 1, 1).wday) % 7
end

#stmObject

start of this month



21
22
23
# File 'lib/mallard/date.rb', line 21

def stm
    Mallard::Date.new(year, month)
end

#stmlyObject

start this month last year



81
82
83
# File 'lib/mallard/date.rb', line 81

def stmly
    Mallard::Date.new(year - 1, month)
end

#stmXy(n) ⇒ Object

start this month X years ago



189
190
191
# File 'lib/mallard/date.rb', line 189

def stmXy (n)
    Mallard::Date.new(year - n, month)
end

#stqObject

start this quarter



56
57
58
# File 'lib/mallard/date.rb', line 56

def stq
    Mallard::Date.new(year, (quarter - 1) * 3 + 1)
end

#stwObject

start this week



36
37
38
# File 'lib/mallard/date.rb', line 36

def stw
    self - wday
end

#stwlyObject

start this week last year



239
240
241
# File 'lib/mallard/date.rb', line 239

def stwly
    sly + (twly - 1) * 7
end

#styObject

start this year, from the week perspective, not simply Jan 1st



229
230
231
# File 'lib/mallard/date.rb', line 229

def sty
    Mallard::Date.new(wyear) + (7 - Mallard::Date.new(wyear).wday) % 7
end

#sXm(n) ⇒ Object

start X months ago



154
155
156
# File 'lib/mallard/date.rb', line 154

def sXm (n)
    slm << (n - 1)
end

#sXw(n) ⇒ Object

start X weeks ago



164
165
166
# File 'lib/mallard/date.rb', line 164

def sXw (n)
    stw - n * 7
end

#tdlyObject

this day last year



249
250
251
# File 'lib/mallard/date.rb', line 249

def tdly
    stwly + wday
end

#to_strObject



253
254
255
# File 'lib/mallard/date.rb', line 253

def to_str
    strftime('%F')
end

#todayObject



257
258
259
# File 'lib/mallard/date.rb', line 257

def today
    Mallard::Date.today
end

#twlyObject

this week last year. Same as week unless we’re in the last week of a 53 week year, in which case it’s 52



224
225
226
# File 'lib/mallard/date.rb', line 224

def twly
    week == 53 ? 52 : week
end

#weekObject

current week number



219
220
221
# File 'lib/mallard/date.rb', line 219

def week
    ((stw - sty) / 7).to_i + 1  # added the to_i to eliminate some weirdness when converting to string
end

#wlyearObject

the week year for last year. See #wyear



214
215
216
# File 'lib/mallard/date.rb', line 214

def wlyear
    stw.year - 1
end

#wyearObject

the ‘week’ year, or in another words what year said week is week number X in. Will only be different from year in early January



209
210
211
# File 'lib/mallard/date.rb', line 209

def wyear
    stw.year
end

#yearX(n) ⇒ Object

X years ago



179
180
181
# File 'lib/mallard/date.rb', line 179

def yearX (n)
    year - n
end