Class: Did::Action::Submit

Inherits:
Object
  • Object
show all
Defined in:
lib/did/action/submit.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSubmit

Returns a new instance of Submit.



12
13
14
# File 'lib/did/action/submit.rb', line 12

def initialize()
  @resolved = false
end

Instance Attribute Details

#end_timeObject

Returns the value of attribute end_time.



10
11
12
# File 'lib/did/action/submit.rb', line 10

def end_time
  @end_time
end

#labelsObject

Returns the value of attribute labels.



8
9
10
# File 'lib/did/action/submit.rb', line 8

def labels
  @labels
end

#start_timeObject

Returns the value of attribute start_time.



9
10
11
# File 'lib/did/action/submit.rb', line 9

def start_time
  @start_time
end

Instance Method Details

#performObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
# File 'lib/did/action/submit.rb', line 16

def perform
  tags = labels.map do |label|
    Tag.find_or_create_by_label(label);
  end
  
  prior_sitting = Sitting.find_current
  current_sitting = nil
  sitting_start = true
  if prior_sitting && prior_sitting.end_time.nil?
    if prior_sitting.start_time > @start_time
      @start_time = prior_sitting.start_time
    end
    current_sitting = prior_sitting
    prior_sitting = nil
  elsif prior_sitting && prior_sitting.end_time > @start_time
    @start_time = prior_sitting.end_time + 1.second
    current_sitting = prior_sitting
    sitting_start = false
  end

  prior_span = prior_sitting.end_span if prior_sitting
  if prior_span && prior_span.tags == tags
    prior_span.update_attributes(:end_time => @end_time)
    prior_sitting.update_attributes(:end_time => @end_time)
    return
  end
  
  span = Span.create(:tags => tags, 
                     :end_time => @end_time, :start_time => @start_time,
                     :sitting_start => sitting_start, :sitting_end => true)

  # We are about to start a new sitting, so we close out the old.
  if prior_sitting && current_sitting.nil?
    prior_sitting.update_attributes(:current => false)
  end

  if current_sitting
    # Shift end time of sitting to include new span.
    current_sitting.end_span.update_attributes(:sitting_end => false) if current_sitting.end_span
    params = {
      :end_time => @end_time,
      :end_span_id => span.id
    }

    # We are commandeering the last sitting created by a 'sit' action.
    params[:start_time] = @start_time if current_sitting.end_time.nil?
    current_sitting.update_attributes(params)
  end
  
  unless current_sitting 
    current_sitting = Sitting.create(:start_time => @start_time, :end_time => @end_time, 
                                     :end_span_id => span.id, :current => true)
  end

  span.update_attributes(:sitting_id => current_sitting.id)
end