Class: Wings::AttributeTransformer

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

Overview

Transform AF attributes to Valkyrie::Resource attributes representation.

Class Method Summary collapse

Class Method Details

.for(obj) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/wings/attribute_transformer.rb', line 9

def self.for(obj)
  case obj
  when Hydra::PCDM::File
    FileAttributeTransformer.new
  else
    self
  end
end

.run(obj) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/wings/attribute_transformer.rb', line 18

def self.run(obj) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
  attrs = obj.reflections.each_with_object({}) do |(key, reflection), mem|
    case reflection
    when ActiveFedora::Reflection::HasManyReflection,
         ActiveFedora::Reflection::HasAndBelongsToManyReflection,
         ActiveFedora::Reflection::IndirectlyContainsReflection
      mem[:"#{key.to_s.singularize}_ids"] =
        obj.association(key).ids_reader
    when ActiveFedora::Reflection::DirectlyContainsReflection
      mem[:"#{key.to_s.singularize}_ids"] =
        Array(obj.public_send(reflection.name)).map(&:id)
    when ActiveFedora::Reflection::FilterReflection,
         ActiveFedora::Reflection::OrdersReflection,
         ActiveFedora::Reflection::HasSubresourceReflection,
         ActiveFedora::Reflection::BelongsToReflection,
         ActiveFedora::Reflection::BasicContainsReflection
      :noop
    when ActiveFedora::Reflection::DirectlyContainsOneReflection
      mem[:"#{key.to_s.singularize}_id"] =
        obj.public_send(reflection.name)&.id
    else
      mem[reflection.foreign_key.to_sym] =
        obj.public_send(reflection.foreign_key.to_sym)
    end
  end

  obj.class.delegated_attributes.keys.each_with_object(attrs) do |attr_name, mem|
    next unless obj.respond_to?(attr_name) && !mem.key?(attr_name.to_sym)
    mem[attr_name.to_sym] = TransformerValueMapper.for(obj.public_send(attr_name)).result
  end
end