Class: Qa::LinkedData::LanguageSortService

Inherits:
Object
  • Object
show all
Defined in:
app/services/qa/linked_data/language_sort_service.rb

Constant Summary collapse

LANGUAGE_LOCALE_KEY_FOR_NO_LANGUAGE =
:NO_LANGUAGE

Instance Method Summary collapse

Constructor Details

#initialize(literals, preferred_language = nil) ⇒ Object

Returns instance of this class.

Parameters:

  • string (Array<RDF::Literals>)

    literals to sort

  • preferred (Symbol)

    language to appear first in the list; defaults to no preference



14
15
16
17
18
19
# File 'app/services/qa/linked_data/language_sort_service.rb', line 14

def initialize(literals, preferred_language = nil)
  @literals = literals
  @preferred_language = preferred_language
  @languages = []
  @bins = {}
end

Instance Method Details

#sortArray<RDF::Literals] sorted version of literals

Sort the literals stored in this instance of the service

Returns:

  • (Array<RDF::Literals] sorted version of literals)

    Array<RDF::Literals] sorted version of literals



23
24
25
26
27
28
29
30
# File 'app/services/qa/linked_data/language_sort_service.rb', line 23

def sort
  return literals unless literals.present?
  return @sorted_literals if @sorted_literals.present?
  parse_into_language_bins
  sort_languages
  sort_language_bins
  @sorted_literals = construct_sorted_literals
end

#uniq_sorted_stringsArray<String>

Sort the literals and return as an array of strings with only unique literals and empty strings removed

Returns:

  • (Array<String>)

    sorted version of literals as strings



34
35
36
# File 'app/services/qa/linked_data/language_sort_service.rb', line 34

def uniq_sorted_strings
  sort.map(&:to_s).uniq.delete_if(&:blank?)
end