Class: ValkyrieActiveFedora::ResourceFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/valkyrie_active_fedora/resource_factory.rb

Overview

we really want a class var here. maybe we could use a singleton instead? rubocop:disable Style/ClassVars

Defined Under Namespace

Classes: ResourceClassCache

Constant Summary collapse

@@resource_class_cache =
ResourceClassCache.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(active_fedora_object:) ⇒ ResourceFactory

Returns a new instance of ResourceFactory.



25
26
27
# File 'lib/valkyrie_active_fedora/resource_factory.rb', line 25

def initialize(active_fedora_object:)
  self.active_fedora_object = active_fedora_object
end

Instance Attribute Details

#active_fedora_objectObject

Returns the value of attribute active_fedora_object.



23
24
25
# File 'lib/valkyrie_active_fedora/resource_factory.rb', line 23

def active_fedora_object
  @active_fedora_object
end

Class Method Details

.for(active_fedora_object) ⇒ Object



29
30
31
# File 'lib/valkyrie_active_fedora/resource_factory.rb', line 29

def self.for(active_fedora_object)
  new(active_fedora_object: active_fedora_object).build
end

Instance Method Details

#buildObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/valkyrie_active_fedora/resource_factory.rb', line 33

def build
  klass = @@resource_class_cache.fetch(active_fedora_object) do
    # we need a local binding to the object for use in the class scope below
    active_fedora_local = active_fedora_object

    Class.new(::Valkyrie::Resource) do
      active_fedora_local.send(:properties).each_key do |property_name|
        attribute property_name.to_sym, ::Valkyrie::Types::String
      end
    end
  end

  klass.new(id: active_fedora_object.id, **active_fedora_object.attributes.symbolize_keys)
end