Sha256: 397f75474d4bfb9bfebd866e7a451ab88817af848d5c055bea1af327dbc05482

Contents?: true

Size: 1.46 KB

Versions: 34

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

module Cocina
  module Generator
    # Class for generating a vocab
    class Vocab
      def initialize(schemas)
        @schemas = schemas
      end

      def filename
        'vocab.rb'
      end

      def generate
        <<~RUBY
                    # frozen_string_literal: true

                    module Cocina
                      module Models
                        # A digital repository object.  See http://sul-dlss.github.io/cocina-models/maps/DRO.json
                        class Vocab

          #{vocab_methods}

                        end
                      end
          end
        RUBY
      end

      private

      attr_reader :schemas

      # rubocop:disable Style/MultilineBlockChain
      def vocabs
        schemas.values.map do |schema|
          type_property = schema.properties['type']
          type_property.nil? ? [] : type_property.enum.to_a
        end
               .flatten
               .uniq
               .sort
               .filter { |vocab| vocab.start_with?('http://cocina.sul.stanford.edu/models') }
      end
      # rubocop:enable Style/MultilineBlockChain

      def vocab_methods
        # Note special handling of 3d
        vocabs.map do |vocab|
          name = vocab[38, vocab.size - 45].gsub('-', '_').gsub('3d', 'three_dimensional')
          <<~RUBY
            def self.#{name}
              "#{vocab}"
            end

          RUBY
        end.join("\n")
      end
    end
  end
end

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
cocina-models-0.54.0 lib/cocina/generator/vocab.rb
cocina-models-0.54.0.beta.2 lib/cocina/generator/vocab.rb
cocina-models-0.53.1 lib/cocina/generator/vocab.rb
cocina-models-0.54.0.beta.1 lib/cocina/generator/vocab.rb
cocina-models-0.53.0 lib/cocina/generator/vocab.rb
cocina-models-0.52.0 lib/cocina/generator/vocab.rb
cocina-models-0.51.0 lib/cocina/generator/vocab.rb
cocina-models-0.50.0 lib/cocina/generator/vocab.rb
cocina-models-0.49.0 lib/cocina/generator/vocab.rb
cocina-models-0.48.0 lib/cocina/generator/vocab.rb
cocina-models-0.47.2 lib/cocina/generator/vocab.rb
cocina-models-0.47.1 lib/cocina/generator/vocab.rb
cocina-models-0.47.0 lib/cocina/generator/vocab.rb
cocina-models-0.46.0 lib/cocina/generator/vocab.rb
cocina-models-0.45.0 lib/cocina/generator/vocab.rb
cocina-models-0.44.0 lib/cocina/generator/vocab.rb
cocina-models-0.43.0 lib/cocina/generator/vocab.rb
cocina-models-0.42.1 lib/cocina/generator/vocab.rb
cocina-models-0.42.0 lib/cocina/generator/vocab.rb
cocina-models-0.41.0 lib/cocina/generator/vocab.rb