Class: Day

Inherits:
Object
  • Object
show all
Includes:
Attributes, Comparable, Totals
Defined in:
lib/day.rb

Constant Summary

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

Methods included from Attributes

#initialize

Instance Attribute Details

#blocksObject

Returns the value of attribute blocks


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

def blocks
  @blocks
end

#dayObject

Returns the value of attribute day


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

def day
  @day
end

#monthObject

Returns the value of attribute month


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

def month
  @month
end

#short_yearObject

Returns the value of attribute short_year


7
8
9
# File 'lib/day.rb', line 7

def short_year
  @short_year
end

#yearObject

Returns the value of attribute year


7
8
9
# File 'lib/day.rb', line 7

def year
  @year
end

Class Method Details

.from(date) ⇒ Object

Parameters:

  • date (String)

    a date of format “DD.MM.YY”, e.g., “26.03.15”.


12
13
14
15
16
# File 'lib/day.rb', line 12

def self.from(date)
  day = Day.new
  day.day, day.month, day.short_year = date.split('.').map(&:to_i)
  day
end

Instance Method Details

#<=>(other) ⇒ Object


204
205
206
207
208
209
210
211
212
213
214
# File 'lib/day.rb', line 204

def <=>(other)
  if year == other.year
    if month == other.month
      day <=> other.day
    else
      month <=> other.month
    end
  else
    year <=> other.year
  end
end

#add(*blocks) ⇒ Object


129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/day.rb', line 129

def add(*blocks)
  highlight!
  blocks.each do |block|

    # Get rid of old blocks with shorter spans than block's.
    self.blocks.reject! do |b|
      b.start >= block.start && b.finish <= block.finish
    end

    # Ignore new block if an existing block covers the new block's span.
    if self.blocks.any? { |b|
      b.start <= block.start && b.finish >= block.finish }
      next
    end

    # Connecting.
    if self.blocks.count > 1
      if (start_overlap = self.blocks.find { |b| b.include?(block.start) })
        if (finish_overlap = self.blocks.reverse.
              find { |b| b.include?(block.finish) })
          start_overlap.finish = finish_overlap.finish
          self.blocks.delete(finish_overlap)
          next
        end
      end
    end

    # Prepending.
    if (overlap = self.blocks.find { |b| b.include?(block.finish) })
      overlap.start = block.start
      next
    end

    # Appending.
    if (overlap = self.blocks.find { |b| b.include?(block.start) })
      overlap.finish = block.finish
      next
    end

    self.blocks << block
  end
end

#at?(time) ⇒ Boolean Also known as: include?

Returns:

  • (Boolean)

216
217
218
# File 'lib/day.rb', line 216

def at?(time)
  (day == time.day) && (month == time.month) && (year == time.year)
end

#block_countObject


125
126
127
# File 'lib/day.rb', line 125

def block_count
  blocks.count
end

#childrenObject


200
201
202
# File 'lib/day.rb', line 200

def children
  blocks
end

#clear_tagsObject


45
46
47
# File 'lib/day.rb', line 45

def clear_tags
  @tags = []
end

#dateObject


53
54
55
# File 'lib/day.rb', line 53

def date
  "#{day.left_pad}.#{month.left_pad}.#{short_year}"
end

#day?(day_nr) ⇒ Boolean

Returns:

  • (Boolean)

18
19
20
# File 'lib/day.rb', line 18

def day?(day_nr)
  day == day_nr.to_i
end

#empty?Boolean

Returns:

  • (Boolean)

117
118
119
# File 'lib/day.rb', line 117

def empty?
  blocks.empty?
end

#extract_tags(tags_str) ⇒ Object


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

def extract_tags(tags_str)
  @tags = tags | tags_str.split(',').map { |s| s.strip.downcase.to_sym }
end

#monday?Boolean

Returns:

  • (Boolean)

57
58
59
# File 'lib/day.rb', line 57

def monday?
  to_time.monday?
end

#month?(month_nr) ⇒ Boolean

Returns:

  • (Boolean)

22
23
24
# File 'lib/day.rb', line 22

def month?(month_nr)
  month == month_nr.to_i
end

#next_day(month) ⇒ Object


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

def next_day(month)
  month.days.find { |d| d.at? time_on_next_day }
end

#remove(*blocks) ⇒ Object


172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/day.rb', line 172

def remove(*blocks)
  highlight!
  blocks.each do |block|
    # Get rid of old blocks with shorter spans than block's. Shadowing.
    self.blocks.reject! { |b| block.include?(b) }

    # Splitting up :(
    if (to_split = self.blocks.find { |b| b.strict_include?(block) })
      new_block = Block.new(
        :start  => block.finish,
        :finish => to_split.finish
      )
      to_split.finish = block.start
      self.blocks << new_block
    end

    # Removing at start.
    if (b = self.blocks.find { |b| b.strict_include? block.finish })
      b.start = block.finish
    end

    # Removing at finish.
    if (b = self.blocks.find { |b| b.strict_include? block.start })
      b.finish = block.start
    end
  end
end

#remove_ongoing_blocks!Object


231
232
233
# File 'lib/day.rb', line 231

def remove_ongoing_blocks!
  blocks.reject!(&:ongoing?)
end

#set(time) ⇒ Object


225
226
227
228
229
# File 'lib/day.rb', line 225

def set(time)
  self.day   = time.day
  self.month = time.month
  self.year  = time.year
end

#short_nameObject


73
74
75
# File 'lib/day.rb', line 73

def short_name
  to_time.strftime('%a')
end

#tagsObject


49
50
51
# File 'lib/day.rb', line 49

def tags
  @tags ||= []
end

#tags_strObject


113
114
115
# File 'lib/day.rb', line 113

def tags_str
  tags.map { |t| t.to_s.upcase }.join(', ')
end

#time_on_next_dayObject


77
78
79
# File 'lib/day.rb', line 77

def time_on_next_day
  to_time.to_date.next_day.to_time
end

#to_s(options = {}) ⇒ Object


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/day.rb', line 85

def to_s(options = {})
  blocks.sort!
  blocks_str = blocks.map { |b| b.to_s(options) }.join('   ')

  # Padding before "Total:"
  max_block_count = options.fetch :padding, 0
  if block_count < max_block_count
    (max_block_count - block_count).times do
      blocks_str << '              '
    end
  end

  str = "#{date}   #{blocks_str}   "
  str << "Total: #{total_str}" if blocks.any?
  str << "   #{tags_str}" if tags.any?

  if options.fetch :prepend_name, false
    str = str.prepend "#{short_name}   "
  end

  if options.fetch :fancy, false
    return str.highlighted if highlight?
    return str.today_color if today?
  end

  str
end

#to_timeObject


69
70
71
# File 'lib/day.rb', line 69

def to_time
  @time ||= Time.new year, month, day
end

#today?Boolean

Returns:

  • (Boolean)

221
222
223
# File 'lib/day.rb', line 221

def today?
  at? Time.now
end

#workday?Boolean

Returns:

  • (Boolean)

61
62
63
64
65
66
67
# File 'lib/day.rb', line 61

def workday?
  if Punch.config.workdays.empty?
    return !(to_time.saturday? || to_time.sunday?)
  end

  Punch.config.workdays.any? { |day| to_time.send("#{day}?") }
end

#year?(year_nr) ⇒ Boolean

Returns:

  • (Boolean)

26
27
28
29
# File 'lib/day.rb', line 26

def year?(year_nr)
  year_nr = year_nr.to_i
  short_year == year_nr || year == year_nr
end