Class: Metanorma::Plugin::Glossarist::Liquid::CustomBlocks::WithGlossaristContext
- Inherits:
-
Liquid::Block
- Object
- Liquid::Block
- Metanorma::Plugin::Glossarist::Liquid::CustomBlocks::WithGlossaristContext
- Defined in:
- lib/metanorma/plugin/glossarist/liquid/custom_blocks/with_glossarist_context.rb
Instance Method Summary collapse
-
#apply_field_filter(collection, field_filter) ⇒ Object
rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity.
- #apply_group_filter(collection, group_filter) ⇒ Object
- #apply_lang_filter(collection, lang_filter) ⇒ Object
- #apply_sort_filter(collection, sort_filter) ⇒ Object
- #filter_collection(collection, filters) ⇒ Object
-
#initialize(tag_name, markup, tokens) ⇒ WithGlossaristContext
constructor
rubocop:disable Metrics/AbcSize,Metrics/MethodLength.
- #load_collection(folder_path) ⇒ Object
- #render(context) ⇒ Object
Constructor Details
#initialize(tag_name, markup, tokens) ⇒ WithGlossaristContext
rubocop:disable Metrics/AbcSize,Metrics/MethodLength
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/metanorma/plugin/glossarist/liquid/custom_blocks/with_glossarist_context.rb', line 11 def initialize(tag_name, markup, tokens) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength super @contexts = [] @filters = {} contexts, filters = markup.strip.split(";", 2) if filters && !filters.empty? filters = filters.strip.gsub(/^['"]|['"]$/, "").split(";") filters.each do |filter| property, value = filter.split("=") @filters[property] = value end end contexts.split(",").each do |context| context_name, file_path = context.split("=").map(&:strip) @contexts << { name: context_name, file_path: file_path, } end end |
Instance Method Details
#apply_field_filter(collection, field_filter) ⇒ Object
rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/metanorma/plugin/glossarist/liquid/custom_blocks/with_glossarist_context.rb', line 61 def apply_field_filter(collection, field_filter) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity return collection if field_filter.nil? || field_filter.empty? collection.select do |obj| # rubocop:disable Metrics/BlockLength fields = field_filter.keys.first.split(".") value = field_filter.values.first start_with = false # check if the last field is a start_with condition if fields.last.start_with?("start_with(") && fields.last.include?(")") start_with = true # Extract content between first '(' and last ')' f = fields.last value = f[(f.index("(") + 1)...f.rindex(")")] fields = fields[0..-2] end fields.each do |field| # contain index (i.e. field['abc'] or field[1]) if field.include?("[") field, index = field[0..-2].split("[") index = if index.include?("'") || index.include?("\"") index.gsub(/['"]/, "") else index.to_i end obj = obj.send(field.to_sym)[index] else obj = obj.send(field.to_sym) end end # check if the object matches the value if start_with obj.start_with?(value) else obj == value end end end |
#apply_group_filter(collection, group_filter) ⇒ Object
113 114 115 116 117 118 119 |
# File 'lib/metanorma/plugin/glossarist/liquid/custom_blocks/with_glossarist_context.rb', line 113 def apply_group_filter(collection, group_filter) return collection unless group_filter collection.select do |concept| concept.data.groups&.include?(group_filter) end end |
#apply_lang_filter(collection, lang_filter) ⇒ Object
105 106 107 108 109 110 111 |
# File 'lib/metanorma/plugin/glossarist/liquid/custom_blocks/with_glossarist_context.rb', line 105 def apply_lang_filter(collection, lang_filter) return collection unless lang_filter collection.select do |concept| concept.data.localizations.key?(lang_filter) end end |
#apply_sort_filter(collection, sort_filter) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/metanorma/plugin/glossarist/liquid/custom_blocks/with_glossarist_context.rb', line 121 def apply_sort_filter(collection, sort_filter) return collection unless sort_filter sort_filter = case sort_filter when "term" "default_designation" else sort_filter end collection.sort_by do |concept| concept.send(sort_filter.to_sym).downcase end end |
#filter_collection(collection, filters) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/metanorma/plugin/glossarist/liquid/custom_blocks/with_glossarist_context.rb', line 47 def filter_collection(collection, filters) return collection unless filters concept_filters = filters.dup lang_filter = concept_filters.delete("lang") group_filter = concept_filters.delete("group") sort_filter = concept_filters.delete("sort_by") collection = apply_lang_filter(collection, lang_filter) collection = apply_group_filter(collection, group_filter) collection = apply_field_filter(collection, concept_filters) apply_sort_filter(collection, sort_filter) end |
#load_collection(folder_path) ⇒ Object
37 38 39 40 41 42 43 44 45 |
# File 'lib/metanorma/plugin/glossarist/liquid/custom_blocks/with_glossarist_context.rb', line 37 def load_collection(folder_path) @@collections ||= {} return @@collections[folder_path] if @@collections[folder_path] collection = ::Glossarist::ManagedConceptCollection.new collection.load_from_files(folder_path) @@collections[folder_path] = collection end |
#render(context) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/metanorma/plugin/glossarist/liquid/custom_blocks/with_glossarist_context.rb', line 136 def render(context) @contexts.each do |local_context| context_file = local_context[:file_path].strip collection = load_collection(context_file) filtered_collection = filter_collection(collection, @filters) context[local_context[:name]] = filtered_collection .map(&:to_liquid) end super end |