Class: Checkoff::CustomFields

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

Overview

Work with custom fields in Asana

Constant Summary collapse

MINUTE =

Returns:

  • (Object)
60
HOUR =

Returns:

  • (Object)
MINUTE * 60
DAY =

Returns:

  • (Object)
24 * HOUR
REALLY_LONG_CACHE_TIME =

Returns:

  • (Object)
HOUR * 1
LONG_CACHE_TIME =

Returns:

  • (Object)
MINUTE * 15
SHORT_CACHE_TIME =

Returns:

  • (Object)
MINUTE

Instance Attribute Summary collapse

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:)) ⇒ Object

sord warn - Asana::Client wasn't able to be resolved to a constant in this project @param config

@param workspaces

@param clients

@param client



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

Instance Attribute Details

#clientAsana::Client (readonly)

sord warn - Asana::Client wasn't able to be resolved to a constant in this project

Returns:

  • (Asana::Client)


179
180
181
# File 'lib/checkoff/custom_fields.rb', line 179

def client
  @client
end

#workspacesCheckoff::Workspaces (readonly)



176
177
178
# File 'lib/checkoff/custom_fields.rb', line 176

def workspaces
  @workspaces
end

Class Method Details

.runvoid

This method returns an undefined value.



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

def run
  # @type [String]
  workspace_name = ARGV[0] || raise('Please pass workspace name as first argument')
  # @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?

sord warn - Asana::Resources::CustomField wasn't able to be resolved to a constant in this project @param workspace_name

@param custom_field_name

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

sord warn - Asana::Resources::CustomField wasn't able to be resolved to a constant in this project @sg-ignore

@param workspace_name

@param custom_field_name

Parameters:

  • workspace_name (String)
  • custom_field_name (String)

Returns:

  • (Asana::Resources::CustomField)


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

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

#find_gids(custom_field, enum_value) ⇒ ::Array[String]

@param custom_field

@param enum_value

Parameters:

  • custom_field (::Hash[untyped, untyped])
  • enum_value (::Hash[String, String], nil)

Returns:

  • (::Array[String])


167
168
169
170
171
172
173
# File 'lib/checkoff/custom_fields.rb', line 167

def find_gids(custom_field, enum_value)
  if enum_value.nil?
    []
  else
    [enum_value.fetch('gid')]
  end
end

#resource_custom_field_by_gid_or_raise(resource, custom_field_gid) ⇒ ::Hash[untyped, untyped]

sord warn - Asana::Resources::Project wasn't able to be resolved to a constant in this project sord warn - Asana::Resources::Task wasn't able to be resolved to a constant in this project @sg-ignore

@param resource

@param custom_field_gid

Parameters:

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

Returns:

  • (::Hash[untyped, untyped])


127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# 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

  # @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[untyped, untyped]?

sord warn - Asana::Resources::Task wasn't able to be resolved to a constant in this project sord warn - Asana::Resources::Project wasn't able to be resolved to a constant in this project @param resource

@param custom_field_name

Parameters:

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

Returns:

  • (::Hash[untyped, untyped], nil)


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

def resource_custom_field_by_name(resource, custom_field_name)
  # @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

  # @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[untyped, untyped]

sord warn - Asana::Resources::Task wasn't able to be resolved to a constant in this project sord warn - Asana::Resources::Project wasn't able to be resolved to a constant in this project @sg-ignore

@param resource

@param custom_field_name

Parameters:

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

Returns:

  • (::Hash[untyped, untyped])


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

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_enum_values(custom_field) ⇒ ::Array[::Hash[untyped, untyped]]

@sg-ignore

@param custom_field

Parameters:

  • custom_field (::Hash[String, (::Hash[untyped, untyped] | ::Array[::Hash[untyped, untyped]])])

Returns:

  • (::Array[::Hash[untyped, untyped]])


150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/checkoff/custom_fields.rb', line 150

def resource_custom_field_enum_values(custom_field)
  resource_subtype = custom_field.fetch('resource_subtype')
  case resource_subtype
  when 'enum'
    # @type [Array<Hash>]
    [custom_field.fetch('enum_value')]
  when 'multi_enum'
    # @type [Array<Hash>]
    custom_field.fetch('multi_enum_values')
  else
    raise "Teach me how to handle resource_subtype #{resource_subtype}"
  end
end

#resource_custom_field_values_gids_or_raise(resource, custom_field_gid) ⇒ ::Array[String]

sord warn - Asana::Resources::Project wasn't able to be resolved to a constant in this project sord warn - Asana::Resources::Task wasn't able to be resolved to a constant in this project @param resource

@param custom_field_gid

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]

sord warn - Asana::Resources::Project wasn't able to be resolved to a constant in this project sord warn - Asana::Resources::Task wasn't able to be resolved to a constant in this project @sg-ignore

@param resource

@param custom_field_name

Parameters:

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

Returns:

  • (::Array[String])


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

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