class Koyomi::Week

Constants

DAYS
DEFAULT_START
START_RANGE
WDAYS

Attributes

start_wday[R]

Public Class Methods

ends?(date, start_wday = DEFAULT_START) click to toggle source

week end?

@param [Date] date @param [Object] start_wday week start @return [Boolean]

# File lib/koyomi/week.rb, line 63
def self.ends?(date, start_wday = DEFAULT_START)
  (date + 1).wday == self.windex(start_wday)
end
new(date = nil, start_wday = DEFAULT_START) click to toggle source

initialize method

@param [Date] date optional, default use Date.today. @param [Object] start_wday optionail, default use Koyomi::Week::DEFAULT_START

Calls superclass method Koyomi::Period::new
# File lib/koyomi/week.rb, line 74
def initialize(date = nil, start_wday = DEFAULT_START)
  super()
  @start_wday = start_wday
  setup_range(date||created_at)
end
starts?(date, start_wday = DEFAULT_START) click to toggle source

week starts?

@param [Date] date @param [Object] start_wday week start @return [Boolean]

# File lib/koyomi/week.rb, line 54
def self.starts?(date, start_wday = DEFAULT_START)
  (date).wday == self.windex(start_wday)
end
wday_name(value) click to toggle source

week day name

@param [Object] value @return [Symbol]

# File lib/koyomi/week.rb, line 45
def self.wday_name(value)
  WDAYS.at(self.windex(value))
end
windex(value) click to toggle source

week index

@param [Object] value @return [Integer]

# 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

Public Instance Methods

wday(wday_name) click to toggle source

sepified week day

@param [Object] wday_name @return [Date]

# File lib/koyomi/week.rb, line 84
def wday(wday_name)
  diff = self.class.windex(wday_name) - self.class.windex(self.start_wday)
  factor = diff + ((diff < 0) ? DAYS : 0)
  first + factor
end

Private Instance Methods

method_missing(name, *args, &block) click to toggle source
Calls superclass method Koyomi::Period#method_missing
# File lib/koyomi/week.rb, line 108
def method_missing(name, *args, &block)
  case
  when WDAYS.include?(name.to_s.to_sym)
    self.wday(name,*args, &block)
  else
    super
  end
end
setup_range(date) click to toggle source

setup week range with given week start

# File lib/koyomi/week.rb, line 102
def setup_range(date)
  diff = date.wday - self.class.windex(start_wday)
  @first  = date - (diff + ((diff < 0) ? DAYS : 0))
  @last   = @first + DAYS - 1
end