Class: TeamApi::CrossReferenceData

Inherits:
Object
  • Object
show all
Defined in:
lib/team_api/cross_reference_data.rb

Overview

Provides a collection with the ability to replace identifiers with more detailed cross-reference values from another collection, and with the ability to construct its own cross-reference values to assign to values from other collections.

The intent is to provide enough cross-reference information to surface in an API without requiring the client to join the data necessary to produce cross-links. For example, instead of surfacing ‘[’mbland’]‘ in a list of team members, this class will produce `[=> ‘mbland’, ‘full_name’

> ‘Mike Bland’, ‘first_name’ => ‘Mike’, ‘last_name’ => ‘Bland’]‘, which

the client can use to more easily sort multiple values and transform into: ‘<a href=“hub.18f.gov/team/mbland/”>Mike Bland</a>`.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site, collection_name, item_xref_fields) ⇒ CrossReferenceData

Returns a new instance of CrossReferenceData.

Parameters:

  • site object

  • name of collection within site.data

  • name of the field to cross-reference

  • list of fields from which to produce cross-references for this collection



30
31
32
33
34
35
# File 'lib/team_api/cross_reference_data.rb', line 30

def initialize(site, collection_name, item_xref_fields)
  @collection_name = collection_name
  @data = site.data[collection_name] || {}
  @item_xref_fields = item_xref_fields
  @public_mode = site.config['public']
end

Instance Attribute Details

#collection_nameObject

Returns the value of attribute collection_name.



23
24
25
# File 'lib/team_api/cross_reference_data.rb', line 23

def collection_name
  @collection_name
end

#dataObject

Returns the value of attribute data.



23
24
25
# File 'lib/team_api/cross_reference_data.rb', line 23

def data
  @data
end

#item_xref_fieldsObject

Returns the value of attribute item_xref_fields.



23
24
25
# File 'lib/team_api/cross_reference_data.rb', line 23

def item_xref_fields
  @item_xref_fields
end

#public_modeObject

Returns the value of attribute public_mode.



23
24
25
# File 'lib/team_api/cross_reference_data.rb', line 23

def public_mode
  @public_mode
end

Instance Method Details

#create_xrefs(target, source_to_target_field: nil, alternate_names: nil) ⇒ Object

Translates identifiers into cross-reference values in both this object’s collection and the target collection.

This object’s collection is considered the “source”, and references to its values will be injected into “target”. For each “source” object, source[target.collection_name] should be an existing field containing identifiers that are keys into target.data. The target collection values should not contain a target[source.collection_name] field; that field will be created by this method.

Parameters:

  • contains data to cross-reference with items from this object’s collection

  • (defaults to: nil)

    if specified, the field from this collection’s objects that contain identifiers of objects stored within target; if not specified, target.collection_name will be used instead



58
59
60
61
62
63
64
65
66
# File 'lib/team_api/cross_reference_data.rb', line 58

def create_xrefs(target, source_to_target_field: nil, alternate_names: nil)
  item_xref_fields << 'deprecated_name'

  target_collection_field = source_to_target_field || target.collection_name
  data.values.each do |source|
    create_xrefs_for_source source, target_collection_field, target, alternate_names
  end
  target.data.values.each { |item| (item[collection_name] || []).uniq! }
end

#item_to_xref(item) ⇒ Object

Selects fields from item to produce a smaller hash as a cross-reference.



39
40
41
# File 'lib/team_api/cross_reference_data.rb', line 39

def item_to_xref(item)
  item.select { |field, _| item_xref_fields.include? field }
end