Class: Checkoff::Internal::TaskTiming

Inherits:
Object
  • Object
show all
Defined in:
lib/checkoff/internal/task_timing.rb

Overview

Utility methods for working with task dates and times

Instance Method Summary collapse

Constructor Details

#initialize(time_class: Time, date_class: Date, client: Checkoff::Clients.new.client, custom_fields: Checkoff::CustomFields.new(client: client)) ⇒ TaskTiming

Returns a new instance of TaskTiming.

Parameters:

  • time_class (Class<Time>) (defaults to: Time)
  • date_class (Class<Date>) (defaults to: Date)
  • client (Asana::Client) (defaults to: Checkoff::Clients.new.client)
  • custom_fields (Checkoff::CustomFields) (defaults to: Checkoff::CustomFields.new(client: client))


11
12
13
14
15
16
17
# File 'lib/checkoff/internal/task_timing.rb', line 11

def initialize(time_class: Time, date_class: Date,
               client: Checkoff::Clients.new.client,
               custom_fields: Checkoff::CustomFields.new(client: client))
  @time_class = time_class
  @date_class = date_class
  @custom_fields = custom_fields
end

Instance Method Details

#custom_field(task, custom_field_name) ⇒ Time, ...

Parameters:

  • task (Asana::Resources::Task)
  • custom_field_name (String)

Returns:

  • (Time, Date, nil)


68
69
70
71
72
73
74
75
76
# File 'lib/checkoff/internal/task_timing.rb', line 68

def custom_field(task, custom_field_name)
  custom_field = @custom_fields.resource_custom_field_by_name_or_raise(task, custom_field_name)
  # @sg-ignore
  # @type [String, nil]
  time_str = custom_field.fetch('display_value')
  return nil if time_str.nil?

  Time.parse(time_str)
end

#date_or_time_field_by_name(task, field_name) ⇒ Date, ...

@sg-ignore

Parameters:

  • task (Asana::Resources::Task)
  • field_name (Symbol, Array)

Returns:

  • (Date, Time, nil)


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/checkoff/internal/task_timing.rb', line 83

def date_or_time_field_by_name(task, field_name)
  return due_date_or_time(task) if field_name == :due

  return start_date_or_time(task) if field_name == :start

  return modified_time(task) if field_name == :modified

  return start_date_or_time(task) || due_date_or_time(task) if field_name == :ready

  if field_name.is_a?(Array)
    # @sg-ignore
    # @type [Symbol]
    actual_field_name = field_name.first
    args = field_name[1..]

    return custom_field(task, *args) if actual_field_name == :custom_field
  end

  raise "Teach me how to handle field #{field_name.inspect}"
end

#due_date_or_time(task) ⇒ Date, ...

@sg-ignore

Parameters:

  • task (Asana::Resources::Task)
  • field_name (Symbol)

Returns:

  • (Date, Time, nil)


49
50
51
52
53
54
55
# File 'lib/checkoff/internal/task_timing.rb', line 49

def due_date_or_time(task)
  return @time_class.parse(task.due_at) unless task.due_at.nil?

  return @date_class.parse(task.due_on) unless task.due_on.nil?

  nil
end

#due_time(task) ⇒ Time?

Parameters:

  • task (Asana::Resources::Task)

Returns:

  • (Time, nil)


27
28
29
# File 'lib/checkoff/internal/task_timing.rb', line 27

def due_time(task)
  date_or_time_field_by_name(task, :due)&.to_time
end

#modified_time(task) ⇒ Time?

Parameters:

  • task (Asana::Resources::Task)

Returns:

  • (Time, nil)


60
61
62
# File 'lib/checkoff/internal/task_timing.rb', line 60

def modified_time(task)
  @time_class.parse(task.modified_at) unless task.modified_at.nil?
end

#start_date_or_time(task) ⇒ Date, ...

@sg-ignore

Parameters:

  • task (Asana::Resources::Task)
  • field_name (Symbol)

Returns:

  • (Date, Time, nil)


36
37
38
39
40
41
42
# File 'lib/checkoff/internal/task_timing.rb', line 36

def start_date_or_time(task)
  return @time_class.parse(task.start_at) unless task.start_at.nil?

  return @date_class.parse(task.start_on) unless task.start_on.nil?

  nil
end

#start_time(task) ⇒ Time?

Parameters:

  • task (Asana::Resources::Task)

Returns:

  • (Time, nil)


21
22
23
# File 'lib/checkoff/internal/task_timing.rb', line 21

def start_time(task)
  date_or_time_field_by_name(task, :start)&.to_time
end