metanorma-plugin-glossarist 0.2.3 → 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,129 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Liquid
4
- module Drops
5
- class ConceptsDrop < Liquid::Drop
6
- NON_LANGUAGE_FIELDS = %w[identifier localized_concepts groups term].freeze
7
-
8
- # rubocop:disable Lint/MissingSuper
9
- def initialize(managed_concept_collection, filters = {})
10
- @concepts_collection = managed_concept_collection
11
- @concepts = filtered_concepts(@concepts_collection, filters)
12
- end
13
- # rubocop:enable Lint/MissingSuper
14
-
15
- def concepts
16
- @concepts
17
- end
18
-
19
- def [](concept_name)
20
- @concepts.find { |c| c["term"] == concept_name }
21
- end
22
-
23
- def each(&block)
24
- @concepts.each(&block)
25
- end
26
-
27
- private
28
-
29
- def filtered_concepts(concepts_collection, filters)
30
- concept_filters = filters.dup
31
- language_filter = concept_filters.delete("lang")
32
- sort_filter = concept_filters.delete("sort_by")
33
- group_filter = concept_filters.delete("group")
34
-
35
- concepts = concepts_collection.map do |concept|
36
- filtered_concept = concept.to_h["data"]
37
- filtered_concept["term"] = concept.default_designation
38
-
39
- filtered_concept = filtered_concept.merge(
40
- extract_localized_concepts(concept, language_filter),
41
- )
42
-
43
- if retain_concept?(filtered_concept, concept_filters)
44
- filtered_concept
45
- end
46
- end.compact
47
-
48
- apply_group_filter(concepts, group_filter)
49
- apply_sort_filter(concepts, sort_filter)
50
- end
51
-
52
- def extract_localized_concepts(concept, languages)
53
- localized_concepts = {}
54
-
55
- if !languages || languages.empty?
56
- concept.localized_concepts.each do |lang, _localized_concept_uuid|
57
- localized_concepts[lang] = concept.localizations[lang].to_h["data"]
58
- end
59
- else
60
- languages.split(",").each do |lang|
61
- localization = concept.localizations[lang]&.to_h&.dig("data") and
62
- localized_concepts[lang] = localization
63
- end
64
- end
65
-
66
- localized_concepts
67
- end
68
-
69
- def retain_concept?(filtered_concept, concept_filters)
70
- concept_filters.each do |name, value|
71
- fields = extract_nested_field_names(name)
72
- if fields.last.start_with?("start_with")
73
- value = fields.last.gsub(/start_with\(([^\)]*)\)/, '\1')
74
- fields = fields[0..-2]
75
- filtered_concept.dig(*fields).start_with?(value) or
76
- return false
77
- elsif filtered_concept.dig(*fields) != value
78
- return false
79
- end
80
- end
81
-
82
- filtered_concept.keys & NON_LANGUAGE_FIELDS != filtered_concept.keys
83
- end
84
-
85
- def apply_sort_filter(concepts, sort_by)
86
- return concepts unless sort_by
87
-
88
- concepts.sort_by do |concept|
89
- concept.dig(*extract_nested_field_names(sort_by))
90
- end
91
- end
92
-
93
- def apply_group_filter(concepts, groups)
94
- return concepts unless groups
95
-
96
- concepts.select! do |concept|
97
- groups.split(",").reduce(true) do |pre_result, group|
98
- pre_result && concept["groups"].include?(group.strip)
99
- end
100
- end
101
- end
102
-
103
- def extract_nested_field_names(name)
104
- name.split(".").map do |field|
105
- field_name = field.strip
106
-
107
- /^\d+$/.match?(field_name) ? field_name.to_i : field_name
108
- end
109
- end
110
-
111
- def allowed_language?(language, lang_filter)
112
- return false if NON_LANGUAGE_FIELDS.include?(language)
113
- return true unless lang_filter
114
-
115
- language&.strip == lang_filter&.strip
116
- end
117
-
118
- def except(hash, keys)
119
- dup_hash = hash.dup
120
-
121
- keys.each do |key|
122
- dup_hash.delete(key)
123
- end
124
-
125
- dup_hash
126
- end
127
- end
128
- end
129
- end