class TeamApi::CollectionCanonicalizer

Public Class Methods

parse_collection_spec(collection_spec) click to toggle source
# File lib/team_api/collection_canonicalizer.rb, line 47
def self.parse_collection_spec(collection_spec)
  if collection_spec.instance_of? Hash
    [collection_spec['field'],
     Config.endpoint_info_by_collection[collection_spec['collection']]]
  else
    [collection_spec, Config.endpoint_info_by_collection[collection_spec]]
  end
end
sort_collections(site_data) click to toggle source
# File lib/team_api/collection_canonicalizer.rb, line 8
def self.sort_collections(site_data)
  Config.endpoint_config.each do |endpoint_info|
    collection = endpoint_info['collection']
    next unless site_data.member? collection
    sorted = sort_collection_values(endpoint_info,
      site_data[collection].values)
    sort_item_xrefs endpoint_info, sorted
    item_id_field = endpoint_info['item_id']
    site_data[collection] = sorted.map { |i| [i[item_id_field], i] }.to_h
  end
end

Private Class Methods

sort_collection_values(endpoint_info, values) click to toggle source
# File lib/team_api/collection_canonicalizer.rb, line 20
def self.sort_collection_values(endpoint_info, values)
  sort_by_field = endpoint_info['sort_by']
  if sort_by_field == 'last_name'
    NameCanonicalizer.sort_by_last_name values
  else
    values.sort_by { |i| (i[sort_by_field] || '').downcase }
  end
end
sort_item_xrefs(endpoint_info, collection) click to toggle source
# File lib/team_api/collection_canonicalizer.rb, line 30
def self.sort_item_xrefs(endpoint_info, collection)
  collection.each do |item|
    sortable_item_fields(item, endpoint_info).each do |field, field_info|
      item[field] = sort_collection_values field_info, item[field]
    end
  end
end
sortable_item_fields(item, collection_endpoint_info) click to toggle source
# File lib/team_api/collection_canonicalizer.rb, line 39
def self.sortable_item_fields(item, collection_endpoint_info)
  collection_endpoint_info['item_collections'].map do |item_spec|
    field, endpoint_info = parse_collection_spec item_spec
    [field, endpoint_info] if item[field]
  end.compact
end