Module: RDF::Util::Coercions

Included in:
Changeset, Mutable, Writable
Defined in:
vendor/bundler/ruby/2.7.0/bundler/gems/rdf-3c0fc67183db/lib/rdf/util/coercions.rb

Instance Method Summary collapse

Instance Method Details

#coerce_statements(statements, query: false, constant: false) {|RDF::Statement, RDF::Enumerable| ... } ⇒ Object (protected)

Coerce an array of arguments into Statement, or Enumerable and then yield to a block. Note that this code was amalgamated from that which was sandwiched around both Writable#insert_statements and Mutable#delete_statements. The parameters query and constant are therefore present to handle the conditions where the statements contain wildcards and what to do about them.

Examples:

coerce_statements(statements) { |value| do_something(value) }

Parameters:

  • statements (#map)

    The arbitrary-ish input to be manipulated

  • query (false, true) (defaults to: false)

    Whether to call query before the block (as expected by Mutable#delete_statements)

  • constant (false, true) (defaults to: false)

    Whether to test if the statements are constant (as expected by Mutable#delete_statements)

Yields:

Returns:

  • statements

Raises:

  • (ArgumentError)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'vendor/bundler/ruby/2.7.0/bundler/gems/rdf-3c0fc67183db/lib/rdf/util/coercions.rb', line 32

def coerce_statements(statements, query: false, constant: false, &block)
  raise ArgumentError, 'expecting a block' unless block_given?

  statements = statements.map do |value|
    case
    when value.respond_to?(:each_statement)
      block.call(value)
      nil
    when (statement = Statement.from(value)) &&
        (!constant || statement.constant?)
      statement
    when query      # XXX note that this only makes sense when the module is include()d

      block.call(self.query(value))
      nil
    else
      raise ArgumentError, "Not a valid statement: #{value.inspect}"
    end
  end.compact

  block.call(statements) unless statements.empty?

  # eh might as well return these
  statements
end