Skip to content
Open
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
27 changes: 24 additions & 3 deletions lib/seek/bio_schema/resource_decorators/base_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ class BaseDecorator
include ActionView::Helpers::SanitizeHelper
include Seek::Util.routes

attr_reader :resource
attr_reader :resource, :additional_context

def initialize(resource)
@resource = resource
@additional_context = {}
end

def mappings
Expand All @@ -26,7 +27,10 @@ def attributes

# The @context to be used for the JSON-LD
def context
Seek::BioSchema::Serializer::SCHEMA_ORG
{
'@vocab' => Seek::BioSchema::Serializer::SCHEMA_ORG,
dct: Seek::BioSchema::Serializer::DCT
}
end

# The schema.org @type .
Expand Down Expand Up @@ -78,10 +82,14 @@ def resource_url(resource, opts = {})
# associated_items member: :people
# create a method 'member' that returns a collection of Hash objects containing the
# minimal definition for each item resulting from calling 'people' on the resource
# Also adds to the additional_context the context for each item in the associated resources
def associated_items(**pairs)
pairs.each do |method, collection|
define_method(method) do
mini_definitions(send(collection)) if respond_to?(collection)
return unless respond_to?(collection)

@additional_context.merge!(additional_contexts(send(collection)))
mini_definitions(send(collection))
end
end
end
Expand Down Expand Up @@ -116,6 +124,19 @@ def mini_definitions(collection)
mini_col
end

def additional_contexts(collection)
return {} if collection.empty?

ctx = {}
collection.each do |item|
next if item.respond_to?(:public?) && !item.public?

decorator = Seek::BioSchema::ResourceDecorators::Factory.instance.get(item)
ctx.merge!(decorator.context)
end
ctx
end

def respond_to_missing?(name, include_private = false)
resource.respond_to?(name,
include_private) || resource.is_a_version? && resource.parent.respond_to?(name,
Expand Down
11 changes: 11 additions & 0 deletions lib/seek/bio_schema/resource_decorators/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ module ResourceDecorators
# Decorator that provides extensions for a Event
class Event < Thing
EVENT_PROFILE = 'https://bioschemas.org/profiles/Event/0.3-DRAFT'.freeze
HOST_INSTITUTION_PROPERTY = 'https://bioschemas.org/terms/hostInstitution'.freeze
CONTACT_PROPERTY = 'https://bioschemas.org/terms/contact'.freeze
EVENT_TYPE_PROPERTY = 'https://bioschemas.org/terms/eventType'.freeze

associated_items contact: :contributors,
host_institution: :projects,
Expand All @@ -19,6 +22,14 @@ class Event < Thing
created_at: :dateCreated,
updated_at: :dateModified

def context
super.merge(
contact: CONTACT_PROPERTY,
eventType: EVENT_TYPE_PROPERTY,
hostInstitution: HOST_INSTITUTION_PROPERTY
)
end

def conformance
EVENT_PROFILE
end
Expand Down
2 changes: 1 addition & 1 deletion lib/seek/bio_schema/resource_decorators/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Person < Thing
last_name: :familyName,
image: :image,
member_of: :memberOf,
orcid: :orcid,
orcid: :identifier,
Comment thread
stuzart marked this conversation as resolved.
works_for: :worksFor

def conformance
Expand Down
7 changes: 6 additions & 1 deletion lib/seek/bio_schema/resource_decorators/sample.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ class Sample < Thing
schema_mappings properties: :additionalProperty

SAMPLE_PROFILE = 'https://bioschemas.org/profiles/Sample/0.2-RELEASE-2018_11_10/'.freeze
SAMPLE_TYPE = 'https://bioschemas.org/types/Sample/0.2-DRAFT-2018_11_09'.freeze

def context
super.merge(Sample: SAMPLE_TYPE)
end

def schema_type
'Sample'
Expand All @@ -31,7 +36,7 @@ def describe_attribute(attribute)
'value' => value.to_s
}
if attribute.pid
data['propertyId'] = attribute.pid
data['propertyID'] = attribute.pid
end
resolved = attribute.resolve(value)
data['identifier'] = resolved if resolved
Expand Down
9 changes: 9 additions & 0 deletions lib/seek/bio_schema/resource_decorators/sop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,19 @@ module BioSchema
module ResourceDecorators
# Decorator that provides extensions for a Sop
class Sop < CreativeWork
LAB_PROTOCOL_TYPE = 'https://bioschemas.org/types/LabProtocol/0.5-DRAFT'.freeze
COMPUTATIONAL_TOOL_PROPERTY = 'https://bioschemas.org/terms/computationalTool'.freeze

