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
# File 'lib/active_fedora/orders/ordered_list.rb', line 15

def initialize(graph, head_subject, tail_subject)
  @graph = graph
  @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.



26
27
28
# File 'lib/active_fedora/orders/ordered_list.rb', line 26

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.



32
33
34
35
36
37
38
39
40
41
# File 'lib/active_fedora/orders/ordered_list.rb', line 32

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:



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

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



45
46
47
48
# File 'lib/active_fedora/orders/ordered_list.rb', line 45

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



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

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.



137
138
139
# File 'lib/active_fedora/orders/ordered_list.rb', line 137

def changed?
  @changed
end

#changes_committed!Object

Marks this list as not changed.



158
159
160
# File 'lib/active_fedora/orders/ordered_list.rb', line 158

def changes_committed!
  @changed = false
end

#delete_at(loc) ⇒ Object

Parameters:

  • loc (Integer)

    Index of node to delete.



123
124
125
126
127
# File 'lib/active_fedora/orders/ordered_list.rb', line 123

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:



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

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.



130
131
132
133
134
# File 'lib/active_fedora/orders/ordered_list.rb', line 130

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)


69
70
71
# File 'lib/active_fedora/orders/ordered_list.rb', line 69

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



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

def insert_at(loc, target, proxy_in: nil)
  node = build_node(new_node_subject)
  node.target = target
  node.proxy_in = proxy_in
  if loc == 0
    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



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

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 == 0
    append_to(node, head)
  else
    append_to(node, ordered_reader.take(loc).last)
  end
end

#lastListNode

Returns Last node in the list.

Returns:



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

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.



143
144
145
# File 'lib/active_fedora/orders/ordered_list.rb', line 143

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.



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

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." if ActiveFedora::Base.logger
  end
  proxies.first
end

#target_idsObject

Returns IDs of all ordered targets, in order.

Returns:

  • IDs of all ordered targets, in order



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

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.



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

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