Class: ActiveFedora::Orders::OrderedList

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/active_fedora/orders/ordered_list.rb

Overview

Ruby object representation of an ORE doubly linked list.

Defined Under Namespace

Classes: HeadSentinel, NodeCache, Sentinel, TailSentinel

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(graph, head_subject, tail_subject, node_cache: NodeCache.new) ⇒ OrderedList

Returns a new instance of OrderedList.

Parameters:

  • graph (::RDF::Enumerable)

    Enumerable where ORE statements are stored.

  • head_subject (::RDF::URI)

    URI of head node in list.

  • tail_subject (::RDF::URI)

    URI of tail node in list.



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/active_fedora/orders/ordered_list.rb', line 15

def initialize(graph, head_subject, tail_subject, node_cache: NodeCache.new)
  @graph = if graph.respond_to?(:graph)
             graph.graph.data
           else
             graph
           end
  @head_subject = head_subject
  @tail_subject = tail_subject
  @node_cache = node_cache
  @changed = false
  tail
end

Instance Attribute Details

#graphObject (readonly)

Returns the value of attribute graph.



7
8
9
# File 'lib/active_fedora/orders/ordered_list.rb', line 7

def graph
  @graph
end

#headHeadSentinel

Returns Sentinel for the top of the list. If not empty, head.next is the first element.

Returns:

  • (HeadSentinel)

    Sentinel for the top of the list. If not empty, head.next is the first element.



30
31
32
# File 'lib/active_fedora/orders/ordered_list.rb', line 30

def head
  @head ||= HeadSentinel.new(self, next_node: build_node(head_subject))
end

#head_subjectObject (readonly)

Returns the value of attribute head_subject.



7
8
9
# File 'lib/active_fedora/orders/ordered_list.rb', line 7

def head_subject
  @head_subject
end

#tailTailSentinel

Returns Sentinel for the bottom of the list. If not empty, tail.prev is the first element.

Returns:

  • (TailSentinel)

    Sentinel for the bottom of the list. If not empty, tail.prev is the first element.



36
37
38
39
40
41
42
43
# File 'lib/active_fedora/orders/ordered_list.rb', line 36

def tail
  @tail ||=
    if tail_subject
      TailSentinel.new(self, prev_node: build_node(tail_subject))
    else
      head.next
    end
end

#tail_subjectObject (readonly)

Returns the value of attribute tail_subject.



7
8
9
# File 'lib/active_fedora/orders/ordered_list.rb', line 7

def tail_subject
  @tail_subject
end

Instance Method Details

#-(other) ⇒ OrderedList

Returns List with node removed.

Parameters:

  • Nodes (Array<ListNode>)

    to remove.

Returns:



63
64
65
66
67
68
# File 'lib/active_fedora/orders/ordered_list.rb', line 63

def -(other)
  other.each do |node|
    delete_node(node)
  end
  self
end

#[](key) ⇒ ListNode

Returns Node for the proxy at the given position.

Parameters:

  • key (Integer)

    Position of the proxy

Returns:

  • (ListNode)

    Node for the proxy at the given position



47
48
49
50
# File 'lib/active_fedora/orders/ordered_list.rb', line 47

def [](key)
  list = ordered_reader.take(key + 1)
  list[key]
end

#append_target(target, proxy_in: nil) ⇒ Object

