Class: RDF::LDP::InteractionModel

Inherits:
Object
  • Object
show all
Defined in:
lib/rdf/ldp/interaction_model.rb

Overview

Provides an interaction model registry.

Constant Summary collapse

@@interaction_models =

Interaction models are in reverse order of preference for POST/PUT requests; e.g. if a client sends a request with Resource, RDFSource, oand BasicContainer headers, the server gives a basic container.

Interaction models are initialized in the correct order, but with no class registered to handle them.

{
  RDF::LDP::RDFSource.to_uri         => nil,
  RDF::LDP::Container.to_uri         => nil,
  RDF::Vocab::LDP.BasicContainer     => nil,
  RDF::LDP::DirectContainer.to_uri   => nil,
  RDF::LDP::IndirectContainer.to_uri => nil,
  RDF::LDP::NonRDFSource.to_uri      => nil
}

Class Method Summary collapse

Class Method Details

.compatible?(uris) ⇒ TrueClass or FalseClass

Test an array of URIs to see if their interaction models are compatible (e.g., all of the URIs refer either to RDF models or non-RDF models, but not a combination of both).

Parameters:

  • uris (Array<RDF::URI>)

Returns:

  • (TrueClass or FalseClass)

    true if the models specified by ‘uris` are compatible



87
88
89
90
91
92
93
# File 'lib/rdf/ldp/interaction_model.rb', line 87

def compatible?(uris)
  classes        = uris.collect { |m| self.for(m) }
  (rdf, non_rdf) =
    classes.compact.partition { |c| c.ancestors.include?(RDFSource) }

  rdf.empty? || non_rdf.empty?
end

.defaultObject

The default registered interaction model



75
76
77
# File 'lib/rdf/ldp/interaction_model.rb', line 75

def default
  @@default
end

.find(uris) ⇒ Class

Find the appropriate interaction model given a set of Link header URIs.

Parameters:

  • uris (Array<RDF::URI>)

Returns:

  • (Class)

    a subclass of Resource that most narrowly matches the supplied ‘uris`, or the default interaction model if nothing matches



58
59
60
61
# File 'lib/rdf/ldp/interaction_model.rb', line 58

def find(uris)
  match = @@interaction_models.keys.reverse.find { |u| uris.include? u }
  self.for(match) || @@default
end

.for(uri) ⇒ Class

Find the interaction model registered for a given uri

Parameters:

  • uri (RDF::URI)

Returns:

  • (Class)

    the Resource subclass registered to ‘uri`



69
70
71
# File 'lib/rdf/ldp/interaction_model.rb', line 69

def for(uri)
  @@interaction_models[uri]
end

.register(klass, opts = {}) ⇒ RDF::LDP::Resource

Register a new interaction model for one or more Link header URIs. klass.to_uri will automatically be registered.

Parameters:

  • klass (RDF::LDP::Resource)

    the implementation class to register

  • opts (Hash <Symbol, *>) (defaults to: {})

    registration options: :default [true, false] if true, klass will become the new default

    klass for unrecognized Link headers
    

    :for [RDF::URI, Array<RDF::URI>] additional URIs for which klass

    should become the interaction model
    

Returns:



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rdf/ldp/interaction_model.rb', line 36

def register(klass, opts = {})
  unless klass.ancestors.include?(RDF::LDP::Resource)
    raise ArgumentError,
          'Interaction models must subclass `RDF::LDP::Resource`'
  end
  @@default = klass if opts[:default] || @@default.nil?
  @@interaction_models[klass.to_uri] = klass
  Array(opts[:for]).each do |model|
    @@interaction_models[model] = klass
  end
  klass
end