Class: JIRA::Resource::Field

Inherits:
Base
  • Object
show all
Defined in:
lib/jira/resource/field.rb

Constant Summary

Constants inherited from Base

Base::QUERY_PARAMS_FOR_SEARCH, Base::QUERY_PARAMS_FOR_SINGLE_FETCH

Instance Attribute Summary

Attributes inherited from Base

#attrs, #client, #deleted, #expanded

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

all, belongs_to, belongs_to_relationships, build, collection_attributes_are_nested, #collection_path, collection_path, #delete, endpoint_name, #fetch, find, #has_errors?, has_many, has_one, #id, #initialize, key_attribute, #key_value, nested_collections, #new_record?, parse_json, #patched_url, #path_component, #save, #save!, #set_attrs, #set_attrs_from_response, singular_path, #to_json, #to_s, #to_sym, #url

Constructor Details

This class inherits a constructor from JIRA::Base

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/jira/resource/field.rb', line 69

def method_missing(method_name, *args, &block)
  if attrs.key?(method_name.to_s)
    attrs[method_name.to_s]
  else
    official_name = client.Field.name_to_id(method_name)
    if attrs.key?(official_name)
      attrs[official_name]
    else
      super(method_name, *args, &block)
    end
  end
end

Class Method Details

.field_map(client) ⇒ Object



51
52
53
# File 'lib/jira/resource/field.rb', line 51

def self.field_map(client)
  client.cache.field_map
end

.map_fields(client) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/jira/resource/field.rb', line 20

def self.map_fields(client)
  field_map = {}
  field_map_reverse = {}
  fields = client.Field.all

  # two pass approach, so that a custom field with the same name
  # as a system field can't take precedence
  fields.each do |f|
    next if f.custom
    name = safe_name(f.name)
    field_map_reverse[f.id] = [f.name, name] # capture both the official name, and the mapped name
    field_map[name] = f.id
  end

  fields.each do |f|
    next unless f.custom
    name = if field_map.key? f.name
             renamed = safer_name(f.name, f.id)
             warn "Duplicate Field name #{f.name} #{f.id} - renaming as #{renamed}"
             renamed
           else
             safe_name(f.name)
    end
    field_map_reverse[f.id] = [f.name, name] # capture both the official name, and the mapped name
    field_map[name] = f.id
  end

  client.cache.field_map_reverse = field_map_reverse # not sure where this will be used yet, but sure to be useful
  client.cache.field_map = field_map
end

.name_to_id(client, field_name) ⇒ Object



55
56
57
58
59
# File 'lib/jira/resource/field.rb', line 55

def self.name_to_id(client, field_name)
  field_name = field_name.to_s
  return field_name unless client.cache.field_map && client.cache.field_map[field_name]
  client.cache.field_map[field_name]
end

.safe_name(description) ⇒ Object

translate a custom field description to a method-safe name



9
10
11
# File 'lib/jira/resource/field.rb', line 9

def self.safe_name(description)
  description.gsub(/[^a-zA-Z0-9]/, '_')
end

.safer_name(description, jira_id) ⇒ Object

safe_name plus disambiguation if it fails it uses the original jira id (customfield_#####)



14
15
16
17
18
# File 'lib/jira/resource/field.rb', line 14

def self.safer_name(description, jira_id)
  "#{safe_name(description)}_#{jira_id.split('_')[1]}"
rescue StandardError
  jira_id
end

Instance Method Details

#respond_to?(method_name, _include_all = false) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
64
65
66
67
# File 'lib/jira/resource/field.rb', line 61

def respond_to?(method_name, _include_all = false)
  if [method_name.to_s, client.Field.name_to_id(method_name)].any? { |k| attrs.key?(k) }
    true
  else
    super(method_name)
  end
end