Class: Aspire::Object::ListBase

Inherits:
Base
  • Object
show all
Defined in:
lib/aspire/object/list.rb

Overview

The abstract base class of reading list objects (items, lists, sections)

Direct Known Subclasses

List, ListItem, ListSection

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

Base::STRIP_HTML

Constants included from Util

Util::LD_API_URI

Instance Attribute Summary collapse

Attributes inherited from Base

#factory, #uri

Instance Method Summary collapse

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

Parameters:

  • uri (String)

    the reading list object URI (item/list/section)

  • factory (Aspire::Object::Factory)

    a factory returning ListBase subclass instances

  • parent (Aspire::Object::ListBase) (defaults to: nil)

    the parent reading list object of this object

  • json (Hash) (defaults to: nil)

    the parsed JSON data from the Aspire JSON API

  • ld (Hash) (defaults to: nil)

    the parsed JSON data from the Aspire linked data API



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

#entriesArray<Aspire::Object::ListBase>

Returns the ordered list of child objects.

Returns:



16
17
18
# File 'lib/aspire/object/list.rb', line 16

def entries
  @entries
end

#parentAspire::Object::ListBase

Returns the parent reading list object of this object.

Returns:



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

Yields:

  • (entry)

    passes the child reading list object to the block

Yield Parameters:



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)

Yields:

  • (entry)

    passes the list item to the block

Yield Parameters:



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)

Yields:

  • (entry)

    passes the list section to the block

Yield Parameters:



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

Parameters:

  • ld (Hash) (defaults to: nil)

    the parsed JSON data from the Aspire linked data API

Returns:



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

#itemsArray<Aspire::Object::ListItem>

Returns the child items of this object in display order

Returns:



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

Parameters:

  • item_type (Symbol) (defaults to: nil)

    selects the list entry type to count :entry = top-level item or section :item = list item (default) :section = top-level section

Returns:

  • (Integer)

    the number of list entry instances



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_listAspire::Object::List

Returns the parent list of this object

Returns:



123
124
125
# File 'lib/aspire/object/list.rb', line 123

def parent_list
  parent_lists[0]
end

#parent_listsArray<Aspire::Object::List>

Returns the ancestor lists of this object (nearest ancestor first)

Returns:



129
130
131
# File 'lib/aspire/object/list.rb', line 129

def parent_lists
  parents(List)
end

#parent_sectionAspire::Object::ListSection

Returns the parent section of this object

Returns:



135
136
137
# File 'lib/aspire/object/list.rb', line 135

def parent_section
  parent_sections[0]
end

#parent_sectionsArray<Aspire::Object::ListSection>

Returns the ancestor sections of this object (nearest ancestor first)

Returns:



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.

Yields:

  • (ancestor)

    passes the ancestor to the block

Yield Parameters:

Yield Returns:

  • (Boolean)

    if true, include in the ancestor list, otherwise ignore



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.

Parameters:

Yields:

  • (ancestor)

    passes the ancestor to the block

Yield Parameters:

Yield Returns:

  • (Boolean)

    if true, include in the ancestor list, otherwise ignore

Returns:

  • (Boolean)


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

#sectionsArray<Aspire::Object::ListSection>

Returns the child sections of this object

Returns:



187
188
189
# File 'lib/aspire/object/list.rb', line 187

def sections
  entries.select { |e| e.is_a?(ListSection) }
end