Class: Wings::OrmConverter Private

Inherits:
Object
  • Object
show all
Defined in:
lib/wings/orm_converter.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Transform AF object class to Valkyrie::Resource class representation.

Class Method Summary collapse

Class Method Details

.base_for(klass:) ⇒ Class

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Selects an existing base class for the generated valkyrie class

Returns:

  • (Class)


13
14
15
16
17
# File 'lib/wings/orm_converter.rb', line 13

def self.base_for(klass:)
  mapped_class = klass.try(:valkyrie_class) || ModelRegistry.reverse_lookup(klass)
  return mapped_class if mapped_class
  klass < Hydra::Works::WorkBehavior ? Hyrax::Work : Hyrax::Resource
end

.to_valkyrie_resource_class(klass:) ⇒ Class

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

rubocop:disable Metrics/AbcSize rubocop:disable Metrics/BlockLength rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity rubocop:disable Metrics/MethodLength because metaprogramming a class

results in long methods

Parameters:

  • klass (Class)

    an ‘ActiveFedora` model class

Returns:

  • (Class)

    a dyamically generated ‘Valkyrie::Resource` subclass mirroring the provided `ActiveFedora` model



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/wings/orm_converter.rb', line 31

def self.to_valkyrie_resource_class(klass:)
  Class.new(base_for(klass: klass)) do
    # store a string we can resolve to the internal resource
    @internal_resource = klass.try(:to_rdf_representation) || klass.name

    class << self
      attr_reader :internal_resource

      def name
        _canonical_valkyrie_model&.name
      end

      def to_rdf_representation
        name
      end

      ##
      # @return [String]
      def to_s
        internal_resource
      end

      ##
      # @api private
      def _canonical_valkyrie_model
        ancestors[1..-1].find { |parent| parent < ::Valkyrie::Resource }
      end
    end

    ##
    # @return [URI::GID]
    def to_global_id
      URI::GID.build([GlobalID.app, internal_resource, id, {}])
    end

    ##
    # @return [ActiveModel::Base]
    def to_model
      model_class = internal_resource.safe_constantize || self

      Hyrax::ActiveFedoraDummyModel.new(model_class, id)
    end

    klass.properties.each_key do |property_name|
      next if fields.include?(property_name.to_sym)

      if klass.properties[property_name].multiple?
        attribute property_name.to_sym, ::Valkyrie::Types::Set.of(::Valkyrie::Types::Anything).optional
      else
        attribute property_name.to_sym, ::Valkyrie::Types::Anything.optional
      end
    end

    # add reflection associations
    (klass.try(:reflections) || []).each do |reflection_key, reflection|
      case reflection
      when ActiveFedora::Reflection::BelongsToReflection, # uses foreign_key SingularRDFPropertyReflection
           ActiveFedora::Reflection::BasicContainsReflection, # ???
           ActiveFedora::Reflection::FilterReflection, # rely on :extending_from
           ActiveFedora::Reflection::HasAndBelongsToManyReflection, # uses foreign_key RDFPropertyReflection
           ActiveFedora::Reflection::HasManyReflection, # captured by inverse relation
           ActiveFedora::Reflection::HasSubresourceReflection, # ???
           ActiveFedora::Reflection::OrdersReflection # map to :unordered_association in Wings::AttributeTransformer (but retain order)
        next
      when ActiveFedora::Reflection::DirectlyContainsOneReflection
        attribute_name = (reflection_key.to_s.singularize + '_id').to_sym
        type = ::Valkyrie::Types::ID.optional
      when ActiveFedora::Reflection::DirectlyContainsReflection,
           ActiveFedora::Reflection::IndirectlyContainsReflection
        attribute_name = (reflection_key.to_s.singularize + '_ids').to_sym
        type = ::Valkyrie::Types::Set.of(::Valkyrie::Types::ID)
      when ActiveFedora::Reflection::SingularRDFPropertyReflection
        attribute_name = reflection.name.to_sym
        type = ::Valkyrie::Types::ID.optional
      when ActiveFedora::Reflection::RDFPropertyReflection
        attribute_name = reflection.name.to_sym
        type = ::Valkyrie::Types::Set.of(::Valkyrie::Types::ID)
      else
        raise NotImplementedError, "Expected a known ActiveFedora::Reflection, but got #{reflection}"
      end

      attribute(attribute_name, type)
    end

    def internal_resource
      self.class.internal_resource
    end
  end
end