Skip to content

Commit

Permalink
Support schemas via lib/linked_data_fragments
Browse files Browse the repository at this point in the history
This is the first step in a refactoring to move schemas to lib. Adds
tests for the schemas.
  • Loading branch information
Tom Johnson committed Feb 2, 2017
1 parent 52c72c5 commit 8fbe5a0
Show file tree
Hide file tree
Showing 12 changed files with 112 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/linked_data_fragments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'active_triples'

# must require 'rdf/vocab' first, due to const_missing metaprogramming in
# pre-2.0 verisons
require 'rdf/vocab'
require 'rdf/vocab/hydra'
require 'rdf/vocab/void'

require 'linked_data_fragments/schemas'

##
# A linked data caching fragment
module LinkedDataFragments
end
4 changes: 4 additions & 0 deletions lib/linked_data_fragments/schemas.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'linked_data_fragments/schemas/control_schema'
require 'linked_data_fragments/schemas/dataset_schema'
require 'linked_data_fragments/schemas/result_schema'
require 'linked_data_fragments/schemas/template_schema'
8 changes: 8 additions & 0 deletions lib/linked_data_fragments/schemas/control_schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module LinkedDataFragments
##
# A HydraCore control schema
class ControlSchema < ActiveTriples::Schema
property :variable, :predicate => RDF::Vocab::HYDRA.variable
property :property, :predicate => RDF::Vocab::HYDRA.property, :cast => false
end
end
12 changes: 12 additions & 0 deletions lib/linked_data_fragments/schemas/dataset_schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module LinkedDataFragments
##
# A schema for `hydracore:Collection`/`Dataset` nodes.
class DatasetSchema < ActiveTriples::Schema
property :subset, predicate: RDF::VOID.subset
property :uri_lookup_endpoint, predicate: RDF::VOID.uriLookupEndpoint

# Change search so that it points to a search node.
property :search, predicate: RDF::URI.intern("http://www.w3.org/ns/hydra/core#search")
property :member, predicate: RDF::URI.intern("http://www.w3.org/ns/hydra/core#member")
end
end
19 changes: 19 additions & 0 deletions lib/linked_data_fragments/schemas/result_schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module LinkedDataFragments
##
# Schema for Result model
class ResultSchema < ActiveTriples::Schema
property :subset, :predicate => RDF::VOID.subset

# Descriptive
property :title, :predicate => RDF::Vocab::DC.title
property :description, :predicate => RDF::Vocab::DC.description
property :source, :predicate => RDF::Vocab::DC.source

# Pagination
property :triples_count, :predicate => RDF::Vocab::VOID.triples
property :total_items, :predicate => RDF::URI("http://www.w3.org/ns/hydra/core#totalItems")
property :items_per_page, :predicate => RDF::URI("http://www.w3.org/ns/hydra/core#itemsPerPage")
property :first_page, :predicate => RDF::URI("http://www.w3.org/ns/hydra/core#firstPage")
end
end

8 changes: 8 additions & 0 deletions lib/linked_data_fragments/schemas/template_schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module LinkedDataFragments
##
# Schema for template models
class TemplateSchema < ActiveTriples::Schema
property :template, :predicate => RDF::Vocab::HYDRA.search
property :mapping, :predicate => RDF::Vocab::HYDRA.mapping
end
end
5 changes: 5 additions & 0 deletions spec/linked_data_fragments/schemas/control_schema_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'spec_helper'

describe LinkedDataFragments::ControlSchema do
it_behaves_like 'a schema', [:variable, :property]
end
5 changes: 5 additions & 0 deletions spec/linked_data_fragments/schemas/dataset_schema_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'spec_helper'

describe LinkedDataFragments::DatasetSchema do
it_behaves_like 'a schema', [:subset, :uri_lookup_endpoint, :search, :member]
end
13 changes: 13 additions & 0 deletions spec/linked_data_fragments/schemas/result_schema_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'spec_helper'

describe LinkedDataFragments::ResultSchema do
it_behaves_like 'a schema', [:subset,
:title,
:description,
:source,
:triples_count,
:total_items,
:items_per_page,
:first_page
]
end
5 changes: 5 additions & 0 deletions spec/linked_data_fragments/schemas/template_schema_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'spec_helper'

describe LinkedDataFragments::TemplateSchema do
it_behaves_like 'a schema', [:template, :mapping]
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'vcr_setup'
require 'linked_data_fragments'

Dir['./spec/support/**/*.rb'].each { |f| require f }

Expand Down
18 changes: 18 additions & 0 deletions spec/support/shared_examples/schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
shared_examples 'a schema' do |properties|
let(:fake_source) do
klass = Class.new { include ActiveTriples::RDFSource }
klass.apply_schema described_class
klass
end

let(:source) { fake_source.new }
let(:value) { :moomin }

properties.each do |property|
it "has configured property #{property}" do
expect { source.send("#{property}=".to_sym, value) }
.to change { source.send(property).to_a }
.to contain_exactly(value)
end
end
end

0 comments on commit 8fbe5a0

Please sign in to comment.