Sha256: 64fb782a15a5bba96355750494855140a244381e00a56bc5759aec6e0158612c

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

module RDF
  ##
  # An RDF query mixin.
  #
  # Classes that include this module must implement an `#each` method that
  # yields {RDF::Statement RDF statements}.
  #
  # @see RDF::Graph
  # @see RDF::Repository
  module Queryable
    include ::Enumerable

    ##
    # Queries `self` for RDF statements matching the given pattern.
    #
    # @param  [Query, Statement, Array(Value)] pattern
    # @yield  [statement]
    # @yieldparam [Statement]
    # @return [Array<Statement>, nil]
    def query(pattern, &block)
      raise TypeError.new("#{self} is not readable") if respond_to?(:readable) && !readable?

      case pattern
        when Query
          pattern.execute(self, &block)
        when Array
          query(Statement.new(*pattern), &block)
        when Statement
          if block_given?
            find_all { |statement| pattern === statement }.each(&block)
          else
            find_all { |statement| pattern === statement }
          end
        else
          raise ArgumentError.new("expected RDF::Query or RDF::Pattern, got #{pattern.inspect}")
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rdf-0.0.8 lib/rdf/mixin/queryable.rb