Class: Procedo::Engine::Intervention::WorkingPeriod

Inherits:
Object
  • Object
show all
Defined in:
lib/procedo/engine/intervention/working_period.rb

Constant Summary collapse

DEFAULT_DURATION =
2.hours

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, attributes = {}) ⇒ WorkingPeriod

Returns a new instance of WorkingPeriod.


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/procedo/engine/intervention/working_period.rb', line 9

def initialize(id, attributes = {})
  @id = id.to_s
  @started_at = begin
                  Time.new(*attributes[:started_at].split(/\D+/))
                rescue
                  nil
                end
  @started_at ||= (Time.zone.now - DEFAULT_DURATION)
  @stopped_at = begin
                  Time.new(*attributes[:stopped_at].split(/\D+/))
                rescue
                  nil
                end
  @stopped_at ||= (@started_at + DEFAULT_DURATION)
end

Instance Attribute Details

#idObject

Returns the value of attribute id


7
8
9
# File 'lib/procedo/engine/intervention/working_period.rb', line 7

def id
  @id
end

#started_atObject

Returns the value of attribute started_at


7
8
9
# File 'lib/procedo/engine/intervention/working_period.rb', line 7

def started_at
  @started_at
end

#stopped_atObject

Returns the value of attribute stopped_at


7
8
9
# File 'lib/procedo/engine/intervention/working_period.rb', line 7

def stopped_at
  @stopped_at
end

Instance Method Details

#durationObject


25
26
27
# File 'lib/procedo/engine/intervention/working_period.rb', line 25

def duration
  @stopped_at - @started_at
end

#impact_with(steps) ⇒ Object


33
34
35
36
37
# File 'lib/procedo/engine/intervention/working_period.rb', line 33

def impact_with(steps)
  raise 'Invalid steps: ' + steps.inspect if steps.size != 1
  field = steps.first
  send(field + '=', send(field))
end

#to_hashObject


29
30
31
# File 'lib/procedo/engine/intervention/working_period.rb', line 29

def to_hash
  { started_at: @started_at.strftime('%Y-%m-%d %H:%M'), stopped_at: @stopped_at.strftime('%Y-%m-%d %H:%M') }
end