rdf-serializers 0.0.6 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/lib/active_model_serializers/adapter/rdf.rb +88 -21
- data/lib/active_model_serializers/adapter/rdf/relationship.rb +12 -2
- data/lib/rdf/serializers.rb +1 -0
- data/lib/rdf/serializers/config.rb +23 -0
- data/lib/rdf/serializers/renderers.rb +2 -1
- data/lib/rdf/serializers/version.rb +1 -1
- metadata +9 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 3a641b5b49c74bdc4479196b25f4c70e5718fabb324a703e10e4f60d07fb8574
|
4
|
+
data.tar.gz: 45be5ccf9f007536f261332f396d65a8ecf8b2f63bd5ee9878320e50433a415c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d908f4bf5f1886890c23e9268779aa4755b3de4f2e5b1e4910f2117c21983a21fef01bc32a8af24b89942b4e1b2fc46d46d0b0481fea52831d86a8b779d42c9a
|
7
|
+
data.tar.gz: f9bdabf7f8455e09dd9ae273d565061c13f127f718cb271f9387cb442a0a5469daa4f0cda5b8058181f74d178c54ab4caae9ca6deb89f65f510a9dafae248d8f
|
@@ -1,5 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
# rubocop:disable Metrics/ClassLength
|
3
4
|
module ActiveModelSerializers
|
4
5
|
module Adapter
|
5
6
|
class RDF < Base
|
@@ -7,7 +8,6 @@ module ActiveModelSerializers
|
|
7
8
|
autoload :Relationship
|
8
9
|
|
9
10
|
delegate :object, to: :serializer
|
10
|
-
delegate :dump, :triples, to: :repository
|
11
11
|
|
12
12
|
def initialize(serializer, options = {})
|
13
13
|
super
|
@@ -16,29 +16,37 @@ module ActiveModelSerializers
|
|
16
16
|
@resource_identifiers = Set.new
|
17
17
|
end
|
18
18
|
|
19
|
+
def dump(*args, **options)
|
20
|
+
if include_named_graphs?(*args)
|
21
|
+
repository.dump(*args, options)
|
22
|
+
else
|
23
|
+
repository.project_graph(nil).dump(*args, options)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def triples(*args, **options)
|
28
|
+
if include_named_graphs?(*args)
|
29
|
+
repository.triples(*args, options)
|
30
|
+
else
|
31
|
+
repository.project_graph(nil).triples(*args, options)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
19
35
|
protected
|
20
36
|
|
21
37
|
attr_reader :fieldset
|
22
38
|
|
23
39
|
private
|
24
40
|
|
25
|
-
def add_attribute(subject, predicate, value)
|
41
|
+
def add_attribute(subject, predicate, value, graph)
|
26
42
|
return unless predicate
|
27
|
-
|
28
|
-
value.
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
when ::RDF::Term
|
35
|
-
object
|
36
|
-
when ActiveSupport::TimeWithZone
|
37
|
-
::RDF::Literal(object.to_datetime)
|
38
|
-
else
|
39
|
-
::RDF::Literal(object)
|
40
|
-
end
|
41
|
-
@repository << ::RDF::Statement.new(subject, ::RDF::URI(predicate), obj, graph_name: graph)
|
43
|
+
|
44
|
+
normalized = value.is_a?(Array) ? value : [value]
|
45
|
+
normalized.compact.map { |v| add_triple([subject, predicate, v, graph]) }
|
46
|
+
end
|
47
|
+
|
48
|
+
def add_triple(triple)
|
49
|
+
@repository << (triple.is_a?(Array) ? normalized_triple(*triple) : triple)
|
42
50
|
end
|
43
51
|
|
44
52
|
def attributes_for(serializer, fields)
|
@@ -46,7 +54,8 @@ module ActiveModelSerializers
|
|
46
54
|
add_attribute(
|
47
55
|
serializer.read_attribute_for_serialization(:rdf_subject),
|
48
56
|
serializer.class._attributes_data[key].try(:options).try(:[], :predicate),
|
49
|
-
value
|
57
|
+
value,
|
58
|
+
serializer.class._attributes_data[key].try(:options).try(:[], :graph)
|
50
59
|
)
|
51
60
|
end
|
52
61
|
end
|
@@ -54,30 +63,74 @@ module ActiveModelSerializers
|
|
54
63
|
def custom_triples_for(serializer)
|
55
64
|
serializer.class.try(:_triples)&.map do |key|
|
56
65
|
serializer.read_attribute_for_serialization(key).each do |triple|
|
57
|
-
|
66
|
+
add_triple(triple)
|
58
67
|
end
|
59
68
|
end
|
60
69
|
end
|
61
70
|
|
62
71
|
def repository
|
63
72
|
return @repository if @repository.present?
|
73
|
+
|
64
74
|
@repository = ::RDF::Repository.new
|
65
75
|
|
66
76
|
serializers.each { |serializer| process_resource(serializer, @include_directive) }
|
67
77
|
serializers.each { |serializer| process_relationships(serializer, @include_directive) }
|
68
|
-
instance_options[:meta]&.each { |meta| add_triple(
|
78
|
+
instance_options[:meta]&.each { |meta| add_triple(meta) }
|
79
|
+
|
80
|
+
raise_missing_nodes if raise_on_missing_nodes?
|
69
81
|
|
70
82
|
@repository
|
71
83
|
end
|
72
84
|
|
85
|
+
def include_named_graphs?(*args)
|
86
|
+
::RDF::Serializers.config.always_include_named_graphs ||
|
87
|
+
::RDF::Writer.for(*args.presence || :nquads).instance_methods.include?(:write_quad)
|
88
|
+
end
|
89
|
+
|
90
|
+
def missing_nodes
|
91
|
+
@missing_nodes ||=
|
92
|
+
@repository
|
93
|
+
.objects
|
94
|
+
.select(&:node?)
|
95
|
+
.reject { |n| @repository.has_subject?(n) }
|
96
|
+
.map { |n| @repository.query([nil, nil, n]).first }
|
97
|
+
end
|
98
|
+
|
99
|
+
def normalized_object(object) # rubocop:disable Metrics/MethodLength
|
100
|
+
case object
|
101
|
+
when ::RDF::Term
|
102
|
+
object
|
103
|
+
when ::RDF::List
|
104
|
+
list = object.statements
|
105
|
+
@repository << object.statements
|
106
|
+
list.first.subject
|
107
|
+
when ActiveSupport::TimeWithZone
|
108
|
+
::RDF::Literal(object.to_datetime)
|
109
|
+
else
|
110
|
+
::RDF::Literal(object)
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
def normalized_triple(subject, predicate, object, graph = nil)
|
115
|
+
::RDF::Statement.new(
|
116
|
+
subject,
|
117
|
+
::RDF::URI(predicate),
|
118
|
+
normalized_object(object),
|
119
|
+
graph_name: graph || ::RDF::Serializers.config.default_graph
|
120
|
+
)
|
121
|
+
end
|
122
|
+
|
73
123
|
def process_relationship(serializer, include_slice)
|
74
124
|
return serializer.each { |s| process_relationship(s, include_slice) } if serializer.respond_to?(:each)
|
125
|
+
|
75
126
|
return unless serializer&.object && process_resource(serializer, include_slice)
|
127
|
+
|
76
128
|
process_relationships(serializer, include_slice)
|
77
129
|
end
|
78
130
|
|
79
131
|
def process_relationships(serializer, include_slice)
|
80
132
|
return unless serializer.respond_to?(:associations)
|
133
|
+
|
81
134
|
serializer.associations(include_slice).each do |association|
|
82
135
|
process_relationship(association.lazy_association.serializer, include_slice[association.key])
|
83
136
|
end
|
@@ -88,16 +141,28 @@ module ActiveModelSerializers
|
|
88
141
|
return serializer.map { |child| process_resource(child, include_slice) }
|
89
142
|
end
|
90
143
|
return unless serializer.respond_to?(:rdf_subject) || serializer.object.respond_to?(:rdf_subject)
|
144
|
+
|
91
145
|
return false unless @resource_identifiers.add?(serializer.read_attribute_for_serialization(:rdf_subject))
|
146
|
+
|
92
147
|
resource_object_for(serializer, include_slice)
|
93
148
|
true
|
94
149
|
end
|
95
150
|
|
151
|
+
def raise_on_missing_nodes?
|
152
|
+
Rails.env.development? || Rails.env.test?
|
153
|
+
end
|
154
|
+
|
155
|
+
def raise_missing_nodes
|
156
|
+
return if missing_nodes.empty?
|
157
|
+
|
158
|
+
raise "The following triples point to nodes that are not included in the graph:\n#{missing_nodes.join("\n")}"
|
159
|
+
end
|
160
|
+
|
96
161
|
def relationships_for(serializer, requested_associations, include_slice)
|
97
162
|
include_directive = JSONAPI::IncludeDirective.new(requested_associations, allow_wildcard: true)
|
98
163
|
serializer.associations(include_directive, include_slice).each do |association|
|
99
164
|
Relationship.new(serializer, instance_options, association).triples.each do |triple|
|
100
|
-
|
165
|
+
add_triple(triple)
|
101
166
|
end
|
102
167
|
end
|
103
168
|
end
|
@@ -106,6 +171,7 @@ module ActiveModelSerializers
|
|
106
171
|
type = type_for(serializer, instance_options).to_s
|
107
172
|
serializer.fetch(self) do
|
108
173
|
break nil if serializer.read_attribute_for_serialization(:rdf_subject).nil?
|
174
|
+
|
109
175
|
requested_fields = fieldset&.fields_for(type)
|
110
176
|
attributes_for(serializer, requested_fields)
|
111
177
|
custom_triples_for(serializer)
|
@@ -124,3 +190,4 @@ module ActiveModelSerializers
|
|
124
190
|
end
|
125
191
|
end
|
126
192
|
end
|
193
|
+
# rubocop:enable Metrics/ClassLength
|
@@ -5,10 +5,12 @@ module ActiveModelSerializers
|
|
5
5
|
class RDF
|
6
6
|
class Relationship < JsonApi::Relationship
|
7
7
|
def triples
|
8
|
-
return [] if
|
8
|
+
return [] if no_data?
|
9
|
+
|
9
10
|
data.map do |object|
|
10
11
|
raise "#{object} is not a RDF::Resource but a #{object.class}" unless object.is_a?(::RDF::Resource)
|
11
|
-
|
12
|
+
|
13
|
+
::RDF::Statement.new(subject, predicate, object, graph_name: graph_name)
|
12
14
|
end
|
13
15
|
end
|
14
16
|
|
@@ -23,6 +25,14 @@ module ActiveModelSerializers
|
|
23
25
|
end
|
24
26
|
end
|
25
27
|
|
28
|
+
def graph_name
|
29
|
+
association.reflection.options[:graph] || ::RDF::Serializers.config.default_graph
|
30
|
+
end
|
31
|
+
|
32
|
+
def no_data?
|
33
|
+
subject.blank? || predicate.blank? || data.empty?
|
34
|
+
end
|
35
|
+
|
26
36
|
def objects_for_many(association)
|
27
37
|
collection_serializer = association.lazy_association.serializer
|
28
38
|
if collection_serializer.respond_to?(:each)
|
data/lib/rdf/serializers.rb
CHANGED
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RDF
|
4
|
+
module Serializers
|
5
|
+
def self.configure
|
6
|
+
yield @config ||= RDF::Serializers::Configuration.new
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.config
|
10
|
+
@config
|
11
|
+
end
|
12
|
+
|
13
|
+
class Configuration
|
14
|
+
include ActiveSupport::Configurable
|
15
|
+
config_accessor :always_include_named_graphs
|
16
|
+
config_accessor :default_graph
|
17
|
+
end
|
18
|
+
|
19
|
+
configure do |config|
|
20
|
+
config.always_include_named_graphs = true
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -16,7 +16,8 @@ module RDF
|
|
16
16
|
symbols = [symbols] unless symbols.respond_to?(:each)
|
17
17
|
symbols.each do |symbol|
|
18
18
|
format = RDF::Format.for(symbol)
|
19
|
-
raise "#{symbol}
|
19
|
+
raise "#{symbol} is not a known rdf format" if format.nil?
|
20
|
+
|
20
21
|
Mime::Type.register format.content_type.first, format.file_extension.first
|
21
22
|
add_renderer(format, opts)
|
22
23
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rdf-serializers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Arthur Dingemans
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-10-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: active_model_serializers
|
@@ -33,7 +33,7 @@ dependencies:
|
|
33
33
|
version: 4.2.0
|
34
34
|
- - "<"
|
35
35
|
- !ruby/object:Gem::Version
|
36
|
-
version: '
|
36
|
+
version: '7'
|
37
37
|
type: :runtime
|
38
38
|
prerelease: false
|
39
39
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: 4.2.0
|
44
44
|
- - "<"
|
45
45
|
- !ruby/object:Gem::Version
|
46
|
-
version: '
|
46
|
+
version: '7'
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: rdf
|
49
49
|
requirement: !ruby/object:Gem::Requirement
|
@@ -68,10 +68,12 @@ files:
|
|
68
68
|
- lib/active_model_serializers/adapter/rdf/relationship.rb
|
69
69
|
- lib/active_model_serializers/serializer.rb
|
70
70
|
- lib/rdf/serializers.rb
|
71
|
+
- lib/rdf/serializers/config.rb
|
71
72
|
- lib/rdf/serializers/renderers.rb
|
72
73
|
- lib/rdf/serializers/version.rb
|
73
|
-
homepage: https://github.com/
|
74
|
-
licenses:
|
74
|
+
homepage: https://github.com/ontola/rdf-serializers
|
75
|
+
licenses:
|
76
|
+
- MIT
|
75
77
|
metadata: {}
|
76
78
|
post_install_message:
|
77
79
|
rdoc_options: []
|
@@ -88,8 +90,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
88
90
|
- !ruby/object:Gem::Version
|
89
91
|
version: '0'
|
90
92
|
requirements: []
|
91
|
-
|
92
|
-
rubygems_version: 2.6.11
|
93
|
+
rubygems_version: 3.0.1
|
93
94
|
signing_key:
|
94
95
|
specification_version: 4
|
95
96
|
summary: Adds RDF serialization, like n-triples or turtle, to active model serializers
|