Class: TimeCost::Range

Inherits:
Object
  • Object
show all
Defined in:
lib/timecost/range.rb

Constant Summary collapse

GRANULARITY_DEFAULT =
0.5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(commit, options = {}) ⇒ Range

Returns a new instance of Range.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/timecost/range.rb', line 8

def initialize commit, options = {}
  @granularity = options[:granularity] || GRANULARITY_DEFAULT

  # FIXME: First approximation for users
  # later, we'll replace with @user = User.parse(commit.author)
  @author = commit.author

  @time_stop = DateTime.parse(commit.date)
  @time_start = @time_stop - (@granularity * 3 / 24.0)
  @commits = [commit]
  self
end

Instance Attribute Details

#authorObject

Returns the value of attribute author.



4
5
6
# File 'lib/timecost/range.rb', line 4

def author
  @author
end

#commitsObject

Returns the value of attribute commits.



4
5
6
# File 'lib/timecost/range.rb', line 4

def commits
  @commits
end

#time_startObject

Returns the value of attribute time_start.



4
5
6
# File 'lib/timecost/range.rb', line 4

def time_start
  @time_start
end

#time_stopObject

Returns the value of attribute time_stop.



4
5
6
# File 'lib/timecost/range.rb', line 4

def time_stop
  @time_stop
end

Instance Method Details

#diffObject



97
98
99
# File 'lib/timecost/range.rb', line 97

def diff 
  return ("%.2f" % ((@time_stop - fixed_start).to_f * 24)).to_f
end

#fixed_startObject



93
94
95
# File 'lib/timecost/range.rb', line 93

def fixed_start 
  return @time_start + (@granularity/24.0)
end

#merge(range) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/timecost/range.rb', line 21

def merge range
  #  B -----[----]----
  #  A  --[----]------
  #  = ---[------]----

  # minimum of both
  new_start = if range.time_start < @time_start then range.time_start
        else @time_start
        end

  new_end = if range.time_stop >= @time_stop then range.time_stop
        else @time_stop
        end

  @time_start = new_start
  @time_stop = new_end
  @commits.concat range.commits
end

#overlap?(range) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/timecost/range.rb', line 40

def overlap? range
  result = false

  # return early result if ranges come from different authors
  return false if (@author != range.author)

  # Ref ----[----]-----
  # overlapping :
  #  A  -[----]--------
  #  B  -------[----]--
  #  C  -[----------]--
  #  D  ------[]-------
  # non-overlapping : 
  #  E  -[]------------
  #  F  -----------[]--

  start_before_start = (range.time_start < @time_start)
  start_after_start = (range.time_start >= @time_start)
  start_after_stop = (range.time_start >= @time_stop)
  start_before_stop = (range.time_start < @time_stop) 

  stop_before_stop = (range.time_stop < @time_stop)
  stop_after_stop = (range.time_stop >= @time_stop)
  stop_before_start = (range.time_stop < @time_start)
  stop_after_start = (range.time_stop >= @time_start)

  # A case
  if start_before_start and start_before_stop and
    stop_after_start and stop_before_stop then
    result = true
  end

  # B case
  if start_after_start and start_before_stop and
    stop_after_start and stop_after_stop then
    result = true
  end

  # C case
  if start_before_start and start_before_stop and
    stop_after_start and stop_after_stop then
    result = true
  end

  # D case
  if start_after_start and start_before_stop and
    stop_after_start and stop_before_stop then
    result = true
  end

  return result
end

#to_s(show_authors = true) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/timecost/range.rb', line 101

def to_s show_authors = true
  val = "(%s)\t%s - %s\n" % [diff, fixed_start, @time_stop]  
  if show_authors  then
    val += "\tby %s\n" % @commits.first.author
  end
  @commits.each do |commit|
    lines = []
    lines.concat commit.note.split(/\n/)
    r = lines.map{ |s| "\t  %s" % s }.join "\n"
    r[1] = '*'
    val += r + "\n"
  end
  return val
end