class Aspire::Object::ListBase
The abstract base class of reading list objects (items, lists, sections)
Constants
Attributes
@!attribute [rw] entries
@return [Array<Aspire::Object::ListBase>] the ordered list of child objects
@!attribute [rw] parent
@return [Aspire::Object::ListBase] the parent reading list object of this object
Public Class Methods
Initialises a new ListBase
instance @param uri [String] the reading list object URI (item/list/section) @param factory [Aspire::Object::Factory] a factory returning ListBase
subclass instances
@param parent [Aspire::Object::ListBase] the parent reading list object
of this object
@param json [Hash] the parsed JSON data from the Aspire
JSON API
@param ld [Hash] the parsed JSON data from the Aspire
linked data API
@return [void]
Aspire::Object::Base::new
# 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
Public Instance Methods
Iterates over the child reading list objects in display order @yield [entry] passes the child reading list object to the block @yieldparam entry [Aspire::Object::ListBase] the reading list object
# File lib/aspire/object/list.rb, line 41 def each(&block) entries.each(&block) end
Iterates over the child list items in display order (depth-first tree traversal) @yield [entry] passes the list item to the block @yieldparam entry [Aspire::Object::ListItem] the reading list item @return [void]
# 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
Iterates over the child list sections in display order (depth-first tree traversal) @yield [entry] passes the list section to the block @yieldparam entry [Aspire::Object::ListSection] the reading list section @return [void]
# 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
Returns a list of child reading list objects in display order @param ld [Hash] the parsed JSON data from the Aspire
linked data API
@return [Array<Aspire::Object::ListBase>] the ordered list of child
objects
# 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
Returns the child items of this object in display order @return [Array<Aspire::Object::ListItem>] the child list items
# File lib/aspire/object/list.rb, line 95 def items result = [] each_item { |item| result.push(item) } result end
Returns the number of items in the list @param item_type [Symbol] selects the list entry type to count
:entry = top-level item or section :item = list item (default) :section = top-level section
@return [Integer] the number of list entry instances
# 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
Returns the parent list of this object @return [Aspire::Object::List] the parent reading list
# File lib/aspire/object/list.rb, line 123 def parent_list parent_lists[0] end
Returns the ancestor lists of this object (nearest ancestor first) @return [Array<Aspire::Object::List>] the ancestor reading lists
# File lib/aspire/object/list.rb, line 129 def parent_lists parents(List) end
Returns the parent section of this object @return [Aspire::Object::ListSection] the parent reading list section
# File lib/aspire/object/list.rb, line 135 def parent_section parent_sections[0] end
Returns the ancestor sections of this object (nearest ancestor first) @return [Array<Aspire::Object::ListSection>] the ancestor reading list
sections
# File lib/aspire/object/list.rb, line 142 def parent_sections parents(ListSection) end
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. @yield [ancestor] passes the ancestor to the block @yieldparam ancestor [Aspire::Object::ListBase] the reading list object @yieldreturn [Boolean] if true, include in the ancestor list, otherwise
ignore
# 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
Returns true if ancestor should be included as a parent, false otherwise @param ancestor [Aspire::Object::ListBase] the reading list object Remaining positional parameters are the reading list classes to include in the result. If no classes are specified, all classes are included. @yield [ancestor] passes the ancestor to the block @yieldparam ancestor [Aspire::Object::ListBase]
the reading list object
@yieldreturn [Boolean] if true, include in the ancestor list, otherwise
ignore
# 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
Returns the child sections of this object @return [Array<Aspire::Object::ListSection>] the child list sections
# File lib/aspire/object/list.rb, line 187 def sections entries.select { |e| e.is_a?(ListSection) } end
Private Instance Methods
Adds a child object to the entries array if key is an ordered property @param key [String] the property name URI @param value [Hash] the property value hash @param entries [Array<Aspire::Object::ListBase>] the ordered list of
child objects
@param ld [Hash] the parsed JSON data from the Aspire
linked data API
@return [Aspire::Object::ListBase] the list object
# File lib/aspire/object/list.rb, line 200 def get_ordered_entry(key, value, entries, ld) prefix, index = key.split('_') return nil unless prefix == KEY_PREFIX uri = value[0]['value'] entries[index.to_i - 1] = factory.get(uri, self, ld: ld) end