class Aspire::Object::List

Represents a reading list in the Aspire API

Attributes

created[RW]

@!attribute [rw] created

@return [DateTime] the creation timestamp of the list
creator[RW]

@!attribute [rw] creator

@return [Array<Aspire::Object::User>] the reading list creators
description[RW]

@!attribute [rw] description

@return [String] the description of the list
items[RW]

@!attribute [rw] items

@return [Hash<String, Aspire::Object::ListItem>] a hash of ListItems
  indexed by item URI
last_published[RW]

@!attribute [rw] last_published

@return [DateTime] the timestamp of the most recent list publication
last_updated[RW]

@!attribute [rw] last_updated

@return [DateTime] the timestamp of the most recent list update
modules[RW]

@!attribute [rw] modules

@return [Array<Aspire::Object::Module>] the modules referencing this
  list
name[RW]

@!attribute [rw] name

@return [String] the reading list name
owner[RW]

@!attribute [rw] owner

@return [Aspire::Object::User] the list owner
publisher[RW]

@!attribute [rw] publisher

@return [Aspire::Object::User] the list publisher
time_period[RW]

@!attribute [rw] time_period

@return [Aspire::Object::TimePeriod] the period covered by the list

Public Class Methods

new(uri, factory, parent = nil, json: nil, ld: nil) click to toggle source

Initialises a new List instance @param uri [String] the URI of the object @param factory [Aspire::Object::Factory] a factory returning ListBase

subclass instances

@param parent [Aspire::Object::ListBase] this object’s parent 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]

Calls superclass method Aspire::Object::ListBase::new
# File lib/aspire/object/list.rb, line 266
def initialize(uri, factory, parent = nil, json: nil, ld: nil)
  # Set properties from the Reading Lists JSON API
  # - this must be called before the superclass constructor so that item
  #   details are available
  init_json_data(uri, factory, json)
  # Initialise the superclass
  super(uri, factory, parent, json: json, ld: ld)
  # Set properties from the linked data API data
  init_linked_data(ld)
end

Public Instance Methods

length(item_type = nil) click to toggle source

