Sha256: 42b3aa42839f77c1fd090a7b1f8198039ffdd36c9ff3f14171a68c0b423833b2

Contents?: true

Size: 1.55 KB

Versions: 1

Compression:

Stored size: 1.55 KB

Contents

# frozen_string_literal: true

require 'cocina/models/version'
require 'zeitwerk'
require 'dry-struct'
require 'dry-types'
require 'json'

# Help Zeitwerk find some of our classes
class CocinaModelsInflector < Zeitwerk::Inflector
  def camelize(basename, _abspath)
    case basename
    when 'dro'
      'DRO'
    when 'dro_builder'
      'DROBuilder'
    when 'request_dro'
      'RequestDRO'
    when 'version'
      'VERSION'
    else
      super
    end
  end
end

loader = Zeitwerk::Loader.new
loader.inflector = CocinaModelsInflector.new
loader.push_dir(File.absolute_path("#{__FILE__}/../.."))
loader.setup

module Cocina
  # Provides Ruby objects for the repository and serializing them to/from JSON.
  module Models
    class Error < StandardError; end
    # Raised when the type attribute is not valid.
    class UnknownTypeError < Error; end

    # Base class for Cocina Structs
    class Struct < Dry::Struct
      transform_keys(&:to_sym)
    end

    # @param [Hash] dyn a ruby hash representation of the JSON serialization of a collection or DRO
    # @return [DRO,Collection]
    # @raises [UnknownTypeError] if a valid type is not found in the data
    # @raises [KeyError] if a type field cannot be found in the data
    def self.build(dyn)
      case dyn.fetch('type')
      when *DRO::TYPES
        DRO.from_dynamic(dyn)
      when *Collection::TYPES
        Collection.from_dynamic(dyn)
      when *AdminPolicy::TYPES
        AdminPolicy.from_dynamic(dyn)
      else
        raise UnknownTypeError, "Unknown type: '#{dyn.fetch('type')}'"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cocina-models-0.20.0 lib/cocina/models.rb