Class: Luxafor::Toggl::State

Inherits:
Object
  • Object
show all
Defined in:
lib/luxafor/toggl/state.rb

Constant Summary collapse

DEFAULT_TIME =
DateTime.parse('2000-01-01 00:00:00')

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.coerce_as_of(value) ⇒ Object



15
16
17
18
19
# File 'lib/luxafor/toggl/state.rb', line 15

def self.coerce_as_of(value)
  return value if value.is_a? DateTime

  DateTime.parse(value)
end

.coerce_state(value) ⇒ Object



11
12
13
# File 'lib/luxafor/toggl/state.rb', line 11

def self.coerce_state(value)
  value.to_sym
end

.new_from_task(task) ⇒ Object



21
22
23
24
25
26
# File 'lib/luxafor/toggl/state.rb', line 21

def self.new_from_task(task)
  instance = allocate

  instance.initialize_from_task(task)
  instance
end

Instance Method Details

#colourObject



39
40
41
42
43
44
45
46
47
48
# File 'lib/luxafor/toggl/state.rb', line 39

def colour
  case state
  when :idle
    '000000'
  when :busy
    'FF0000'
  when :available
    '00FF00'
  end
end

#initialize_from_task(task) ⇒ Object



28
29
30
31
# File 'lib/luxafor/toggl/state.rb', line 28

def initialize_from_task(task)
  @state = determine_state_from_task(task)
  @as_of = determine_as_of_from_task(task)
end

#store!(destination) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/luxafor/toggl/state.rb', line 50

def store!(destination)
  data = {
    'state' => state,
    'as_of' => as_of.to_s,
  }

  File.unlink(destination) if File.exist?(destination)

  File.open(destination, 'w') do |f|
    f.write(Oj.dump(data))
  end
end

#sufficiently_different_from?(other_state) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
37
# File 'lib/luxafor/toggl/state.rb', line 33

def sufficiently_different_from?(other_state)
  return true if @state != other_state.state

  false
end