Class: Suprdate::Month

Inherits:
Unit
  • Object
show all
Defined in:
lib/suprdate/month.rb

Constant Summary collapse

STRFTIME_STR =
'%Y-%m'

Instance Attribute Summary collapse

Attributes inherited from Unit

#value

Instance Method Summary collapse

Methods inherited from Unit

<=>, #==, #hash, #new, significance, #to_s

Methods included from Utility::CleanConstantName

#name_plural, #name_singular

Instance Attribute Details

#day_factoryObject

Returns the value of attribute day_factory.



5
6
7
# File 'lib/suprdate/month.rb', line 5

def day_factory
  @day_factory
end

#yearObject (readonly)

Returns the value of attribute year.



6
7
8
# File 'lib/suprdate/month.rb', line 6

def year
  @year
end

Instance Method Details

#+(increase) ⇒ Object

Return a new month incremented by an integer.



67
# File 'lib/suprdate/month.rb', line 67

def +(increase) new_from_sum(sum + increase) end

#-(decrease) ⇒ Object

Return a new month decremented by an integer.



70
# File 'lib/suprdate/month.rb', line 70

def -(decrease) new_from_sum(sum - decrease) end

#<=>(operand) ⇒ Object



54
55
56
57
58
# File 'lib/suprdate/month.rb', line 54

def <=>(operand)
  return -1 if operand == Infinity
  operand = operand.month
  (@year.value * NUM_MONTHS_IN_YEAR + @value) - (operand.year.value * NUM_MONTHS_IN_YEAR + operand.value)
end

#day(*indices) ⇒ Object Also known as: []

A choice of some specific days from this month.



40
41
42
43
44
45
46
47
# File 'lib/suprdate/month.rb', line 40

def day(*indices)
  indices = [1] if indices.empty?
  rval = indices.map do |i|
    i = num_days + 1 - i.abs if i < 0
    day_factory.new(self, i)
  end
  Utility::disarray(rval)
end

#daysObject

All the days in this month



35
36
37
# File 'lib/suprdate/month.rb', line 35

def days
  (1..num_days).to_a.map { |i| day_factory.new(self, i) }
end

#inspectObject



52
# File 'lib/suprdate/month.rb', line 52

def inspect() "#@year-#{@value.to_s.rjust(2, '0')}" end

#leap?Boolean

Whether this month contains a leap day.

Returns:

  • (Boolean)


50
# File 'lib/suprdate/month.rb', line 50

def leap?() @value == 2 && @year.leap? end

#monthObject



77
# File 'lib/suprdate/month.rb', line 77

def month() self end

#num_daysObject

Number of day in this month.



29
30
31
32
# File 'lib/suprdate/month.rb', line 29

def num_days
  return 29 if leap?
  NUM_DAYS_IN_MONTHS[@value]
end

#of_year_as_sObject



76
# File 'lib/suprdate/month.rb', line 76

def of_year_as_s() MONTHS_AS_STR[@value] end

#of_year_as_symObject



75
# File 'lib/suprdate/month.rb', line 75

def of_year_as_sym() MONTHS_AS_SYM[@value] end

#since(operand) ⇒ Object

Number of months since parameter#month.



61
# File 'lib/suprdate/month.rb', line 61

def since(operand) sum - operand.month.sum end

#succObject

Next successive month.



73
# File 'lib/suprdate/month.rb', line 73

def succ() self + 1 end

#to_symObject

Name of month as symbol.



26
# File 'lib/suprdate/month.rb', line 26

def to_sym() MONTHS_AS_SYM[@value] end

#until(operand) ⇒ Object

Number of months until parameter#month.



64
# File 'lib/suprdate/month.rb', line 64

def until(operand) operand.month.sum - sum end