Skip to content

Commit

Permalink
Merge pull request #39 from ActiveTriples/feature/refactor-schemas
Browse files Browse the repository at this point in the history
Refactor Schemas into LinkedDataFragments Namespace
  • Loading branch information
scande3 authored Feb 3, 2017
2 parents 00b0a57 + 8fbe5a0 commit 56245b5
Show file tree
Hide file tree
Showing 26 changed files with 161 additions and 41 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ group :development, :test do
end

group :development do
gem 'guard-rspec'
# Access an IRB console on exception pages or by using <%= console %> in views
gem 'web-console', '~> 2.0'
end
Expand Down
34 changes: 34 additions & 0 deletions Guardfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# A sample Guardfile
# More info at https://github.com/guard/guard#readme

## Uncomment and set this to only include directories you want to watch
# directories %w(app lib config test spec features) \
# .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")}

## Note: if you are using the `directories` clause above and you are not
## watching the project directory ('.'), then you will want to move
## the Guardfile to a watched dir and symlink it back, e.g.
#
# $ mkdir config
# $ mv Guardfile config/
# $ ln -s config/Guardfile .
#
# and, you'll have to watch "config/Guardfile" instead of "Guardfile"

# Using -W0 to suppress warnings in automatic test runs
guard :rspec, cmd: 'RUBYOPT="-W0" bundle exec rspec' do
require 'guard/rspec/dsl'
dsl = Guard::RSpec::Dsl.new(self)

# Feel free to open issues for suggestions and improvements

# RSpec files
rspec = dsl.rspec
watch(rspec.spec_helper) { rspec.spec_dir }
watch(rspec.spec_support) { rspec.spec_dir }
watch(rspec.spec_files)

# Ruby files
ruby = dsl.ruby
dsl.watch_spec_files_for(ruby.lib_files)
end
2 changes: 1 addition & 1 deletion app/models/control.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Control
include ActiveTriples::RDFSource
apply_schema ControlSchema
apply_schema LinkedDataFragments::ControlSchema
end
3 changes: 2 additions & 1 deletion app/models/dataset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ class Dataset
RDF::URI.intern("http://www.w3.org/ns/hydra/core#Collection"),
RDF::Vocab::VOID.Dataset
]
apply_schema DatasetSchema

apply_schema LinkedDataFragments::DatasetSchema
end
2 changes: 1 addition & 1 deletion app/models/result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ class Result
RDF::URI("http://www.w3.org/ns/hydra/core#Collection"),
RDF::URI("http://www.w3.org/ns/hydra/core#PagedCollection")
]
apply_schema ResultSchema

apply_schema LinkedDataFragments::ResultSchema
end
3 changes: 2 additions & 1 deletion app/models/template.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Template < ActiveTriples::Resource
include ActiveTriples::RDFSource
apply_schema TemplateSchema

apply_schema LinkedDataFragments::TemplateSchema
end
4 changes: 0 additions & 4 deletions app/schemas/control_schema.rb

This file was deleted.

10 changes: 0 additions & 10 deletions app/schemas/dataset_schema.rb

This file was deleted.

14 changes: 0 additions & 14 deletions app/schemas/result_schema.rb

This file was deleted.

4 changes: 0 additions & 4 deletions app/schemas/template_schema.rb

This file was deleted.

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
2 changes: 1 addition & 1 deletion spec/models/control_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
subject { described_class.new }

it "should apply the control schema" do
ControlSchema.properties.each do |property|
LinkedDataFragments::ControlSchema.properties.each do |property|
expect(subject.class.properties[property.name.to_s].predicate).to eq property.predicate
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/models/dataset_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
end

it "should apply the dataset schema" do
DatasetSchema.properties.each do |property|
LinkedDataFragments::DatasetSchema.properties.each do |property|
expect(subject.class.properties[property.name.to_s].predicate).to eq property.predicate
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/models/result_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
end

it "should apply the Result schema" do
ResultSchema.properties.each do |property|
LinkedDataFragments::ResultSchema.properties.each do |property|
expect(subject.class.properties[property.name.to_s].predicate).to eq property.predicate
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/models/template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
let(:uri) { RDF::Node.new("triplePattern") }

it "should apply the template schema" do
TemplateSchema.properties.each do |property|
LinkedDataFragments::TemplateSchema.properties.each do |property|
expect(subject.class.properties[property.name.to_s].predicate).to eq property.predicate
end
end
Expand Down
6 changes: 5 additions & 1 deletion 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 Expand Up @@ -45,7 +46,10 @@
end

config.before(:each) do
allow(Setting).to receive(:cache_backend).and_return('repository')
# @todo: Eliminate this.
if Object.const_defined?('Setting')
allow(Setting).to receive(:cache_backend).and_return('repository')
end
end

# The settings below are suggested to provide a good initial experience
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 56245b5

Please sign in to comment.