Class: Aspire::Object::ListBase
Overview
The abstract base class of reading list objects (items, lists, sections)
Direct Known Subclasses
Constant Summary collapse
- KEY_PREFIX =
The Aspire linked data API returns properties of the form “#KEY_PREFIX_n” where n is a 1-based numeric index denoting the display order of the property.
'http://www.w3.org/1999/02/22-rdf-syntax-ns#'.freeze
Constants inherited from Base
Constants included from Util
Instance Attribute Summary collapse
-
#entries ⇒ Array<Aspire::Object::ListBase>
The ordered list of child objects.
-
#parent ⇒ Aspire::Object::ListBase
The parent reading list object of this object.
Attributes inherited from Base
Instance Method Summary collapse
-
#each {|entry| ... } ⇒ Object
Iterates over the child reading list objects in display order.
-
#each_item {|entry| ... } ⇒ void
Iterates over the child list items in display order (depth-first tree traversal).
-
#each_section {|entry| ... } ⇒ void
Iterates over the child list sections in display order (depth-first tree traversal).
-
#get_entries(ld: nil) ⇒ Array<Aspire::Object::ListBase>
Returns a list of child reading list objects in display order.
-
#initialize(uri, factory, parent = nil, json: nil, ld: nil) ⇒ void
constructor
Initialises a new ListBase instance.
-
#items ⇒ Array<Aspire::Object::ListItem>
Returns the child items of this object in display order.
-
#length(item_type = nil) ⇒ Integer
Returns the number of items in the list.
-
#parent_list ⇒ Aspire::Object::List
Returns the parent list of this object.
-
#parent_lists ⇒ Array<Aspire::Object::List>
Returns the ancestor lists of this object (nearest ancestor first).
-
#parent_section ⇒ Aspire::Object::ListSection
Returns the parent section of this object.
-
#parent_sections ⇒ Array<Aspire::Object::ListSection>
Returns the ancestor sections of this object (nearest ancestor first).
-
#parents(*classes) {|ancestor| ... } ⇒ Object
Returns a list of ancestor reading list objects of this object (nearest ancestor first) Positional parameters are the reading list classes to include in the result.
-
#parents_include?(ancestor, *classes) {|ancestor| ... } ⇒ Boolean
Returns true if ancestor should be included as a parent, false otherwise Remaining positional parameters are the reading list classes to include in the result.
-
#sections ⇒ Array<Aspire::Object::ListSection>
Returns the child sections of this object.
Methods inherited from Base
#get_boolean, #get_date, #get_property, #to_s
Methods included from Util
#child_url?, #duration, #id_from_uri, #item?, #linked_data, #linked_data_path, #list?, #list_url?, #module?, #parent_url?, #parse_url, #resource?, #section?, #url_for_comparison, #url_path, #user?
Constructor Details
#initialize(uri, factory, parent = nil, json: nil, ld: nil) ⇒ void
Initialises a new ListBase instance
32 33 34 35 36 |
# File 'lib/aspire/object/list.rb', line 32 def initialize(uri, factory, parent = nil, json: nil, ld: nil) super(uri, factory) self.parent = parent self.entries = get_entries(ld: ld) end |
Instance Attribute Details
#entries ⇒ Array<Aspire::Object::ListBase>
Returns the ordered list of child objects.
16 17 18 |
# File 'lib/aspire/object/list.rb', line 16 def entries @entries end |
#parent ⇒ Aspire::Object::ListBase
Returns the parent reading list object of this object.
21 22 23 |
# File 'lib/aspire/object/list.rb', line 21 def parent @parent end |
Instance Method Details
#each {|entry| ... } ⇒ Object
Iterates over the child reading list objects in display order
41 42 43 |
# File 'lib/aspire/object/list.rb', line 41 def each(&block) entries.each(&block) end |
#each_item {|entry| ... } ⇒ void
This method returns an undefined value.
Iterates over the child list items in display order (depth-first tree traversal)
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/aspire/object/list.rb', line 50 def each_item(&block) each do |entry| if entry.is_a?(ListItem) # Pass the list item to the block yield(entry) if block_given? else # Iterate the entry's list items entry.each_item(&block) end end nil end |
#each_section {|entry| ... } ⇒ void
This method returns an undefined value.
Iterates over the child list sections in display order (depth-first tree traversal)
68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/aspire/object/list.rb', line 68 def each_section(&block) each do |entry| if entry.is_a?(List) # Iterate the list's sections entry.each_section(&block) elsif entry.is_a?(ListSection) # Pass the list section to the block yield(entry) if block_given? end end nil end |
#get_entries(ld: nil) ⇒ Array<Aspire::Object::ListBase>
Returns a list of child reading list objects in display order
85 86 87 88 89 90 91 |
# File 'lib/aspire/object/list.rb', line 85 def get_entries(ld: nil) entries = [] data = linked_data(uri, ld) return entries unless data data.each { |key, value| get_ordered_entry(key, value, entries, ld) } entries end |
#items ⇒ Array<Aspire::Object::ListItem>
Returns the child items of this object in display order
95 96 97 98 99 |
# File 'lib/aspire/object/list.rb', line 95 def items result = [] each_item { |item| result.push(item) } result end |
#length(item_type = nil) ⇒ Integer
Returns the number of items in the list
107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/aspire/object/list.rb', line 107 def length(item_type = nil) item_type ||= :item # Return the number of top-level entries (items and sections) return entries.length if item_type == :entry # Return the sum of the number of list items in each entry if item_type == :item entries.reduce(0) { |count, entry| count + entry.length(:item) } end # Return the number of top-level sections return sections.length if item_type == :section # Otherwise return 0 for unknown item types 0 end |
#parent_list ⇒ Aspire::Object::List
Returns the parent list of this object
123 124 125 |
# File 'lib/aspire/object/list.rb', line 123 def parent_list parent_lists[0] end |
#parent_lists ⇒ Array<Aspire::Object::List>
Returns the ancestor lists of this object (nearest ancestor first)
129 130 131 |
# File 'lib/aspire/object/list.rb', line 129 def parent_lists parents(List) end |
#parent_section ⇒ Aspire::Object::ListSection
Returns the parent section of this object
135 136 137 |
# File 'lib/aspire/object/list.rb', line 135 def parent_section parent_sections[0] end |
#parent_sections ⇒ Array<Aspire::Object::ListSection>
Returns the ancestor sections of this object (nearest ancestor first)
142 143 144 |
# File 'lib/aspire/object/list.rb', line 142 def parent_sections parents(ListSection) end |
#parents(*classes) {|ancestor| ... } ⇒ Object
Returns a list of ancestor reading list objects of this object (nearest
ancestor first)
Positional parameters are the reading list classes to include in the result. If no classes are specified, all classes are included.
154 155 156 157 158 159 160 161 162 |
# File 'lib/aspire/object/list.rb', line 154 def parents(*classes, &block) result = [] ancestor = parent until ancestor.nil? result.push(ancestor) if parents_include?(ancestor, *classes, &block) ancestor = ancestor.parent end result end |
#parents_include?(ancestor, *classes) {|ancestor| ... } ⇒ Boolean
Returns true if ancestor should be included as a parent, false otherwise Remaining positional parameters are the reading list classes to include in the result. If no classes are specified, all classes are included.
173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/aspire/object/list.rb', line 173 def parents_include?(ancestor, *classes) # Filter ancestors by class if classes.nil? || classes.empty? || classes.include?(ancestor.class) # The ancestor is allowed by class, but may be disallowed by a code # block which returns false. If the code block returns true or is not # given, the ancestor is included. return block_given? && !yield(ancestor) ? false : true end # Otherwise the ancestor is not allowed by class false end |
#sections ⇒ Array<Aspire::Object::ListSection>
Returns the child sections of this object
187 188 189 |
# File 'lib/aspire/object/list.rb', line 187 def sections entries.select { |e| e.is_a?(ListSection) } end |