Class: RDF::List

Inherits:
Object show all
Includes:
Comparable, Enumerable, Value
Defined in:
vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/list.rb

Overview

An RDF list.

Examples:

Constructing a new list

RDF::List[1, 2, 3]

Since:

  • 0.2.3

Constant Summary collapse

NIL =

The canonical empty list.

Since:

  • 0.2.3

RDF::List.new(subject: RDF.nil).freeze

Instance Attribute Summary collapse

Attributes included from Enumerable

#existentials, #universals

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Value

#anonymous?, #canonicalize, #canonicalize!, #constant?, #graph?, #inspect!, #invalid?, #iri?, #literal?, #node?, #resource?, #start_with?, #statement?, #term?, #to_nquads, #to_ntriples, #type_error, #uri?, #validate!, #variable?

Methods included from Enumerable

add_entailment, #contain?, #dump, #each_graph, #each_object, #each_predicate, #each_quad, #each_term, #each_triple, #entail, #enum_graph, #enum_object, #enum_predicate, #enum_quad, #enum_statement, #enum_subject, #enum_term, #enum_triple, #graph_names, #has_graph?, #has_object?, #has_predicate?, #has_quad?, #has_statement?, #has_subject?, #has_term?, #has_triple?, #invalid?, #method_missing, #objects, #predicates, #project_graph, #quads, #respond_to_missing?, #statements, #subjects, #supports?, #terms, #to_h, #triples, #validate!

Methods included from Util::Aliasing::LateBound

#alias_method

Methods included from Countable

#count

Methods included from Isomorphic

#bijection_to, #isomorphic_with?

Constructor Details

#initialize(subject: nil, graph: nil, values: nil) {|list| ... } ⇒ List

Initializes a newly-constructed list.

Instantiates a new list based at subject, which should be an RDF::Node. List may be initialized using passed values.

If a values initializer is set with an empty list, subject will be used as the first element in the list. Otherwise, if the list is not empty, subject identifies the first element of the list to which values are prepended yielding a new subject. Otherwise, if there are no initial values, and subject does not identify an existing list in graph, the list remains identified by subject, but will be invalid.

Examples:

add constructed list to existing graph

l = RDF::List(nil, nil (1, 2, 3))
g = RDF::Graph.new << l
g.count # => l.count

Parameters:

  • subject (RDF::Resource)

    (RDF.nil) Subject should be an Node, not a URI. A list with an IRI head will not validate, but is commonly used to detect if a list is valid.

  • graph (RDF::Graph)

    (RDF::Graph.new)

  • values (Array<RDF::Term>)

    Any values which are not terms are coerced to RDF::Literal.

Yields:

  • (list)

Yield Parameters:

Since:

  • 0.2.3



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/list.rb', line 59

def initialize(subject: nil, graph: nil, values: nil, &block)
  @subject = subject || RDF.nil
  @graph   = graph   || RDF::Graph.new
  is_empty = @graph.query(subject: subject, predicate: RDF.first).empty?

  if subject && is_empty
    # An empty list with explicit subject and value initializers
    @subject = RDF.nil
    first, *values = Array(values)
    if first || values.length > 0
      # Intantiate the list from values, and insert the first value using subject.
      values.reverse_each {|value| self.unshift(value)}
      graph.insert RDF::Statement(subject, RDF.first, first || RDF.nil)
      graph.insert RDF::Statement(subject, RDF.rest, @subject)
    end
    @subject = subject
  else
    # Otherwise, prepend any values, which resets @subject
    Array(values).reverse_each {|value| self.unshift(value)}
  end

  if block_given?
    case block.arity
      when 1 then block.call(self)
      else instance_eval(&block)
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RDF::Enumerable

Instance Attribute Details

#graphRDF::Graph (readonly)

Returns the underlying graph storing the statements that constitute this list

Returns:

  • (RDF::Graph)

    the underlying graph storing the statements that constitute this list

Since:

  • 0.2.3



154
155
156
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/list.rb', line 154

def graph
  @graph
end

#subjectRDF::Resource (readonly)

Returns the subject term of this list.

Returns:

