Class: Checkoff::CustomFields

Inherits:
Object
  • Object
show all
Extended by:
CacheMethod::ClassMethods
Defined in:
lib/checkoff/custom_fields.rb

Overview

Work with custom fields in Asana

Constant Summary collapse

MINUTE =
60
HOUR =
MINUTE * 60
DAY =
24 * HOUR
REALLY_LONG_CACHE_TIME =
HOUR * 1
LONG_CACHE_TIME =
MINUTE * 15
SHORT_CACHE_TIME =
MINUTE

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config: Checkoff::Internal::ConfigLoader.load(:asana), clients: Checkoff::Clients.new(config:), client: clients.client, workspaces: Checkoff::Workspaces.new(config:, client:)) ⇒ CustomFields

Returns a new instance of CustomFields.

Parameters:



31
32
33
34
35
36
37
38
# File 'lib/checkoff/custom_fields.rb', line 31

def initialize(config: Checkoff::Internal::ConfigLoader.load(:asana),
               clients: Checkoff::Clients.new(config:),
               client: clients.client,
               workspaces: Checkoff::Workspaces.new(config:,
                                                    client:))
  @workspaces = workspaces
  @client = client
end

Class Method Details

.runvoid

This method returns an undefined value.



186
187
188
189
190
191
192
193
194
195
196
# File 'lib/checkoff/custom_fields.rb', line 186

def run
  # @sg-ignore
  # @type [String]
  workspace_name = ARGV[0] || raise('Please pass workspace name as first argument')
  # @sg-ignore
  # @type [String]
  custom_field_name = ARGV[1] || raise('Please pass custom_field name as second argument')
  custom_fields = Checkoff::CustomFields.new
  custom_field = custom_fields.custom_field_or_raise(workspace_name, custom_field_name)
  puts "Results: #{custom_field}"
end

Instance Method Details

#custom_field(workspace_name, custom_field_name) ⇒ Asana::Resources::CustomField?

@sg-ignore

Parameters:

  • workspace_name (String)
  • custom_field_name (String)

Returns:

  • (Asana::Resources::CustomField, nil)


57
58
59
60
61
# File 'lib/checkoff/custom_fields.rb', line 57

def custom_field(workspace_name, custom_field_name)
  workspace = workspaces.workspace_or_raise(workspace_name)
  custom_fields = client.custom_fields.get_custom_fields_for_workspace(workspace_gid: workspace.gid)
  custom_fields.find { |custom_field| custom_field.name == custom_field_name }
end

#custom_field_or_raise(workspace_name, custom_field_name) ⇒ Asana::Resources::CustomField

Parameters:

  • workspace_name (String)
  • custom_field_name (String)

Returns:

  • (Asana::Resources::CustomField)


44
45
46
47
48
49
# File 'lib/checkoff/custom_fields.rb', line 44

def custom_field_or_raise(workspace_name, custom_field_name)
  cf = custom_field(workspace_name, custom_field_name)
  raise "Could not find custom_field #{custom_field_name} under workspace #{workspace_name}." if cf.nil?

  cf
end

#resource_custom_field_by_gid_or_raise(resource, custom_field_gid) ⇒ Hash

Parameters:

  • resource (Asana::Resources::Project, Asana::Resources::Task)
  • custom_field_gid (String)

Returns:

  • (Hash)


127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/checkoff/custom_fields.rb', line 127

def resource_custom_field_by_gid_or_raise(resource, custom_field_gid)
  # @type [Array<Hash>]
  custom_fields = resource.custom_fields
  if custom_fields.nil?
    raise "Could not find custom_fields under project (was 'custom_fields' included in 'extra_fields'?)"
  end

  # @sg-ignore
  # @type [Hash, nil]
  matched_custom_field = custom_fields.find { |data| data.fetch('gid') == custom_field_gid }
  if matched_custom_field.nil?
    raise "Could not find custom field with gid #{custom_field_gid} " \
          "in gid #{resource.gid} with custom fields #{custom_fields}"
  end

  matched_custom_field
end

#resource_custom_field_by_name(resource, custom_field_name) ⇒ Hash?

@sg-ignore

Parameters:

  • resource (Asana::Resources::Task, Asana::Resources::Project)
  • custom_field_name (String)

Returns:

  • (Hash, nil)


99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/checkoff/custom_fields.rb', line 99

def resource_custom_field_by_name(resource, custom_field_name)
  # @sg-ignore
  # @type [Array<Hash>]
  custom_fields = resource.custom_fields
  if custom_fields.nil?
    raise "custom fields not found on resource - did you add 'custom_fields' in your extra_fields argument?"
  end

  # @sg-ignore
  # @type [Hash, nil]
  custom_fields.find { |field| field.fetch('name') == custom_field_name }
end

#resource_custom_field_by_name_or_raise(resource, custom_field_name) ⇒ Hash

Parameters:

  • resource (Asana::Resources::Task, Asana::Resources::Project)
  • custom_field_name (String)

Returns:

  • (Hash)


115
116
117
118
119
120
121
122
# File 'lib/checkoff/custom_fields.rb', line 115

def resource_custom_field_by_name_or_raise(resource, custom_field_name)
  custom_field = resource_custom_field_by_name(resource, custom_field_name)
  if custom_field.nil?
    raise "Could not find custom field with name #{custom_field_name} " \
          "in gid #{resource.gid} with custom fields #{resource.custom_fields}"
  end
  custom_field
end

#resource_custom_field_values_gids_or_raise(resource, custom_field_gid) ⇒ Array<String>

Parameters:

  • resource (Asana::Resources::Project, Asana::Resources::Task)
  • custom_field_gid (String)

Returns:

  • (Array<String>)


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

def resource_custom_field_values_gids_or_raise(resource, custom_field_gid)
  custom_field = resource_custom_field_by_gid_or_raise(resource, custom_field_gid)

  resource_custom_field_enum_values(custom_field).flat_map do |enum_value|
    find_gids(custom_field, enum_value)
  end
rescue StandardError => e
  raise "Could not process custom field with gid #{custom_field_gid} " \
        "in gid #{resource.gid} with custom fields #{resource.custom_fields.inspect}: #{e}"
end

#resource_custom_field_values_names_by_name(resource, custom_field_name) ⇒ Array<String>

Parameters:

  • resource (Asana::Resources::Project, Asana::Resources::Task)
  • custom_field_name (String)

Returns:

  • (Array<String>)


82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/checkoff/custom_fields.rb', line 82

def resource_custom_field_values_names_by_name(resource, custom_field_name)
  custom_field = resource_custom_field_by_name(resource, custom_field_name)
  return [] if custom_field.nil?

  resource_custom_field_enum_values(custom_field).flat_map do |enum_value|
    if enum_value.nil?
      []
    else
      [enum_value.fetch('name')]
    end
  end
end