Class: TeamApi::CrossReferenceData
- Inherits:
-
Object
- Object
- TeamApi::CrossReferenceData
- 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
-
#collection_name ⇒ Object
Returns the value of attribute collection_name.
-
#data ⇒ Object
Returns the value of attribute data.
-
#item_xref_fields ⇒ Object
Returns the value of attribute item_xref_fields.
-
#public_mode ⇒ Object
Returns the value of attribute public_mode.
Instance Method Summary collapse
-
#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.
-
#initialize(site, collection_name, item_xref_fields) ⇒ CrossReferenceData
constructor
A new instance of CrossReferenceData.
-
#item_to_xref(item) ⇒ Object
Selects fields from ‘item` to produce a smaller hash as a cross-reference.
Constructor Details
#initialize(site, collection_name, item_xref_fields) ⇒ CrossReferenceData
Returns a new instance of CrossReferenceData.
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_name ⇒ Object
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 |
#data ⇒ Object
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_fields ⇒ Object
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_mode ⇒ Object
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` should be an existing field containing identifiers that are keys into `target.data`. The `target` collection values should not contain a `target` field; that field will be created by this method.
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 |