Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Models for Independence from the Rails App #41

Merged
merged 8 commits into from
Feb 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 0 additions & 20 deletions app/builders/control_builder.rb

This file was deleted.

69 changes: 0 additions & 69 deletions app/builders/dataset_builder.rb

This file was deleted.

19 changes: 0 additions & 19 deletions app/builders/template_builder.rb

This file was deleted.

5 changes: 1 addition & 4 deletions app/controllers/dataset_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
class DatasetController < ApplicationController

def index
@data = built_dataset

Expand All @@ -15,8 +14,6 @@ def index
private

def built_dataset
DatasetBuilder.new.build
LinkedDataFragments::DatasetBuilder.new.build
end


end
Empty file removed app/models/concerns/.keep
Empty file.
4 changes: 0 additions & 4 deletions app/models/control.rb

This file was deleted.

13 changes: 0 additions & 13 deletions app/models/dataset.rb

This file was deleted.

22 changes: 0 additions & 22 deletions app/models/hydra_template.rb

This file was deleted.

9 changes: 0 additions & 9 deletions app/models/result.rb

This file was deleted.

63 changes: 2 additions & 61 deletions app/models/setting.rb
Original file line number Diff line number Diff line change
@@ -1,62 +1,3 @@
##
# A class to hold site-wide configuration.
# @todo extract to a configuration file.
class Setting
class << self
def config
@config ||= YAML::load(File.open(config_path))[env]
.with_indifferent_access
end

def app_root
return @app_root if @app_root
@app_root = Rails.root if defined?(Rails) and defined?(Rails.root)
@app_root ||= APP_ROOT if defined?(APP_ROOT)
@app_root ||= '.'
end

def env
return @env if @env
#The following commented line always returns "test" in a rails c production console. Unsure of how to fix this yet...
#@env = ENV["RAILS_ENV"] = "test" if ENV
@env ||= Rails.env if defined?(Rails) and defined?(Rails.root)
@env ||= 'development'
end

def config_path
File.join(app_root, 'config', 'ldf.yml')
end

def uri_endpoint
Setting.config[:uri_endpoint] || 'http://localhost:3000/{?subject}'
end

def uri_endpoint_route
if uri_endpoint.match(/^http[s]*\:\/\/.+\//)
endpoint = uri_endpoint.gsub(/^http[s]*\:\/\/[^\/]+/, '')
endpoint.gsub!('{?subject}', '*subject')
else
#FIXME: What type of error should this be? Need to unit test this as well once figured out.
raise ArgumentError, 'Invalid uri endpoint url specified'
end

return endpoint
end

def uri_root
Setting.config[:uri_root] || 'http://localhost:3000/#dataset'
end

def cache_backend
Setting.config[:cache_backend][:provider] || 'marmotta'
end

def cache_backend_url
Setting.config[:cache_backend][:url] || 'http://localhost:8988/marmotta'
end

def cache_backend_context
Setting.config[:cache_backend][:context] || 'linked_data_fragments_unknown'
end
end
end
# An alias to LinkedDataFragments::Settings
Setting = LinkedDataFragments::Settings
5 changes: 0 additions & 5 deletions app/models/template.rb

This file was deleted.

7 changes: 6 additions & 1 deletion lib/linked_data_fragments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@

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

require 'linked_data_fragments/settings'

require 'linked_data_fragments/builders'
require 'linked_data_fragments/schemas'
require 'linked_data_fragments/models'
require 'linked_data_fragments/hydra_template'

##
# A linked data caching fragment
Expand Down
3 changes: 3 additions & 0 deletions lib/linked_data_fragments/builders.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require 'linked_data_fragments/builders/control_builder'
require 'linked_data_fragments/builders/dataset_builder'
require 'linked_data_fragments/builders/template_builder'
26 changes: 26 additions & 0 deletions lib/linked_data_fragments/builders/control_builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module LinkedDataFragments
class ControlBuilder
##
# @!attribute [rw] control
# @return []
# @!attribute [rw] property
# @return [RDF::URI]
attr_accessor :control, :property

##
#
def initialize(control, property)
@control = control
@property = property
end

##
# @return [Control]
def build
Control.new.tap do |t|
t.variable = control
t.property = property
end
end
end
end
69 changes: 69 additions & 0 deletions lib/linked_data_fragments/builders/dataset_builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
module LinkedDataFragments
##
# A Builder for Dataset instances.
#
# @example Building a dataset
# builder = DatasetBuilder.new
# builder.uri_endpoint = HydraTemplate.new('http://example.com/{?subject}')
# dataset = builder.build
#
# dataset.dump :ttl
# # <http://localhost:3000/#dataset> a <http://www.w3.org/ns/hydra/core#Collection>,
# # <http://rdfs.org/ns/void#Dataset>;
# # <http://rdfs.org/ns/void#uriLookupEndpoint> "http://example.com/{?subject}";
# # <http://www.w3.org/ns/hydra/core#search> [
# # <http://www.w3.org/ns/hydra/core#mapping> [
# # <http://www.w3.org/ns/hydra/core#property> <http://www.w3.org/1999/02/22-rdf-syntax-ns#subject>;
# # <http://www.w3.org/ns/hydra/core#variable> "subject"
# # ];
# # <http://www.w3.org/ns/hydra/core#search> "http://example.com/{?subject}"
# # ] .
class DatasetBuilder
# @!attribute [w]
# @return [HydraTemplate]
attr_writer :uri_endpoint

def build
Dataset.new(uri_root).tap do |dataset|
dataset.uri_lookup_endpoint = uri_endpoint.to_s
dataset.search = template_builder.new(dataset, uri_endpoint).build

uri_endpoint.controls.each do |control|
dataset.search.first.mapping <<
control_builder.new(control, control_mapping[control]).build
end
end
end

##
# @return [HydraTemplate]
# @see Settings#uri_endpoint
def uri_endpoint
@uri_endpoint ||=
LinkedDataFragments::HydraTemplate
.new(Settings.uri_endpoint)
end

##
# @return [String] a URI-like string representing the root URI
#
# @see Settings#uri_root
def uri_root
Settings.uri_root
end

private

def template_builder
TemplateBuilder
end

def control_builder
ControlBuilder
end

def control_mapping
{ "subject" => RDF.subject }
end
end
end
26 changes: 26 additions & 0 deletions lib/linked_data_fragments/builders/template_builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module LinkedDataFragments
##
# A Builder for Templates.
class TemplateBuilder
##
# @!attribute [r] dataset_node
# @return [Dataset]
# @!attribute [r] uri_template
# @return [String]
attr_reader :dataset_node, :uri_template

##
# @param dataset_node [Dataset]
# @param uri_template [#to_s]
def initialize(dataset_node, uri_template)
@dataset_node = dataset_node
@uri_template = uri_template.to_s
end

def build
Template.new(nil, dataset_node).tap do |template|
template.template = self.uri_template
end
end
end
end
Loading