Since:

  • 0.2.3



150
151
152
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/list.rb', line 150

def subject
  @subject
end

Class Method Details

.[](*values) ⇒ RDF::List

Constructs a new list from the given values.

The list will be identified by a new autogenerated blank node, and backed by an initially empty in-memory graph.

Examples:

RDF::List[]
RDF::List[*(1..10)]
RDF::List[1, 2, 3]
RDF::List["foo", "bar"]
RDF::List["a", 1, "b", 2, "c", 3]

Parameters:

Returns:

Since:

  • 0.2.3



30
31
32
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/list.rb', line 30

def self.[](*values)
  self.new(subject: nil, graph: nil, values: values)
end

Instance Method Details

#&(other) ⇒ RDF::List

Returns the set intersection of this list and other.

The resulting list contains the elements common to both lists, with no duplicates.

Examples:

RDF::List[1, 2] & RDF::List[1, 2]       #=> RDF::List[1, 2]
RDF::List[1, 2] & RDF::List[2, 3]       #=> RDF::List[2]
RDF::List[1, 2] & RDF::List[3, 4]       #=> RDF::List[]

Parameters:

Returns:

See Also:

Since:

  • 0.2.3



177
178
179
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/list.rb', line 177

def &(other)
  RDF::List[*(to_a & other.to_a)]
end

#*(times) ⇒ RDF::List #*(sep) ⇒ RDF::List

Returns either a repeated list or a string concatenation of the elements in this list.

Overloads:

  • #*(times) ⇒ RDF::List

    Returns a new list built of times repetitions of this list.

    Examples:

    RDF::List[1, 2, 3] * 2                #=> RDF::List[1, 2, 3, 1, 2, 3]

    Parameters:

    Returns:

  • #*(sep) ⇒ RDF::List

    Returns the string concatenation of the elements in this list separated by sep. Equivalent to self.join(sep).

    Examples:

    RDF::List[1, 2, 3] * ","              #=> "1,2,3"

    Parameters:

    Returns:

Returns:

See Also:

Since:

  • 0.2.3



251
252
253
254
255
256
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/list.rb', line 251

def *(int_or_str)
  case int_or_str
    when Integer then RDF::List[*(to_a * int_or_str)]
    else join(int_or_str.to_s)
  end
end

#+(other) ⇒ RDF::List

Returns the concatenation of this list and other.

Examples:

RDF::List[1, 2] + RDF::List[3, 4]       #=> RDF::List[1, 2, 3, 4]

Parameters:

Returns:

See Also:

Since:

  • 0.2.3



208
209
210
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/list.rb', line 208

def +(other)
  RDF::List[*(to_a + other.to_a)]
end

#-(other) ⇒ RDF::List

Returns the difference between this list and other, removing any elements that appear in both lists.

Examples:

RDF::List[1, 2, 2, 3] - RDF::List[2]    #=> RDF::List[1, 3]

Parameters:

Returns:

See Also:

Since:

  • 0.2.3



222
223
224
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/list.rb', line 222

def -(other)
  RDF::List[*(to_a - other.to_a)]
end

#<<(value) ⇒ RDF::List

Appends an element to the tail of this list.

Examples:

RDF::List[] << 1 << 2 << 3              #=> RDF::List[1, 2, 3]

Parameters:

Returns:

See Also:

Since:

  • 0.2.3



426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/list.rb', line 426

def <<(value)
  value = normalize_value(value)

  if empty?
    @subject = new_subject = RDF::Node.new
  else
    old_subject, new_subject = last_subject, RDF::Node.new
    graph.delete([old_subject, RDF.rest, RDF.nil])
    graph.insert([old_subject, RDF.rest, new_subject])
  end

  graph.insert([new_subject, RDF.first, value.is_a?(RDF::List) ? value.subject : value])
  graph.insert([new_subject, RDF.rest, RDF.nil])

  self
end

#<=>(other) ⇒ Integer

Compares this list to other for sorting purposes.

Examples:

RDF::List[1] <=> RDF::List[1]           #=> 0
RDF::List[1] <=> RDF::List[2]           #=> -1
RDF::List[2] <=> RDF::List[1]           #=> 1

