Module: SortNameProcessor::Utils

Defined in:
backend/app/model/mixins/sort_name_processor.rb

Class Method Summary collapse

Class Method Details

.first_date(data, date_type) ⇒ Object



3
4
5
6
7
# File 'backend/app/model/mixins/sort_name_processor.rb', line 3

def self.first_date(data, date_type)
  return nil unless data[date_type] && data[date_type]&.count.positive?

  stringify_date(data[date_type][0])
end

.stringify_date(date_json) ⇒ Object

processing in pseudocode if date expression return date expression else if begin and end date return begin date year - end date year else return begin date year



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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'backend/app/model/mixins/sort_name_processor.rb', line 20

def self.stringify_date(date_json)
  date_substring = ""

  if date_json["date_type_structured"] == "single"
    std = date_json["structured_date_single"]['date_standardized']
    exp = date_json["structured_date_single"]['date_expression']

    # only grab the year
    std = std.split("-")[0] unless std.nil?

    if exp
      date_substring = exp
    elsif std
      s_type = date_json["structured_date_single"]["date_standardized_type"]
      date_substring = std
      date_substring = "#{I18n.t('enumerations.date_standardized_type.' + s_type)} #{date_substring}" unless s_type == "standard"
    end
  elsif date_json["date_type_structured"] == "range"
    b_std = date_json["structured_date_range"]['begin_date_standardized']
    b_exp = date_json["structured_date_range"]['begin_date_expression']
    e_std = date_json["structured_date_range"]['end_date_standardized']
    e_exp = date_json["structured_date_range"]['end_date_expression']

    b_s_type = date_json["structured_date_range"]["begin_date_standardized_type"]
    e_s_type = date_json["structured_date_range"]["end_date_standardized_type"]

    # only grab the years
    b_std = b_std.split("-")[0] unless b_std.nil?
    e_std = e_std.split("-")[0] unless e_std.nil?

    if b_exp && e_exp
      date_substring = b_exp + "-" + e_exp
    elsif b_exp
      date_substring = b_exp
    elsif b_std && e_std
      b_std = "#{I18n.t('enumerations.date_standardized_type.' + b_s_type)} #{b_std}" unless b_s_type == "standard"
      e_std = "#{I18n.t('enumerations.date_standardized_type.' + e_s_type)} #{e_std}" unless e_s_type == "standard"
      date_substring = b_std + "-" + e_std
    elsif b_std
      b_std = "#{I18n.t('enumerations.date_standardized_type.' + b_s_type)} #{b_std}" unless b_s_type == "standard"
      date_substring = b_std
    end
  end

  return date_substring
end