pragmatic_context 0.1.2 → 0.1.3
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
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a60bf7a2275e2574efef334b737bd33f8f906257
|
4
|
+
data.tar.gz: 7af80e83673d61cfa2878280783ac29fd0be7bf5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 00e267df94129526296bd61a1e810b45c87c29e71121f95deb93272a8203ebfade3f94750746f4eaace246ba0afdb2d450bc6ace4f65421471c472468f414fd5
|
7
|
+
data.tar.gz: fb7bbae5beda4427d6ca15551ce7aa01a3782e21e03dc3bdf992d3f50aebc29e6c2d61eb01b2c862657f111d114acb87ddea6f46450a45e18dad97f3823249ff
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# PragmaticContext
|
2
2
|
|
3
|
-
PragmaticContext
|
3
|
+
PragmaticContext allows you to declaratively contextualize your
|
4
4
|
ActiveModel objects in terms of Linked Data concepts, giving you an easy way to
|
5
5
|
get your models talking JSON-LD.
|
6
6
|
|
@@ -111,9 +111,11 @@ it like so:
|
|
111
111
|
field :last_name
|
112
112
|
field :email
|
113
113
|
|
114
|
-
|
115
|
-
|
116
|
-
contextualize :
|
114
|
+
contextualize_as_type 'https://schema.org/Person'
|
115
|
+
|
116
|
+
contextualize :first_name, :as => 'http://schema.org/givenName'
|
117
|
+
contextualize :last_name, :as => 'http://schema.org/familyName'
|
118
|
+
contextualize :email, :as => 'http://schema.org/email'
|
117
119
|
end
|
118
120
|
|
119
121
|
This gives Pragmatic Context everything it needs to be able to create JSON-LD
|
@@ -128,10 +130,11 @@ the `Person` object described above will produce the following JSON-LD document:
|
|
128
130
|
|
129
131
|
{
|
130
132
|
"@context": {
|
131
|
-
"first_name": { "@id", "http://
|
132
|
-
"last_name": { "@id", "http://
|
133
|
-
"email": { "@id", "http://
|
133
|
+
"first_name": { "@id", "http://schema.org/givenName" },
|
134
|
+
"last_name": { "@id", "http://schema.org/familyName" },
|
135
|
+
"email": { "@id", "http://schema.org/email" },
|
134
136
|
},
|
137
|
+
"@type": "http://schema.org/Person",
|
135
138
|
"first_name": "Mat",
|
136
139
|
"last_name": "Trudel",
|
137
140
|
"email": "mat@geeky.net"
|
@@ -144,6 +147,9 @@ A couple of things to note about this implementation:
|
|
144
147
|
a document which do not have a context defined.
|
145
148
|
* The produced JSON-LD document embeds the context directly in the document,
|
146
149
|
within the `@context` field.
|
150
|
+
* If a type is given for the contextualized class (via the
|
151
|
+
`contextualize_as_type` statement), it will have a matching `@type` field in
|
152
|
+
its JSON-LD representation.
|
147
153
|
* For each field present in your object's `as_json` method *that has a matching
|
148
154
|
`contextualize` statement*, `as_jsonld` will:
|
149
155
|
* If the field is a primitive value (string, number, boolean, nil), its
|
@@ -8,6 +8,7 @@ module PragmaticContext
|
|
8
8
|
|
9
9
|
module ClassMethods
|
10
10
|
attr_accessor :contextualizer
|
11
|
+
attr_accessor :contextualized_type
|
11
12
|
|
12
13
|
def contextualize_with(klass)
|
13
14
|
self.contextualizer = klass.new
|
@@ -18,6 +19,10 @@ module PragmaticContext
|
|
18
19
|
self.contextualizer.add_term(field, params)
|
19
20
|
end
|
20
21
|
|
22
|
+
def contextualize_as_type(type)
|
23
|
+
self.contextualized_type = type
|
24
|
+
end
|
25
|
+
|
21
26
|
private
|
22
27
|
|
23
28
|
def setup_default_contextualizer
|
@@ -35,6 +40,7 @@ module PragmaticContext
|
|
35
40
|
terms_with_context = self.class.contextualizer.definitions_for_terms(terms).keys
|
36
41
|
json_results = as_json(opts).slice(*terms_with_context)
|
37
42
|
results = {}
|
43
|
+
results['@type'] = self.class.contextualized_type if self.class.contextualized_type
|
38
44
|
terms_with_context.each do |term|
|
39
45
|
# Don't use idiomatic case here since Mongoid relations return proxies
|
40
46
|
# that fail the Contextualizable test
|
@@ -44,6 +44,13 @@ describe PragmaticContext::Contextualizable do
|
|
44
44
|
subject.contextualize :bacon, :as => 'http://bacon.yum'
|
45
45
|
end
|
46
46
|
end
|
47
|
+
|
48
|
+
describe 'contextualize_as_type' do
|
49
|
+
it 'should set the type on the class' do
|
50
|
+
subject.contextualize_as_type 'bacon'
|
51
|
+
subject.contextualized_type.should eq 'bacon'
|
52
|
+
end
|
53
|
+
end
|
47
54
|
end
|
48
55
|
|
49
56
|
describe 'included instance methods' do
|
@@ -53,10 +60,17 @@ describe PragmaticContext::Contextualizable do
|
|
53
60
|
@contextualizer = double('contextualizer')
|
54
61
|
contextualizer_class = double('contextualizer class')
|
55
62
|
contextualizer_class.stub(:new) { @contextualizer }
|
63
|
+
subject.class.contextualize_as_type nil
|
56
64
|
subject.class.contextualize_with contextualizer_class
|
57
65
|
end
|
58
66
|
|
59
67
|
describe 'as_jsonld' do
|
68
|
+
it 'should respond with @type if it has been set' do
|
69
|
+
@contextualizer.stub(:definitions_for_terms) { {} }
|
70
|
+
subject.class.contextualize_as_type 'Food'
|
71
|
+
subject.as_jsonld.should == { "@type" => "Food", "@context" => {} }
|
72
|
+
end
|
73
|
+
|
60
74
|
it 'should respond with only contextualized terms plus their context' do
|
61
75
|
@contextualizer.stub(:definitions_for_terms) do |terms|
|
62
76
|
{ 'bacon' => { "@id" => "http://bacon.yum" } }.slice(*terms)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pragmatic_context
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mat Trudel
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-07-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|