Class: RDF::N3::Algebra::Str::Replace

Inherits:
ListOperator
  • Object
show all
Includes:
Builtin
Defined in:
lib/rdf/n3/algebra/str/replace.rb

Overview

A built-in for replacing characters or sub. takes a list of 3 strings; the first is the input data, the second the old and the third the new string. The object is calculated as the replaced string.

Examples:

("fofof bar", "of", "baz") string:replace "fbazbaz bar"

Constant Summary collapse

NAME =
:strReplace
URI =
RDF::N3::Str.replace

Instance Attribute Summary

Attributes included from Enumerable

#existentials, #universals

Instance Method Summary collapse

Methods included from Builtin

#each, #evaluate, #hash, #input_operand, #rank, #to_uri

Methods inherited from ListOperator

#as_literal, #execute, #input_operand

Instance Method Details

#resolve(list) ⇒ RDF::Term

Parameters:

Returns:

See Also:

  • ListOperator#evaluate


15
16
17
18
19
# File 'lib/rdf/n3/algebra/str/replace.rb', line 15

def resolve(list)
  format, *args = list.to_a.map(&:value)
  input, old_str, new_str = list.to_a
  RDF::Literal(input.to_s.gsub(old_str.to_s, new_str.to_s))
end

#validate(list) ⇒ Boolean

Subclasses may override or supplement validate to perform validation on the list subject

Parameters:

Returns:

  • (Boolean)


26
27
28
29
30
31
32
33
# File 'lib/rdf/n3/algebra/str/replace.rb', line 26

def validate(list)
  if super && list.length == 3 && list.to_a.all?(&:literal?)
    true
  else
    log_error(NAME) {"list must have exactly three entries: #{list.to_sxp}"}
    false
  end
end