metanorma-plugin-glossarist 0.2.2 → 0.2.4

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,133 +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_map = {}
12
-
13
- filtered_concepts(@concepts_collection, filters).each do |concept|
14
- @concepts_map[concept["term"]] = concept
15
- end
16
- end
17
- # rubocop:enable Lint/MissingSuper
18
-
19
- def concepts
20
- @concepts_map
21
- end
22
-
23
- def [](concept_name)
24
- @concepts_map[concept_name]
25
- end
26
-
27
- def each(&block)
28
- @concepts_map.values.each(&block)
29
- end
30
-
31
- private
32
-
33
- def filtered_concepts(concepts_collection, filters)
34
- concept_filters = filters.dup
35
- language_filter = concept_filters.delete("lang")
36
- sort_filter = concept_filters.delete("sort_by")
37
- group_filter = concept_filters.delete("group")
38
-
39
- concepts = concepts_collection.map do |concept|
40
- filtered_concept = concept.to_h["data"]
41
- filtered_concept["term"] = concept.default_designation
42
-
43
- filtered_concept = filtered_concept.merge(
44
- extract_localized_concepts(concept, language_filter),
45
- )
46
-
47
- if retain_concept?(filtered_concept, concept_filters)
48
- filtered_concept
49
- end
50
- end.compact
51
-
52
- apply_group_filter(concepts, group_filter)
53
- apply_sort_filter(concepts, sort_filter)
54
- end
55
-
56
- def extract_localized_concepts(concept, languages)
57
- localized_concepts = {}
58
-
59
- if !languages || languages.empty?
60
- concept.localized_concepts.each do |lang, _localized_concept_uuid|
61
- localized_concepts[lang] = concept.localizations[lang].to_h["data"]
62
- end
63
- else
64
- languages.split(",").each do |lang|
65
- localization = concept.localizations[lang]&.to_h&.dig("data") and
66
- localized_concepts[lang] = localization
67
- end
68
- end
69
-
70
- localized_concepts
71
- end
72
-
73
- def retain_concept?(filtered_concept, concept_filters)
74
- concept_filters.each do |name, value|
75
- fields = extract_nested_field_names(name)
76
- if fields.last.start_with?("start_with")
77
- value = fields.last.gsub(/start_with\(([^\)]*)\)/, '\1')
78
- fields = fields[0..-2]
79
- filtered_concept.dig(*fields).start_with?(value) or
80
- return false
81
- elsif filtered_concept.dig(*fields) != value
82
- return false
83
- end
84
- end
85
-
86
- filtered_concept.keys & NON_LANGUAGE_FIELDS != filtered_concept.keys
87
- end
88
-
89
- def apply_sort_filter(concepts, sort_by)
90
- return concepts unless sort_by
91
-
92
- concepts.sort_by do |concept|
93
- concept.dig(*extract_nested_field_names(sort_by))
94
- end
95
- end
96
-
97
- def apply_group_filter(concepts, groups)
98
- return concepts unless groups
99
-
100
- concepts.select! do |concept|
101
- groups.split(",").reduce(true) do |pre_result, group|
102
- pre_result && concept["groups"].include?(group.strip)
103
- end
104
- end
105
- end
106
-
107
- def extract_nested_field_names(name)
108
- name.split(".").map do |field|
109
- field_name = field.strip
110
-
111
- /^\d+$/.match?(field_name) ? field_name.to_i : field_name
112
- end
113
- end
114
-
115
- def allowed_language?(language, lang_filter)
116
- return false if NON_LANGUAGE_FIELDS.include?(language)
117
- return true unless lang_filter
118
-
119
- language&.strip == lang_filter&.strip
120
- end
121
-
122
- def except(hash, keys)
123
- dup_hash = hash.dup
124
-
125
- keys.each do |key|
126
- dup_hash.delete(key)
127
- end
128
-
129
- dup_hash
130
- end
131
- end
132
- end
133
- end