Class: ActiveFedora::Aggregation::ProxyContainer

Inherits:
Base
  • Object
show all
Defined in:
lib/active_fedora/aggregation/proxy_container.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#parentObject



13
14
15
# File 'lib/active_fedora/aggregation/proxy_container.rb', line 13

def parent
  @parent || raise("Parent hasn't been set on #{self.class}")
end

Class Method Details

.find_or_initialize(id) ⇒ Object



91
92
93
94
95
# File 'lib/active_fedora/aggregation/proxy_container.rb', line 91

def self.find_or_initialize(id)
  find(id)
rescue ActiveFedora::ObjectNotFoundError
  new(id)
end

Instance Method Details

#<<(obj) ⇒ Object

Parameters:

  • obj (ActiveFedora::Base)


72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/active_fedora/aggregation/proxy_container.rb', line 72

def << (obj)
  node = if persisted?
           parent.proxies.create(id: mint_proxy_id, target: obj, prev: parent.tail)
         else
           parent.proxies.build(id: mint_proxy_id, target: obj, prev: parent.tail)
         end
  # set the old tail, if present, to have this new proxy as its next
  parent.tail.update(next: node) if parent.tail
  # update the tail to point at the new node
  parent.tail = node
  # if this is the first node, set it to be the head
  parent.head = node unless parent.head
  reset_target!
end

#build_proxies(objects) ⇒ Object

TODO clear out the old proxies (or reuse them)



52
53
54
55
56
57
# File 'lib/active_fedora/aggregation/proxy_container.rb', line 52

def build_proxies(objects)
  # need to create the proxies before we can add the links otherwise the linked to resource won't exist
  objects.map do |object|
    Proxy.create(id: mint_proxy_id, target: object)
  end
end

#build_proxies_with_ids(object_ids) ⇒ Object

TODO clear out the old proxies (or reuse them)



60
61
62
63
64
65
# File 'lib/active_fedora/aggregation/proxy_container.rb', line 60

def build_proxies_with_ids(object_ids)
  # need to create the proxies before we can add the links otherwise the linked to resource won't exist
  object_ids.map do |file_id|
    Proxy.create(id: mint_proxy_id, target_id: file_id)
  end
end

#default_relationsObject



17
18
19
20
# File 'lib/active_fedora/aggregation/proxy_container.rb', line 17

def default_relations
  self.member_relation = [::RDF::URI.new("http://pcdm.org/hasMember")] # TODO wrong predicate!
  self.inserted_content_relation = [::RDF::Vocab::ORE.proxyFor]
end

#firstObject



22
23
24
# File 'lib/active_fedora/aggregation/proxy_container.rb', line 22

def first
  parent.head.target
end

Set the links on the nodes in the list



40
41
42
43
44
45
46
47
48
49
# File 'lib/active_fedora/aggregation/proxy_container.rb', line 40

def link_target(new_proxies)
  new_proxies.each_with_index do |proxy, idx|
    proxy.next_id = new_proxies[idx+1].id unless new_proxies.length - 1 <= idx
    proxy.prev_id = new_proxies[idx-1].id unless idx == 0
  end

  parent.head = new_proxies.first
  parent.tail = new_proxies.last
  parent.proxies = new_proxies
end

#list_of_proxiesObject

return the proxies in order



103
104
105
106
107
108
109
# File 'lib/active_fedora/aggregation/proxy_container.rb', line 103

def list_of_proxies
  @proxy_list ||= if parent.head
    parent.head.as_list
  else
    []
  end
end

#mint_proxy_idObject



87
88
89
# File 'lib/active_fedora/aggregation/proxy_container.rb', line 87

def mint_proxy_id
  "#{id}/#{SecureRandom.uuid}"
end

#reset_target!Object



97
98
99
100
# File 'lib/active_fedora/aggregation/proxy_container.rb', line 97

def reset_target!
  @proxy_list = nil
  @target = nil
end

#target=(collection) ⇒ Object



31
32
33
# File 'lib/active_fedora/aggregation/proxy_container.rb', line 31

def target= (collection)
  link_target(build_proxies(collection))
end

#target_idsObject



67
68
69
# File 'lib/active_fedora/aggregation/proxy_container.rb', line 67

def target_ids
  list_of_proxies.map(&:target_id)
end

#target_ids=(object_ids) ⇒ Object



35
36
37
# File 'lib/active_fedora/aggregation/proxy_container.rb', line 35

def target_ids=(object_ids)
  link_target(build_proxies_with_ids(object_ids))
end

#to_aObject

This can be a very expensive operation. avoid if possible



27
28
29
# File 'lib/active_fedora/aggregation/proxy_container.rb', line 27

def to_a
  @target ||= list_of_proxies.map(&:target)
end