associated_items computational_tool: :workflows
schema_mappings computational_tool: :computationalTool

def context
super.merge(
LabProtocol: LAB_PROTOCOL_TYPE,
computationalTool: COMPUTATIONAL_TOOL_PROPERTY
)
end

def schema_type
'LabProtocol'
end
Expand Down
14 changes: 13 additions & 1 deletion lib/seek/bio_schema/resource_decorators/workflow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,26 @@ module ResourceDecorators
# Decorator that provides extensions for a Workflow
class Workflow < CreativeWork
WORKFLOW_PROFILE = 'https://bioschemas.org/profiles/ComputationalWorkflow/1.0-RELEASE/'.freeze

WORKFLOW_TYPE = 'https://bioschemas.org/terms/ComputationalWorkflow'.freeze
FORMALPARAMETER_PROFILE = 'https://bioschemas.org/profiles/FormalParameter/1.0-RELEASE/'.freeze
FORMALPARAMETER_TYPE = 'https://bioschemas.org/terms/FormalParameter'.freeze
INPUT_PROPERTY = 'https://bioschemas.org/terms/input'.freeze
OUTPUT_PROPERTY = 'https://bioschemas.org/terms/output'.freeze

schema_mappings programming_language: :programmingLanguage,
inputs: :input,
outputs: :output,
sd_publisher: :sdPublisher

def context
super.merge(
input: INPUT_PROPERTY,
output: OUTPUT_PROPERTY,
ComputationalWorkflow: WORKFLOW_TYPE,
FormalParameter: FORMALPARAMETER_TYPE
)
end

def contributors
[contributor]
end
Expand Down
5 changes: 4 additions & 1 deletion lib/seek/bio_schema/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ class Serializer
include ActionView::Helpers::SanitizeHelper
attr_reader :resource

SCHEMA_ORG = 'https://schema.org'
SCHEMA_ORG = 'https://schema.org/'.freeze
DCT = 'http://purl.org/dc/terms/'.freeze

# initialise with a resource
def initialize(resource)
Expand All @@ -23,6 +24,8 @@ def json_representation
representation['dct:conformsTo'] = { '@id' => resource_decorator.conformance }
end
representation = representation.merge(attributes_json)
# After attributes_json has been generated, additional context will be populated
representation['@context'].merge!(resource_decorator.additional_context)
representation.deep_stringify_keys
end

Expand Down
2 changes: 1 addition & 1 deletion test/unit/bio_schema/data_dump_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class DataDumpTest < ActiveSupport::TestCase

assert_equal 'workflows-bioschemas-dump.jsonld', dump.file_name
assert dump.exists?
assert_in_delta 3800, dump.size, 500
assert_in_delta 5000, dump.size, 500
assert_in_delta Time.now, dump.date_modified, 60
end

Expand Down
6 changes: 5 additions & 1 deletion test/unit/bio_schema/decorator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ class DecoratorTest < ActiveSupport::TestCase

decorator = Seek::BioSchema::ResourceDecorators::Thing.new(data_file)
identifier = "http://localhost:3000/data_files/#{data_file.id}"
context = {
'@vocab' => Seek::BioSchema::Serializer::SCHEMA_ORG,
:dct => Seek::BioSchema::Serializer::DCT
}
assert_equal identifier, decorator.identifier
assert_equal identifier, decorator.url
assert_equal Seek::BioSchema::Serializer::SCHEMA_ORG, decorator.context
assert_equal context, decorator.context
assert_equal %w[blue green red], decorator.keywords.split(',').collect(&:strip).sort

properties = decorator.attributes.collect(&:property).collect(&:to_s).sort
Expand Down
Loading
Loading