Returns the number of items in the list @see (Aspire::Object::ListBase#length)

Calls superclass method Aspire::Object::ListBase#length
# File lib/aspire/object/list.rb, line 279
def length(item_type = nil)
  item_type ||= :item
  # The item length of a list is the length of the items property,
  # avoiding the need to sum list entry lengths
  item_type == :item ? items.length : super(item_type)
end
to_s() click to toggle source

Returns a string representation of the List instance (the name) @return [String] the string representation of the List instance

Calls superclass method Aspire::Object::Base#to_s
# File lib/aspire/object/list.rb, line 288
def to_s
  name || super
end

Private Instance Methods

init_json_data(uri, factory, json = nil) click to toggle source

Retrieves the list details and history from the Aspire JSON API @param uri [String] the URI of the object @param factory [Aspire::Object::Factory] a factory returning ListBase

subclass instances

@param json [Hash] the parsed JSON data from the Aspire JSON API @return [void]

# File lib/aspire/object/list.rb, line 300
def init_json_data(uri, factory, json = nil)
  init_json_defaults
  # Get the list details
  json ||= factory.cache.read(uri, json: true)
  if json
    self.name = json['name']
    init_json_items(json['items'])
    init_json_modules(json['modules'], factory)
    init_json_time_period(json['timePeriod'], factory)
  end
  # Return the parsed JSON data from the Aspire list details JSON API
  json
end
init_json_defaults() click to toggle source

Sets the property defaults for JSON API fields @return [void]

# File lib/aspire/object/list.rb, line 316
def init_json_defaults
  # Default values
  self.modules = nil
  self.name = nil
  self.time_period = nil
end
init_json_items(items) click to toggle source

Builds a mapping from item URI to JSON data for items from the JSON API @param items [Array<Hash>] the parsed JSON data for the items array @return [void]

# File lib/aspire/object/list.rb, line 326
def init_json_items(items)
  # A hash mapping item URI to item
  self.items = {}
  return unless items
  items.each { |item| self.items[item['uri']] = item }
end
init_json_modules(mods, factory) click to toggle source

Builds a list of Module instances for modules from the JSON API @param mods [Array<Hash>] the parsed JSON data for the modules array @param factory [Aspire::Object::Factory] a factory returning ListBase

subclass instances

@return [void]

# File lib/aspire/object/list.rb, line 338
def init_json_modules(mods, factory)
  return unless mods
  self.modules = mods.map { |m| Module.new(m['uri'], factory, json: m) }
end
init_json_time_period(period, factory) click to toggle source

Sets the time period for the list from the JSON API @param period [Array<Hash>] the parsed JSON data for the time period @param factory [Aspire::Object::Factory] a factory returning ListBase

subclass instances

@return [void]

# File lib/aspire/object/list.rb, line 348
def init_json_time_period(period, factory)
  self.time_period = if period
                       TimePeriod.new(period['uri'], factory,
                                      json: period)
                     end
end
init_linked_data(ld = nil) click to toggle source

Sets reading list properties from the Aspire linked data API @return [void]

# File lib/aspire/object/list.rb, line 357
def init_linked_data(ld = nil)
  list_data = linked_data(uri, ld)
  init_linked_data_creator(list_data, ld)
  init_linked_data_modules(list_data, ld)
  init_linked_data_owner(list_data, ld)
  init_linked_data_publisher(list_data, ld)
  self.created = get_date(CREATED, list_data)
  self.description = get_property(DESCRIPTION, list_data)
  self.last_published = get_date(LAST_PUBLISHED, list_data)
  self.last_updated = get_date(LAST_UPDATED, list_data)
  self.name = get_property(NAME, list_data) unless name
end
init_linked_data_creator(list_data, ld) click to toggle source

Sets the reading list creator @param list_data [Hash] the parsed JSON data for the list from the

Aspire linked data API

@param ld [Hash] the parsed JSON data from the Aspire linked data API @return [void]

# File lib/aspire/object/list.rb, line 375
def init_linked_data_creator(list_data, ld)
  has_creator = get_property(HAS_CREATOR, list_data, single: false) || []
  self.creator = has_creator.map { |u| factory.get(u, ld: ld) }
end
init_linked_data_modules(list_data, ld) click to toggle source

Sets the list modules @param list_data [Hash] the parsed JSON data for the list from the

Aspire linked data API

@param ld [Hash] the parsed JSON data from the Aspire linked data API @return [void]

# File lib/aspire/object/list.rb, line 385
def init_linked_data_modules(list_data, ld)
  return unless modules.nil?
  mods = get_property(USED_BY, list_data, single: false) || []
  self.modules = mods.map { |u| factory.get(u, ld: ld) } if mods
end
init_linked_data_owner(list_data, ld) click to toggle source

Sets the list owner @param list_data [Hash] the parsed JSON data for the list from the

Aspire linked data API

@param ld [Hash] the parsed JSON data from the Aspire linked data API @return [void]

# File lib/aspire/object/list.rb, line 396
def init_linked_data_owner(list_data, ld)
  has_owner = get_property(HAS_OWNER, list_data, single: false) || []
  self.owner = has_owner.map { |u| factory.get(u, ld: ld) }
end
init_linked_data_publisher(list_data, ld) click to toggle source

Sets the list publisher @param list_data [Hash] the parsed JSON data for the list from the

Aspire linked data API

@param ld [Hash] the parsed JSON data from the Aspire linked data API @return [void]

# File lib/aspire/object/list.rb, line 406
def init_linked_data_publisher(list_data, ld)
  published_by = get_property(PUBLISHED_BY, list_data)
  self.publisher = factory.get(published_by, ld: ld)
end