Class: ActiveFedora::Orders::OrderedList
- Inherits:
-
Object
- Object
- ActiveFedora::Orders::OrderedList
- 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
-
#graph ⇒ Object
readonly
Returns the value of attribute graph.
-
#head ⇒ HeadSentinel
Sentinel for the top of the list.
-
#head_subject ⇒ Object
readonly
Returns the value of attribute head_subject.
-
#tail ⇒ TailSentinel
Sentinel for the bottom of the list.
-
#tail_subject ⇒ Object
readonly
Returns the value of attribute tail_subject.
Instance Method Summary collapse
-
#-(other) ⇒ OrderedList
List with node removed.
-
#[](key) ⇒ ListNode
Node for the proxy at the given position.
- #append_target(target, proxy_in: nil) ⇒ Object
-
#changed? ⇒ Boolean
Whether this list was changed since instantiation.
-
#changes_committed! ⇒ Object
Marks this list as not changed.
- #delete_at(loc) ⇒ Object
- #delete_node(node) ⇒ Object
- #delete_target(obj) ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(graph, head_subject, tail_subject, node_cache: NodeCache.new) ⇒ OrderedList
constructor
A new instance of OrderedList.
- #insert_at(loc, target, proxy_in: nil) ⇒ Object
- #insert_proxy_for_at(loc, proxy_for, proxy_in: nil) ⇒ Object
-
#last ⇒ ListNode
Last node in the list.
-
#order_will_change! ⇒ Object
Marks this ordered list as about to change.
-
#proxy_in ⇒ Object
The node all proxies are a proxy in.
-
#target_ids ⇒ Object
IDs of all ordered targets, in order.
-
#to_graph ⇒ ::RDF::Graph
Graph representation of this list.
Constructor Details
#initialize(graph, head_subject, tail_subject, node_cache: NodeCache.new) ⇒ OrderedList
Returns a new instance of OrderedList.
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
#graph ⇒ Object (readonly)
Returns the value of attribute graph.
7 8 9 |
# File 'lib/active_fedora/orders/ordered_list.rb', line 7 def graph @graph end |
#head ⇒ HeadSentinel
Returns 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_subject ⇒ Object (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 |
#tail ⇒ TailSentinel
Returns 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_subject ⇒ Object (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.
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.
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
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.
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
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
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
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
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
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
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 |
#last ⇒ ListNode
Returns Last node in the list.
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_in ⇒ Object
If there are multiple proxy_ins this will log a warning and return the first.
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_ids ⇒ Object
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.
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 |