Parameters:

  • target (ActiveFedora::Base)

    Target to append to list.

  • [::RDF::URI, (Hash)

    a customizable set of options



78
79
80
81
82
83
# File 'lib/active_fedora/orders/ordered_list.rb', line 78

def append_target(target, proxy_in: nil)
  node = build_node(new_node_subject)
  node.target = target
  node.proxy_in = proxy_in
  append_to(node, tail.prev)
end

#changed?Boolean

Returns Whether this list was changed since instantiation.

Returns:

  • (Boolean)

    Whether this list was changed since instantiation.



139
140
141
# File 'lib/active_fedora/orders/ordered_list.rb', line 139

def changed?
  @changed
end

#changes_committed!Object

Marks this list as not changed.



160
161
162
# File 'lib/active_fedora/orders/ordered_list.rb', line 160

def changes_committed!
  @changed = false
end

#delete_at(loc) ⇒ Object

Parameters:

  • loc (Integer)

    Index of node to delete.



125
126
127
128
129
# File 'lib/active_fedora/orders/ordered_list.rb', line 125

def delete_at(loc)
  return nil if loc.nil?
  arr = ordered_reader.take(loc + 1)
  delete_node(arr.last) if arr.length == loc + 1
end

#delete_node(node) ⇒ Object

Parameters:



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/active_fedora/orders/ordered_list.rb', line 112

def delete_node(node)
  node = ordered_reader.find { |x| x == node }
  return unless node

  prev_node = node.prev
  next_node = node.next
  node.prev.next = next_node
  node.next.prev = prev_node
  @changed = true
  node
end

#delete_target(obj) ⇒ Object

Parameters:

  • obj

    target of node to delete.



132
133
134
135
136
# File 'lib/active_fedora/orders/ordered_list.rb', line 132

def delete_target(obj)
  nodes_to_remove = ordered_reader.select { |list_node| obj.id == list_node.target_id }
  nodes_to_remove.each { |list_node| delete_node(list_node) }
  nodes_to_remove.last.try(:target)
end

#empty?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/active_fedora/orders/ordered_list.rb', line 71

def empty?
  head.next == tail
end

#insert_at(loc, target, proxy_in: nil) ⇒ Object

Parameters:

  • loc (Integer)

    Location to insert target at

  • target (ActiveFedora::Base)

    Target to insert



87
88
89
90
91
92
93
94
95
96
# File 'lib/active_fedora/orders/ordered_list.rb', line 87

def insert_at(loc, target, proxy_in: nil)
  node = build_node(new_node_subject)
  node.target = target
  node.proxy_in = proxy_in
  if loc.zero?
    append_to(node, head)
  else
    append_to(node, ordered_reader.take(loc).last)
  end
end

#insert_proxy_for_at(loc, proxy_for, proxy_in: nil) ⇒ Object

Parameters:

  • loc (Integer)

    Location to insert target at

  • proxy_for (String)

    proxyFor to add



100
101
102
103
104
105
106
107
108
109
# File 'lib/active_fedora/orders/ordered_list.rb', line 100

def insert_proxy_for_at(loc, proxy_for, proxy_in: nil)
  node = build_node(new_node_subject)
  node.proxy_for = proxy_for
  node.proxy_in = proxy_in
  if loc.zero?
    append_to(node, head)
  else
    append_to(node, ordered_reader.take(loc).last)
  end
end

#lastListNode

Returns Last node in the list.

Returns:



53
54
55
56
57
58
59
# File 'lib/active_fedora/orders/ordered_list.rb', line 53

def last
  if empty?
    nil
  else
    tail.prev
  end
end

#order_will_change!Object

Marks this ordered list as about to change. Useful for when changing proxies individually.



145
146
147
# File 'lib/active_fedora/orders/ordered_list.rb', line 145

def order_will_change!
  @changed = true
end

#proxy_inObject

Note:

If there are multiple proxy_ins this will log a warning and return the first.

Returns The node all proxies are a proxy in.

Returns:

  • The node all proxies are a proxy in.



172
173
174
175
176
# File 'lib/active_fedora/orders/ordered_list.rb', line 172

def proxy_in
  proxies = to_a.map(&:proxy_in_id).compact.uniq
  ActiveFedora::Base.logger.warn "WARNING: List contains nodes aggregated under different URIs. Returning only the first." if proxies.length > 1
  proxies.first
end

#target_idsObject

Returns IDs of all ordered targets, in order.

Returns:

  • IDs of all ordered targets, in order



165
166
167
# File 'lib/active_fedora/orders/ordered_list.rb', line 165

def target_ids
  to_a.map(&:target_id)
end

#to_graph::RDF::Graph

Returns Graph representation of this list.

Returns:

  • (::RDF::Graph)

    Graph representation of this list.



150
151
152
153
154
155
156
157
# File 'lib/active_fedora/orders/ordered_list.rb', line 150

def to_graph
  ::RDF::Graph.new.tap do |g|
    array = to_a
    array.map(&:to_graph).each do |resource_graph|
      g << resource_graph
    end
  end
end