Class: ActiveFedora::Aggregation::Aggregator

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_or_initialize(id) ⇒ Object



76
77
78
79
80
# File 'lib/active_fedora/aggregation/aggregator.rb', line 76

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

Instance Method Details

#<<(obj) ⇒ Object

Parameters:

  • obj (ActiveFedora::Base)


57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/active_fedora/aggregation/aggregator.rb', line 57

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

#build_proxies(objects) ⇒ Object

TODO clear out the old proxies (or reuse them)



37
38
39
40
41
42
# File 'lib/active_fedora/aggregation/aggregator.rb', line 37

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)



45
46
47
48
49
50
# File 'lib/active_fedora/aggregation/aggregator.rb', line 45

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

#firstObject



7
8
9
# File 'lib/active_fedora/aggregation/aggregator.rb', line 7

def first
  head.target
end

Set the links on the nodes in the list



25
26
27
28
29
30
31
32
33
34
# File 'lib/active_fedora/aggregation/aggregator.rb', line 25

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

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

#list_of_proxiesObject

return the proxies in order



88
89
90
91
92
93
94
# File 'lib/active_fedora/aggregation/aggregator.rb', line 88

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

#mint_proxy_idObject



72
73
74
# File 'lib/active_fedora/aggregation/aggregator.rb', line 72

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

#reset_target!Object



82
83
84
85
# File 'lib/active_fedora/aggregation/aggregator.rb', line 82

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

#target=(collection) ⇒ Object



16
17
18
# File 'lib/active_fedora/aggregation/aggregator.rb', line 16

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

#target_idsObject



52
53
54
# File 'lib/active_fedora/aggregation/aggregator.rb', line 52

def target_ids
  list_of_proxies.map(&:target_id)
end

#target_ids=(object_ids) ⇒ Object



20
21
22
# File 'lib/active_fedora/aggregation/aggregator.rb', line 20

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



12
13
14
# File 'lib/active_fedora/aggregation/aggregator.rb', line 12

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