Sha256: cef4a12d795814aec3b3f219d004bc164bc68ad2e918c19e57a63815fa3440b0

Contents?: true

Size: 1.23 KB

Versions: 27

Compression:

Stored size: 1.23 KB

Contents

# (c) Copyright 2020 Ribose Inc.
#

module Jekyll
  module Geolexica
    # A decorator responsible for serializing concepts in the most simplistic
    # machine-readable formats like JSON or YAML but unlike RDF ontologies.
    class ConceptSerializer < SimpleDelegator
      include Configuration

      NON_LANGUAGE_KEYS = %w[term termid]

      # A Jekyll::Site instance.
      attr_reader :site

      def initialize(concept, site)
        super(concept)
        @site = site
      end

      def to_json
        JSON.dump(in_all_languages)
      end

      def to_yaml
        YAML.dump(in_all_languages)
      end

      private

      # Returns concept hash in all supported languages, with +nil+ value for
      # every supported language that this concept is not translated to.
      def in_all_languages(concept_hash = data)
        hash_keys = NON_LANGUAGE_KEYS + term_languages
        slice_hash_with_default(concept_hash, nil, *hash_keys)
      end

      # Like Hash#slice, but takes a +default_value+, which is used for every
      # key not present in the +hash+.
      def slice_hash_with_default(hash, default_value, *keys)
        h = hash.dup
        keys.each { |k| h[k] ||= default_value }
        h.slice(*keys)
      end
    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
jekyll-geolexica-1.9.6 lib/jekyll/geolexica/concept_serializer.rb
jekyll-geolexica-1.9.5 lib/jekyll/geolexica/concept_serializer.rb
jekyll-geolexica-1.9.4 lib/jekyll/geolexica/concept_serializer.rb
jekyll-geolexica-1.9.3 lib/jekyll/geolexica/concept_serializer.rb
jekyll-geolexica-1.9.2 lib/jekyll/geolexica/concept_serializer.rb
jekyll-geolexica-1.9.1 lib/jekyll/geolexica/concept_serializer.rb
jekyll-geolexica-1.9.0 lib/jekyll/geolexica/concept_serializer.rb
jekyll-geolexica-1.8.15 lib/jekyll/geolexica/concept_serializer.rb
jekyll-geolexica-1.8.14 lib/jekyll/geolexica/concept_serializer.rb
jekyll-geolexica-1.8.13 lib/jekyll/geolexica/concept_serializer.rb
jekyll-geolexica-1.8.12 lib/jekyll/geolexica/concept_serializer.rb
jekyll-geolexica-1.8.11 lib/jekyll/geolexica/concept_serializer.rb
jekyll-geolexica-1.8.10 lib/jekyll/geolexica/concept_serializer.rb
jekyll-geolexica-1.8.9 lib/jekyll/geolexica/concept_serializer.rb
jekyll-geolexica-1.8.8 lib/jekyll/geolexica/concept_serializer.rb
jekyll-geolexica-1.8.7 lib/jekyll/geolexica/concept_serializer.rb
jekyll-geolexica-1.8.6 lib/jekyll/geolexica/concept_serializer.rb
jekyll-geolexica-1.8.5 lib/jekyll/geolexica/concept_serializer.rb
jekyll-geolexica-1.8.4 lib/jekyll/geolexica/concept_serializer.rb
jekyll-geolexica-1.8.3 lib/jekyll/geolexica/concept_serializer.rb