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: config), client: clients.client, workspaces: Checkoff::Workspaces.new(config: config, client: client)) ⇒ CustomFields

Returns a new instance of CustomFields.

Parameters:

  • config (Hash) (defaults to: Checkoff::Internal::ConfigLoader.load(:asana))
  • workspaces (Checkoff::Workspaces) (defaults to: Checkoff::Workspaces.new(config: config, client: client))
  • clients (Checkoff::Clients) (defaults to: Checkoff::Clients.new(config: config))
  • client (Asana::Client) (defaults to: clients.client)


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

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

Class Method Details

.runvoid

This method returns an undefined value.



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

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)


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

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)


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

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)


119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/checkoff/custom_fields.rb', line 119

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:

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

Returns:

  • (Hash, nil)


91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/checkoff/custom_fields.rb', line 91

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)


107
108
109
110
111
112
113
114
# File 'lib/checkoff/custom_fields.rb', line 107

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>)


67
68
69
70
71
72
73
# File 'lib/checkoff/custom_fields.rb', line 67

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
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>)


78
79
80
81
82
83
84
85
# File 'lib/checkoff/custom_fields.rb', line 78

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|
    find_names(enum_value)
  end
end