Class: Koyomi::Week
Constant Summary collapse
- DEFAULT_START =
——————–# constants
:mon
- WDAYS =
[:sun, :mon, :tue, :wed, :thu, :fri, :sat]
- DAYS =
WDAYS.size
- START_RANGE =
(0..(DAYS - 1))
Instance Attribute Summary
Attributes inherited from Period
Class Method Summary collapse
-
.ends?(date, start_wday = nil) ⇒ Boolean
week end?.
-
.starts?(date, start_wday = nil) ⇒ Boolean
week starts?.
-
.wday_name(value) ⇒ Symbol
week day name.
-
.windex(value) ⇒ Integer
week index.
Instance Method Summary collapse
-
#ends ⇒ Date
end date.
-
#initialize(date = nil, start_wday = nil) ⇒ Week
constructor
initialize method.
-
#starts ⇒ Date
start date.
-
#wday(wday_name) ⇒ Date
sepified week day.
Constructor Details
#initialize(date = nil, start_wday = nil) ⇒ Week
initialize method
76 77 78 79 80 |
# File 'lib/koyomi/week.rb', line 76 def initialize(date = nil, start_wday = nil) super() self.date = date||self.created_at self.start_wday = start_wday||DEFAULT_START end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object (private)
131 132 133 134 135 136 137 138 |
# File 'lib/koyomi/week.rb', line 131 def method_missing(name, *args, &block) case when WDAYS.include?(name.to_s.to_sym) self.wday(name,*args, &block) else super end end |
Class Method Details
.ends?(date, start_wday = nil) ⇒ Boolean
week end?
64 65 66 67 |
# File 'lib/koyomi/week.rb', line 64 def self.ends?(date, start_wday = nil) start_wday ||= DEFAULT_START (date + 1).wday == self.windex(start_wday) end |
.starts?(date, start_wday = nil) ⇒ Boolean
week starts?
54 55 56 57 |
# File 'lib/koyomi/week.rb', line 54 def self.starts?(date, start_wday = nil) start_wday ||= DEFAULT_START (date).wday == self.windex(start_wday) end |
.wday_name(value) ⇒ Symbol
week day name
45 46 47 |
# File 'lib/koyomi/week.rb', line 45 def self.wday_name(value) WDAYS.at(self.windex(value)) end |
.windex(value) ⇒ Integer
week index
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/koyomi/week.rb', line 24 def self.windex(value) case value when Numeric index = value when Date index = value.wday when String, Symbol value = value.to_s.downcase[0, 3].to_sym raise "Range invalid, required #{WDAYS}." unless WDAYS.include?(value) index = WDAYS.index(value) else index = value.to_s.to_i end raise "Range overflow, required (#{START_RANGE})." unless START_RANGE.cover?(index) index end |