Sha256: 95d79bb04fc5126d16e5f0e6ef0c1b12459e813b43896b718aed9bd2bc3e44af

Contents?: true

Size: 1.35 KB

Versions: 11

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true

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

# Help Zeitwerk find some of our classes
class CocinaModelsInflector < Zeitwerk::Inflector
  def camelize(basename, _abspath)
    case basename
    when 'dro'
      'DRO'
    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

    # @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

11 entries across 11 versions & 1 rubygems

Version Path
cocina-models-0.16.0 lib/cocina/models.rb
cocina-models-0.15.0 lib/cocina/models.rb
cocina-models-0.14.0 lib/cocina/models.rb
cocina-models-0.13.0 lib/cocina/models.rb
cocina-models-0.12.0 lib/cocina/models.rb
cocina-models-0.11.0 lib/cocina/models.rb
cocina-models-0.10.0 lib/cocina/models.rb
cocina-models-0.9.0 lib/cocina/models.rb
cocina-models-0.8.0 lib/cocina/models.rb
cocina-models-0.7.0 lib/cocina/models.rb
cocina-models-0.6.0 lib/cocina/models.rb