Class: ShEx::Extension

Inherits:
Object
  • Object
show all
Extended by:
Enumerable
Defined in:
lib/shex/extensions/extension.rb

Overview

Abstract class of ShEx Extension extensions.

Extensions are registered automatically when they are required by subclassing this class.

Implementations may provide an initializer which is called once for a given semantic action. Additionally, enter and exit methods are invoked when beginning any Triple Expression containing this Semantic Action. The visit method is invoked once for each matched triple within that Triple Expression.

Subclasses must define at least visit.

Examples:

Test extension

class Test < ShEx::Extension("http://shex.io/extensions/Test/")

  # Called to initialize module before evaluating shape
  def initialize(schema: nil, depth: 0, logger: nil, **options)
  end

  # Called on entry to containing Triple Expression
  def enter(code: nil, arcs_in: nil, arcs_out: nil, depth: 0, **options)
  end

  # Called once for each matched statement
  def visit(code: nil, matched: nil, depth: 0, **options)
  end

  # Called on entry to containing Triple Expression
  def exit(code: nil, matched: [], unmatched: [], depth: 0, **options)
  end

  # Called after shape completes on success or failure
  def close(schema: nil, depth: 0, **options)
  end

See Also:

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema: nil, logger: nil, depth: 0, **options) ⇒ self

Initializer for a given instance. Implementations may define this for instance and/or class


101
102
103
104
105
# File 'lib/shex/extensions/extension.rb', line 101

def initialize(schema: nil, logger: nil, depth: 0, **options)
  @logger = logger
  @options = options
  self
end

Class Method Details

.each {|klass| ... } ⇒ Enumerator

Enumerates known Semantic Action classes.

Yields:

  • (klass)

Yield Parameters:

  • klass (Class)

54
55
56
57
58
59
60
61
62
63
# File 'lib/shex/extensions/extension.rb', line 54

def each(&block)
  if self.equal?(ShEx::Extension)    # This is needed since all Semantic Action classes are defined using
    # Ruby's autoloading facility, meaning that `@@subclasses` will be
    # empty until each subclass has been touched or require'd.

    @@subclasses.values.each(&block)
  else
    block.call(self)
  end
end

.find(name) ⇒ SemanticAction

Return the SemanticAction associated with a URI.


70
71
72
# File 'lib/shex/extensions/extension.rb', line 70

def find(name)
  @@subclasses.fetch(name.to_s, nil)
end

.nameString

The "name" of this class is a URI used to uniquely identify it.


44
45
46
# File 'lib/shex/extensions/extension.rb', line 44

def name
  @@subclasses.invert[self]
end

Instance Method Details

#close(schema: nil, depth: 0, **options) ⇒ self

Called after shape completes on success or failure


157
158
159
# File 'lib/shex/extensions/extension.rb', line 157

def close(schema: nil, depth: 0, **options)
  self
end

#enter(code: nil, arcs_in: nil, arcs_out: nil, expression: nil, depth: 0, **options) ⇒ Boolean

Called on entry to containing TripleExpression


118
119
120
# File 'lib/shex/extensions/extension.rb', line 118

def enter(code: nil, arcs_in: nil, arcs_out: nil, expression: nil, depth: 0, **options)
  true
end

#exit(code: nil, matched: [], unmatched: [], expression: nil, depth: 0, **options) ⇒ self

Called on exit from containing TripleExpression


147
148
149
# File 'lib/shex/extensions/extension.rb', line 147

def exit(code: nil, matched: [], unmatched: [], expression: nil, depth: 0, **options)
  self
end

#visit(code: nil, matched: nil, expression: nil, depth: 0, **options) ⇒ Boolean

Called after a TripleExpression has matched zero or more statements

Raises:

  • (NotImplementedError)

132
133
134
# File 'lib/shex/extensions/extension.rb', line 132

def visit(code: nil, matched: nil, expression: nil, depth: 0, **options)
  raise NotImplementedError
end