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) ⇒ 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)
  @graph = if graph.respond_to?(:graph)
             graph.graph.data
           else
             graph
           end
  @head_subject = head_subject
  @tail_subject = tail_subject
  @node_cache ||= NodeCache.new
  @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
44
45
# File 'lib/active_fedora/orders/ordered_list.rb', line 36

def tail
  @tail ||=
    begin
      if tail_subject
        TailSentinel.new(self, prev_node: build_node(tail_subject))
      else
        head.next
      end
    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:



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

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



49
50
51
52
# File 'lib/active_fedora/orders/ordered_list.rb', line 49

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



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

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.



141
142
143
# File 'lib/active_fedora/orders/ordered_list.rb', line 141

def changed?
  @changed
end

#changes_committed!Object

Marks this list as not changed.



162
163
164
# File 'lib/active_fedora/orders/ordered_list.rb', line 162

def changes_committed!
  @changed = false
end

#delete_at(loc) ⇒ Object

Parameters:

  • loc (Integer)

    Index of node to delete.



127
128
129
130
131
# File 'lib/active_fedora/orders/ordered_list.rb', line 127

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:



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

def delete_node(node)
  node = ordered_reader.find { |x| x == node }
  if node
    prev_node = node.prev
    next_node = node.next
    node.prev.next = next_node
    node.next.prev = prev_node
    @changed = true
    node
  end
end

#delete_target(obj) ⇒ Object

Parameters:

  • obj

    target of node to delete.



134
135
136
137
138
# File 'lib/active_fedora/orders/ordered_list.rb', line 134

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)


73
74
75
# File 'lib/active_fedora/orders/ordered_list.rb', line 73

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



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

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



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

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:



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

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.



147
148
149
# File 'lib/active_fedora/orders/ordered_list.rb', line 147

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.



174
175
176
177
178
179
180
# File 'lib/active_fedora/orders/ordered_list.rb', line 174

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

#target_idsObject

Returns IDs of all ordered targets, in order.

Returns:

  • IDs of all ordered targets, in order



167
168
169
# File 'lib/active_fedora/orders/ordered_list.rb', line 167

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.



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

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