Class: Bulkrax::FactoryClassFinder

Inherits:
Object
  • Object
show all
Defined in:
app/services/bulkrax/factory_class_finder.rb

Defined Under Namespace

Modules: DefaultCoercer, ValkyrieMigrationCoercer

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entry:, coercer:) ⇒ FactoryClassFinder

Returns a new instance of FactoryClassFinder.



51
52
53
54
# File 'app/services/bulkrax/factory_class_finder.rb', line 51

def initialize(entry:, coercer:)
  @entry = entry
  @coercer = coercer
end

Instance Attribute Details

#coercerObject (readonly)

Returns the value of attribute coercer.



55
56
57
# File 'app/services/bulkrax/factory_class_finder.rb', line 55

def coercer
  @coercer
end

#entryObject (readonly)

Returns the value of attribute entry.



55
56
57
# File 'app/services/bulkrax/factory_class_finder.rb', line 55

def entry
  @entry
end

Class Method Details

.find(entry:, coercer: Bulkrax.factory_class_name_coercer || DefaultCoercer) ⇒ Class

Parameters:

Returns:

  • (Class)


47
48
49
# File 'app/services/bulkrax/factory_class_finder.rb', line 47

def self.find(entry:, coercer: Bulkrax.factory_class_name_coercer || DefaultCoercer)
  new(entry: entry, coercer: coercer).find
end

Instance Method Details

#findClass, Nil

Returns:

  • (Class)

    when we are able to derive the class based on the #name.

  • (Nil)

    when we encounter errors with constantizing the #name.

See Also:



61
62
63
64
65
66
67
# File 'app/services/bulkrax/factory_class_finder.rb', line 61

def find
  coercer.call(name)
rescue NameError
  nil
rescue
  entry.default_work_type.constantize
end

#nameString

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.

Returns:

  • (String)


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/services/bulkrax/factory_class_finder.rb', line 72

def name
  fc = if entry.&.[]('model').present?
         Array.wrap(entry.['model']).first
       elsif entry.importerexporter&.mapping&.[]('work_type').present?
         # Because of delegation's nil guard, we're reaching rather far into the implementation
         # details.
         Array.wrap(entry.['work_type']).first
       else
         entry.default_work_type
       end

  # Let's coerce this into the right shape; we're not mutating the string because it might well
  # be frozen.
  fc = fc.tr(' ', '_')
  fc = fc.downcase if fc.match?(/[-_]/)
  fc.camelcase
rescue
  entry.default_work_type
end