Parameters:

Returns:

See Also:

Since:

  • 0.2.3



468
469
470
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/list.rb', line 468

def <=>(other)
  to_a <=> Array(other)
end

#==(other) ⇒ Object

See Also:

  • Value#==

Since:

  • 0.2.3



158
159
160
161
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/list.rb', line 158

def ==(other)
  return false if other.is_a?(RDF::Value) && !other.list?
  super
end

#[]=(index, term) ⇒ RDF::Term #[]=(start, length, value) ⇒ RDF::Term, RDF::List #[]=(range, value) ⇒ RDF::Term, RDF::List

Element Assignment — Sets the element at index, or replaces a subarray from the start index for length elements, or replaces a subarray specified by the range of indices.

If indices are greater than the current capacity of the array, the array grows automatically. Elements are inserted into the array at start if length is zero.

Negative indices will count backward from the end of the array. For start and range cases the starting index is just before an element.

An IndexError is raised if a negative index points past the beginning of the array.

(see #unshift).

Examples:

a = RDF::List.new
a[4] = "4";                 #=> [rdf:nil, rdf:nil, rdf:nil, rdf:nil, "4"]
a[0, 3] = [ 'a', 'b', 'c' ] #=> ["a", "b", "c", rdf:nil, "4"]
a[1..2] = [ 1, 2 ]          #=> ["a", 1, 2, rdf:nil, "4"]
a[0, 2] = "?"               #=> ["?", 2, rdf:nil, "4"]
a[0..2] = "A"               #=> ["A", "4"]
a[-1]   = "Z"               #=> ["A", "Z"]
a[1..-1] = nil              #=> ["A", rdf:nil]
a[1..-1] = []               #=> ["A"]
a[0, 0] = [ 1, 2 ]          #=> [1, 2, "A"]
a[3, 0] = "B"               #=> [1, 2, "A", "B"]

Overloads:

Since:

  • 1.1.15



320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/list.rb', line 320

def []=(*args)
  start, length = 0, 0

  ary = self.to_a

  value = case args.last
  when Array then args.last
  when RDF::List then args.last.to_a
  else [args.last]
  end

  ret = case args.length
  when 3
    start, length = args[0], args[1]
    ary[start, length] = value
  when 2
    case args.first
    when Integer
      raise ArgumentError, "Index form of []= takes a single term" if args.last.is_a?(Array)
      ary[args.first] = args.last.is_a?(RDF::List) ? args.last.subject : args.last
    when Range
      ary[args.first] = value
    else
      raise ArgumentError, "Index form of must use an integer or range"
    end
  else
    raise ArgumentError, "List []= takes one or two index values"
  end

  # Clear the list and create a new list using the existing subject
  subject = @subject unless ary.empty? || @subject == RDF.nil
  self.clear
  new_list = RDF::List.new(subject: subject, graph: @graph, values: ary)
  @subject = new_list.subject
  ret # Returns inserted values
end

#at(index) ⇒ RDF::Term? Also known as: nth

Returns the element at index.

Examples:

RDF::List[1, 2, 3].at(0)                #=> 1
RDF::List[1, 2, 3].at(4)                #=> nil

Returns:

See Also:

Since:

  • 0.2.3



584
585
586
587
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/list.rb', line 584

def at(index)
  each.with_index { |v, i| return v if i == index }
  return nil
end

#clearRDF::List

Empties this list

Examples:

RDF::List[1, 2, 2, 3].clear    #=> RDF::List[]

Returns:

See Also:

Since:

  • 0.2.3



410
411
412
413
414
415
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/list.rb', line 410

def clear
  until empty?
    shift
  end
  return self
end

#eachEnumerator

Yields each element in this list.

Examples:

RDF::List[1, 2, 3].each do |value|
  puts value.inspect
end

Returns:

See Also:

Since:

  • 0.2.3



801
802
803
804
805
806
807
808
809
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/list.rb', line 801

def each
  return to_enum unless block_given?

  each_subject do |subject|
    if value = graph.first_object(subject: subject, predicate: RDF.first)
      yield value # FIXME
    end
  end
end

#each_statement(&block) ⇒ Enumerator Also known as: to_rdf

Yields each statement constituting this list.

Examples:

RDF::List[1, 2, 3].each_statement do |statement|
  puts statement.inspect
end

Returns:

See Also:

Since:

  • 0.2.3



821
822
823
824
825
826
827
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/list.rb', line 821

def each_statement(&block)
  return enum_statement unless block_given?

  each_subject do |subject|
    graph.query(subject: subject, &block)
  end
end

#each_subject {|subject| ... } ⇒ Enumerator

Yields each subject term constituting this list.

Examples:

RDF::List[1, 2, 3].each_subject do |subject|
  puts subject.inspect
end

Yields:

Returns:

See Also:

  • Enumerable#each

Since:

  • 0.2.3



778
779
780
781
782
783
784
785
786
787
788
789
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/list.rb', line 778

def each_subject
  return enum_subject unless block_given?

  subject = self.subject
  yield subject

  loop do
    rest = graph.first_object(subject: subject, predicate: RDF.rest)
    break if rest.nil? || rest.eql?(RDF.nil)
    yield subject = rest
  end
end

#eighthRDF::Term

Returns the eighth element in this list.

Examples:

RDF::List[*(1..10)].eighth              #=> RDF::Literal(8)

Returns:

Since:

  • 0.2.3



675
676
677
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/list.rb', line 675

def eighth
  at(7)
end

#empty?Boolean

Returns true if this list is empty.

Examples:

RDF::List[].empty?                      #=> true
RDF::List[1, 2, 3].empty?               #=> false

Returns:

  • (Boolean)

See Also:

Since:

  • 0.2.3



481
482
483
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/list.rb', line 481

def empty?
  graph.query(subject: subject, predicate: RDF.first).empty?
end

#eql?(other) ⇒ Integer

Compares this list to other using eql? on each component.

Examples:

RDF::List[1, 2, 3].eql? RDF::List[1, 2, 3]  #=> true
RDF::List[1, 2, 3].eql? [1, 2, 3]           #=> true

Parameters:

Returns:

See Also:

Since:

  • 0.2.3



453
454
455
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/list.rb', line 453

def eql?(other)
  to_a.eql? other.to_a # TODO: optimize this
end

#fetch(index, default = UNSET) ⇒ RDF::Term?

Returns element at index with default.

Examples:

RDF::List[1, 2, 3].fetch(0)             #=> RDF::Literal(1)
RDF::List[1, 2, 3].fetch(4)             #=> IndexError
RDF::List[1, 2, 3].fetch(4, nil)        #=> nil
RDF::List[1, 2, 3].fetch(4) { |n| n*n } #=> 16

Returns:

See Also:

Since:

  • 0.2.3



564
565
566
567
568
569
570
571
572
573
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/list.rb', line 564

def fetch(index, default = UNSET)
  val = at(index)
  return val unless val.nil?

  case
    when block_given?         then yield index
    when !default.eql?(UNSET) then default
    else raise IndexError, "index #{index} not in the list #{self.inspect}"
  end
end

#fifthRDF::Term

Returns the fifth element in this list.

Examples:

RDF::List[*(1..10)].fifth               #=> RDF::Literal(5)

Returns:

Since:

  • 0.2.3



642
643
644
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/list.rb', line 642

def fifth
  at(4)
end

#firstRDF::Term

Returns the first element in this list.

Examples:

RDF::List[*(1..10)].first               #=> RDF::Literal(1)

Returns:

Since:

  • 0.2.3



598
599
600
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/list.rb', line 598

def first
  graph.first_object(subject: first_subject, predicate: RDF.first)
end

#first_subjectRDF::Resource

Returns the first subject term constituting this list.

This is equivalent to subject.

Examples:

RDF::List[1, 2, 3].first_subject        #=> RDF::Node(...)

Returns:

Since:

  • 0.2.3



744
745
746
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/list.rb', line 744

def first_subject
  subject
end

#fourthRDF::Term

Returns the fourth element in this list.

Examples:

RDF::List[*(1..10)].fourth              #=> RDF::Literal(4)

Returns:

Since:

  • 0.2.3



631
632
633
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/list.rb', line 631

def fourth
  at(3)
end

#index(value) ⇒ Integer

Returns the index of the first element equal to value, or nil if no match was found.

Examples:

RDF::List['a', 'b', 'c'].index('a')     #=> 0
RDF::List['a', 'b', 'c'].index('d')     #=> nil

Parameters:

Returns:

See Also:

Since:

  • 0.2.3



511
512
513
514
515
516
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/list.rb', line 511

def index(value)
  each.with_index do |v, i|
    return i if v == value
  end
  return nil
end

#inspectString

Returns a developer-friendly representation of this list.

Examples:

RDF::List[].inspect                     #=> "#<RDF::List(_:g2163790380)>"

Returns:

Since:

  • 0.2.3



948
949
950
951
952
953
954
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/list.rb', line 948

def inspect
  if self.equal?(NIL)
    'RDF::List::NIL'
  else
    sprintf("#<%s:%#0x(%s)>", self.class.name, __id__, join(', '))
  end
end

#join(sep = $,) ⇒ String

Returns a string created by converting each element of this list into a string, separated by sep.

Examples:

RDF::List[1, 2, 3].join                 #=> "123"
RDF::List[1, 2, 3].join(", ")           #=> "1, 2, 3"

Parameters:

  • sep (String) (defaults to: $,)

Returns:

See Also:

Since:

  • 0.2.3



841
842
843
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/list.rb', line 841

def join(sep = $,)
  map(&:to_s).join(sep)
end

#lastRDF::Term

Returns the last element in this list.

Examples:

RDF::List[*(1..10)].last                 #=> RDF::Literal(10)

Returns:

See Also:

Since:

  • 0.2.3



709
710
711
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/list.rb', line 709

def last
  graph.first_object(subject: last_subject, predicate: RDF.first)
end

#last_subjectRDF::Resource

Returns the last subject term constituting this list.

Examples:

RDF::List[1, 2, 3].last_subject         #=> RDF::Node(...)

Returns:

Since:

  • 0.2.3



764
765
766
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/list.rb', line 764

def last_subject
  each_subject.to_a.last # TODO: optimize this
end

#lengthInteger Also known as: size

Returns the length of this list.

Examples:

RDF::List[].length                      #=> 0
RDF::List[1, 2, 3].length               #=> 3

Returns:

See Also:

Since:

  • 0.2.3



494
495
496
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/list.rb', line 494

def length
  each.count
end

#list?Boolean

Is this a RDF::List?

Returns:

  • (Boolean)

Since:

  • 0.2.3



97
98
99
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/list.rb', line 97

def list?
  true
end

#ninthRDF::Term

Returns the ninth element in this list.

Examples:

RDF::List[*(1..10)].ninth               #=> RDF::Literal(9)

Returns:

Since:

  • 0.2.3



686
687
688
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/list.rb', line 686

def ninth
  at(8)
end

#restRDF::List

Returns a list containing all but the first element of this list.

Examples:

RDF::List[1, 2, 3].rest                 #=> RDF::List[2, 3]

Returns:

Since:

  • 0.2.3



720
721
722
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/list.rb', line 720

def rest
  (subject = rest_subject).eql?(RDF.nil) ? nil : self.class.new(subject: subject, graph: graph)
end

#rest_subjectRDF::Resource

Examples:

RDF::List[1, 2, 3].rest_subject         #=> RDF::Node(...)

Returns:

Since:

  • 0.2.3



753
754
755
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/list.rb', line 753

def rest_subject
  graph.first_object(subject: subject, predicate: RDF.rest)
end

#reverseRDF::List

Returns the elements in this list in reversed order.

Examples:

RDF::List[1, 2, 3].reverse              #=> RDF::List[3, 2, 1]

Returns:

See Also:

Since:

  • 0.2.3



853
854
855
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/list.rb', line 853

def reverse
  RDF::List[*to_a.reverse]
end

#secondRDF::Term

Returns the second element in this list.

Examples:

RDF::List[*(1..10)].second              #=> RDF::Literal(2)

Returns:

Since:

  • 0.2.3



609
610
611
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/list.rb', line 609

def second
  at(1)
end

#seventhRDF::Term

Returns the seventh element in this list.

Examples:

RDF::List[*(1..10)].seventh             #=> RDF::Literal(7)

Returns:

Since:

  • 0.2.3



664
665
666
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/list.rb', line 664

def seventh
  at(6)
end

#shiftRDF::Term

Removes and returns the element at the head of this list.

Examples:

RDF::List[1,2,3].shift              #=> 1

Returns:

See Also:

Since:

  • 0.2.3



389
390
391
392
393
394
395
396
397
398
399
400
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/list.rb', line 389

def shift
  return nil if empty?

  value = first
  old_subject, new_subject = subject, rest_subject
  graph.delete([old_subject, RDF.type, RDF.List])
  graph.delete([old_subject, RDF.first, value])
  graph.delete([old_subject, RDF.rest, new_subject])

  @subject = new_subject
  return value
end

#sixthRDF::Term

Returns the sixth element in this list.

Examples:

RDF::List[*(1..10)].sixth               #=> RDF::Literal(6)

Returns:

Since:

  • 0.2.3



653
654
655
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/list.rb', line 653

def sixth
  at(5)
end

#slice(*args) ⇒ RDF::Term Also known as: []

Returns a slice of a list.

Examples:

RDF::List[1, 2, 3].slice(0)    #=> RDF::Literal(1),
RDF::List[1, 2, 3].slice(0, 2) #=> RDF::List[1, 2],
RDF::List[1, 2, 3].slice(0..2) #=> RDF::List[1, 2, 3]

Returns:

See Also:

Since:

  • 0.2.3



528
529
530
531
532
533
534
535
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/list.rb', line 528

def slice(*args)
  case argc = args.size
    when 2 then slice_with_start_and_length(*args)
    when 1 then (arg = args.first).is_a?(Range) ? slice_with_range(arg) : at(arg)
    when 0 then raise ArgumentError, "wrong number of arguments (0 for 1)"
    else raise ArgumentError, "wrong number of arguments (#{argc} for 2)"
  end
end

#sort(&block) ⇒ RDF::List

Returns the elements in this list in sorted order.

Examples:

RDF::List[2, 3, 1].sort                 #=> RDF::List[1, 2, 3]

Returns:

See Also:

Since:

  • 0.2.3



865
866
867
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/list.rb', line 865

def sort(&block)
  RDF::List[*super]
end

#sort_by(&block) ⇒ RDF::List

Returns the elements in this list in sorted order.

Examples:

RDF::List[2, 3, 1].sort_by(&:to_i)      #=> RDF::List[1, 2, 3]

Returns:

See Also:

Since:

  • 0.2.3



877
878
879
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/list.rb', line 877

def sort_by(&block)
  RDF::List[*super]
end

#tailRDF::List

Returns a list containing the last element of this list.

Examples:

RDF::List[1, 2, 3].tail                 #=> RDF::List[3]

Returns:

Since:

  • 0.2.3



731
732
733
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/list.rb', line 731

def tail
  (subject = last_subject).eql?(RDF.nil) ? nil : self.class.new(subject: subject, graph: graph)
end

#tenthRDF::Term

Returns the tenth element in this list.

Examples:

RDF::List[*(1..10)].tenth               #=> RDF::Literal(10)

Returns:

Since:

  • 0.2.3



697
698
699
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/list.rb', line 697

def tenth
  at(9)
end

#thirdRDF::Term

Returns the third element in this list.

Examples:

RDF::List[*(1..10)].third               #=> RDF::Literal(4)

Returns:

Since:

  • 0.2.3



620
621
622
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/list.rb', line 620

def third
  at(2)
end

#to_aArray

Returns the elements in this list as an array.

Examples:

RDF::List[].to_a                        #=> []
RDF::List[1, 2, 3].to_a                 #=> [RDF::Literal(1), RDF::Literal(2), RDF::Literal(3)]

Returns:

Since:

  • 0.2.3



901
902
903
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/list.rb', line 901

def to_a
  each.to_a
end

#to_sString

Returns a string representation of this list.

Examples:

RDF::List[].to_s                        #=> "RDF::List[]"
RDF::List[1, 2, 3].to_s                 #=> "RDF::List[1, 2, 3]"

Returns:

Since:

  • 0.2.3



937
938
939
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/list.rb', line 937

def to_s
  'RDF::List[' + join(', ') + ']'
end

#to_setSet

Returns the elements in this list as a set.

Examples:

RDF::List[1, 2, 3].to_set               #=> Set[RDF::Literal(1), RDF::Literal(2), RDF::Literal(3)]

Returns:

  • (Set)

Since:

  • 0.2.3



912
913
914
915
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/list.rb', line 912

def to_set
  require 'set' unless defined?(::Set)
  each.to_set
end

#to_termRDF::Resource

Returns the subject of the list.

Examples:

RDF::List[].to_term                     #=> "RDF[:nil]"
RDF::List[1, 2, 3].to_term              #=> "RDF::Node"

Returns:

Since:

  • 0.2.3



925
926
927
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/list.rb', line 925

def to_term
  subject
end

#uniqRDF::List

Returns a new list with the duplicates in this list removed.

Examples:

RDF::List[1, 2, 2, 3].uniq              #=> RDF::List[1, 2, 3]

Returns:

See Also:

Since:

  • 0.2.3



889
890
891
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/list.rb', line 889

def uniq
  RDF::List[*to_a.uniq]
end

#unshift(value) ⇒ RDF::List

Appends an element to the head of this list. Existing references are not updated, as the list subject changes as a side-effect.

Examples:

RDF::List[].unshift(1).unshift(2).unshift(3) #=> RDF::List[3, 2, 1]

Parameters:

Returns:

See Also:

Since:

  • 0.2.3



368
369
370
371
372
373
374
375
376
377
378
379
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/list.rb', line 368

def unshift(value)
  value = normalize_value(value)

  new_subject, old_subject = RDF::Node.new, subject

  graph.insert([new_subject, RDF.first, value.is_a?(RDF::List) ? value.subject : value])
  graph.insert([new_subject, RDF.rest, old_subject])

  @subject = new_subject

  return self
end

#valid?Boolean

Validate the list ensuring that

  • each node is referenced exactly once (except for the head, which may have no reference)
  • rdf:rest values are all BNodes are nil
  • each subject has exactly one value for rdf:first and rdf:rest.
  • The value of rdf:rest must be either a BNode or rdf:nil.
  • only the list head may have any other properties

Returns:

  • (Boolean)

Since:

  • 0.2.3



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/list.rb', line 110

def valid?
  li = subject
  list_nodes = []
  while li != RDF.nil do
    return false if list_nodes.include?(li)
    list_nodes << li
    rest = nil
    firsts = rests = 0
    @graph.query(subject: li) do |st|
      return false unless st.subject.node?
      case st.predicate
      when RDF.first
        firsts += 1
      when RDF.rest
        rest = st.object
        return false unless rest.node? || rest == RDF.nil
        rests += 1
      when RDF.type
      else
        # It may have no other properties
        return false unless li == subject
      end
    end
    return false unless firsts == 1 && rests == 1
    li = rest
  end

  # All elements other than the head must be referenced exactly once
  return list_nodes.all? do |li|
    refs = @graph.query(object: li).count
    case refs
    when 0 then li == subject
    when 1 then true
    else        false
    end
  end
end

#|(other) ⇒ RDF::List

Returns the set union of this list and other.

The resulting list contains the elements from both lists, with no duplicates.

Examples:

RDF::List[1, 2] | RDF::List[1, 2]       #=> RDF::List[1, 2]
RDF::List[1, 2] | RDF::List[2, 3]       #=> RDF::List[1, 2, 3]
RDF::List[1, 2] | RDF::List[3, 4]       #=> RDF::List[1, 2, 3, 4]

Parameters:

Returns:

See Also:

Since:

  • 0.2.3



195
196
197
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/list.rb', line 195

def |(other)
  RDF::List[*(to_a | other.to_a)]
end