Class: Month

Inherits:
Object
  • Object
show all
Includes:
Totals
Defined in:
lib/month.rb

Constant Summary collapse

NEWLINE =
"\r\n"
NAMES =
{
  1  => 'januar',
  2  => 'februar',
  3  => 'maerz',
  4  => 'april',
  5  => 'mai',
  6  => 'juni',
  7  => 'juli',
  8  => 'august',
  9  => 'september',
  10 => 'oktober',
  11 => 'november',
  12 => 'dezember',
}

Constants included from Totals

Totals::FORMATTERS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Totals

format, #total, #total_str

Constructor Details

#initialize(name) ⇒ Month

Returns a new instance of Month.


36
37
38
39
# File 'lib/month.rb', line 36

def initialize(name)
  @name = name
  @days = []
end

Instance Attribute Details

#daysObject

Returns the value of attribute days


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

def days
  @days
end

#nameObject

Returns the value of attribute name


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

def name
  @name
end

#numberObject Also known as: month

Returns the value of attribute number


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

def number
  @number
end

#yearObject

Returns the value of attribute year


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

def year
  @year
end

Class Method Details

.from(brf_str, month_nr, year) ⇒ Object


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

def self.from(brf_str, month_nr, year)
  month        = BRFParser.new.parse(brf_str)
  month.number = month_nr
  month.year   = year
  month
end

.name(month_nr) ⇒ Object


25
26
27
# File 'lib/month.rb', line 25

def self.name(month_nr)
  NAMES[month_nr]
end

Instance Method Details

#add(*new_days) ⇒ Object


55
56
57
58
59
60
61
62
63
# File 'lib/month.rb', line 55

def add(*new_days)
  new_days.each do |day|
    if (existing = days.find { |d| d.date == day.date })
      existing.add(*day.blocks)
    else
      days << day
    end
  end
end

#blocksObject


142
143
144
# File 'lib/month.rb', line 142

def blocks
  days.flat_map(&:blocks)
end

#childrenObject


138
139
140
# File 'lib/month.rb', line 138

def children
  days
end

#cleanup!(options = {}) ⇒ Object


154
155
156
157
# File 'lib/month.rb', line 154

def cleanup!(options = {})
  days.each(&:remove_ongoing_blocks!) if options[:remove_ongoing_blocks]
  days.reject!(&:empty?)
end

#fancyObject


118
119
120
# File 'lib/month.rb', line 118

def fancy
  to_s :fancy => true
end

#find_or_create_day_by_date(date) ⇒ Object


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/month.rb', line 83

def find_or_create_day_by_date(date)
  day_nr, month_nr, year_nr = date.split('.')

  day = days.find do |d|
    (day_nr   ? d.day?(day_nr)     : true) &&
    (month_nr ? d.month?(month_nr) : true) &&
    (year_nr  ? d.year?(year_nr)   : true)
  end

  if day.nil?
    day = DateParser.parse(date, self)
    add day
  else
    day
  end

  day
end

#fullObject


122
123
124
125
126
127
128
# File 'lib/month.rb', line 122

def full
  full_month.to_s(
    :fancy => true,
    :prepend_name => true,
    :group_weeks => Punch.config.group_weeks_in_interactive_mode
  )
end

#full_monthObject


130
131
132
# File 'lib/month.rb', line 130

def full_month
  @full_month ||= FullMonth.new(self).full_month
end

#max_block_countObject


146
147
148
# File 'lib/month.rb', line 146

def max_block_count
  days.map(&:block_count).max
end

#month_yearObject


102
103
104
# File 'lib/month.rb', line 102

def month_year
  @month_year ||= MonthYear.new(:month => number, :year => year)
end

#newlineObject


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

def newline
  NEWLINE
end

#next_month_yearObject


110
111
112
# File 'lib/month.rb', line 110

def next_month_year
  month_year.next
end

#prev_month_yearObject


106
107
108
# File 'lib/month.rb', line 106

def prev_month_year
  month_year.prev
end

#short_yearObject


150
151
152
# File 'lib/month.rb', line 150

def short_year
  year - 2000
end

#to_dateObject


114
115
116
# File 'lib/month.rb', line 114

def to_date
  month_year.date
end

#to_s(options = {}) ⇒ Object


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

def to_s(options = {})
  days.sort!
  b_count = max_block_count
  day_options = options.merge :padding => b_count
  days_str = days.map do |d|
    day_str = d.to_s(day_options)

    if (days.first != d) && options[:group_weeks] && d.monday?
      day_str.prepend("\n")
    end

    day_str
  end.join(newline)

  "#{name}#{newline * 2}#{days_str}#{newline * 2}" \
    "Total: #{total_str}#{newline}"
end

#workdaysObject


134
135
136
# File 'lib/month.rb', line 134

def workdays
  full_month.days.select(&:workday?)
end