Sha256: 6682ac560e52a35df8f9f34ba2384eb04e548b204b2bc4a4daca2daf7995b308

Contents?: true

Size: 1.29 KB

Versions: 27

Compression:

Stored size: 1.29 KB

Contents

require "glossarist"

module Stepmod
  module Utils
    class Term < Glossarist::LocalizedConcept
      # Term acronym
      attr_accessor :acronym

      def to_mn_adoc
        mn_adoc = ["=== #{definition.map(&:content).join}"]
        mn_adoc << "\nalt:[#{acronym}]" if acronym
        mn_adoc << "\n\n#{designations.map(&:designation).join(", ")}" if designations&.any?

        mn_adoc.join
      end

      class << self
        def from_h(hash)
          _, definition, acronym = treat_acronym(hash["definition"])

          hash["definition"] = [definition]

          hash["acronym"] = acronym.gsub(/\(|\)/, "") if acronym
          add_designations(hash, hash["synonyms"]) if hash["synonyms"]

          new(hash.reject { |k, _| k == "synonyms" })
        end

        private

        def add_designations(hash, synonyms)
          hash["designations"] ||= []
          hash["designations"] << designation_hash(synonyms) if synonyms
        end

        def designation_hash(value, type = "expression")
          {
            "designation" => value,
            "type" => type,
          }
        end

        def treat_acronym(term_def)
          return [nil, term_def.strip, nil] unless term_def.match?(/.+\(.+?\)$/)

          term_def.match(/(.+?)(\(.+\))$/).to_a
        end
      end
    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
stepmod-utils-0.4.14 lib/stepmod/utils/term.rb
stepmod-utils-0.4.13 lib/stepmod/utils/term.rb
stepmod-utils-0.4.12 lib/stepmod/utils/term.rb
stepmod-utils-0.4.11 lib/stepmod/utils/term.rb
stepmod-utils-0.4.10 lib/stepmod/utils/term.rb
stepmod-utils-0.4.9 lib/stepmod/utils/term.rb
stepmod-utils-0.4.8 lib/stepmod/utils/term.rb
stepmod-utils-0.4.7 lib/stepmod/utils/term.rb
stepmod-utils-0.4.6 lib/stepmod/utils/term.rb
stepmod-utils-0.4.5 lib/stepmod/utils/term.rb
stepmod-utils-0.4.2 lib/stepmod/utils/term.rb
stepmod-utils-0.4.1 lib/stepmod/utils/term.rb
stepmod-utils-0.4.0 lib/stepmod/utils/term.rb
stepmod-utils-0.3.37 lib/stepmod/utils/term.rb
stepmod-utils-0.3.36 lib/stepmod/utils/term.rb
stepmod-utils-0.3.35 lib/stepmod/utils/term.rb
stepmod-utils-0.3.34 lib/stepmod/utils/term.rb
stepmod-utils-0.3.33 lib/stepmod/utils/term.rb
stepmod-utils-0.3.32 lib/stepmod/utils/term.rb
stepmod-utils-0.3.31 lib/stepmod/utils/term.rb