pragmatic_context 0.1.7 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3014f90e43fa32d1fc8bbab86e37528f8aca1edd
4
- data.tar.gz: cf284529ecca0ba696a1c3401c04dad1cbf5871a
3
+ metadata.gz: 198aee0dbd9458b89f2b3f0a18e0473bd058ac3f
4
+ data.tar.gz: 62e51d0200e8ac82ada1e4c3a93bd2449a7aca15
5
5
  SHA512:
6
- metadata.gz: 47b15e7682115ad0f02ac2edadbb925b0dbefe9d028f1d36cc0d5a7b50e1d233ed55c24a49bab1d1b9f20b97709635d371800434f74c4a16f867578a90c43a17
7
- data.tar.gz: 48db73767ecb14747cf0badfd5f1cf03fdd5026d56c9c03cdfb0016a07aaa21b5fe9035285a1ca727365b61e50e350313f70c2e71cfbc4e3e28694beb2b847d5
6
+ metadata.gz: e3819155b979530ae7858722b3ab46e27715358a316654356c99a2f6fc22ee4b6605645fef44c4d637b2397eafd76d5480467176d6cd23d338939fbd39f19215
7
+ data.tar.gz: 17da90b44683b519f69594cf975941a53e6f555b8abd34754496ed480a2d29af79eafbf417f104b58f0d789326574dca34d954fb24c77516a9ebe0da89bc2976
@@ -9,6 +9,7 @@ module PragmaticContext
9
9
  module ClassMethods
10
10
  attr_accessor :contextualizer
11
11
  attr_accessor :contextualized_type
12
+ attr_accessor :context_id_factory
12
13
 
13
14
  def contextualize_with(klass)
14
15
  self.contextualizer = klass.new
@@ -24,6 +25,11 @@ module PragmaticContext
24
25
  self.contextualized_type = type.nil? ? type : type.to_s
25
26
  end
26
27
 
28
+ def contextualize_with_id(&block)
29
+ raise "contextualize_with_id must be called with a block" unless block_given?
30
+ self.context_id_factory = block
31
+ end
32
+
27
33
  private
28
34
 
29
35
  def setup_default_contextualizer
@@ -42,6 +48,7 @@ module PragmaticContext
42
48
  terms_with_context = context.keys
43
49
  json_results = as_json(opts).slice(*terms_with_context)
44
50
  results = {}
51
+ results['@id'] = self.class.context_id_factory.call(self) if self.class.context_id_factory
45
52
  results['@type'] = self.class.contextualized_type if self.class.contextualized_type
46
53
  terms_with_context.each do |term|
47
54
  # Don't use idiomatic case here since Mongoid relations return proxies
@@ -1,3 +1,3 @@
1
1
  module PragmaticContext
2
- VERSION = "0.1.7"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -62,6 +62,18 @@ describe PragmaticContext::Contextualizable do
62
62
  subject.contextualized_type.should eq "http://schema.org/Person"
63
63
  end
64
64
  end
65
+
66
+ describe 'contextualize_with_id' do
67
+ it 'should set the id_factory on the class' do
68
+ block =->(obj) {}
69
+ subject.contextualize_with_id &block
70
+ subject.context_id_factory.should eq block
71
+ end
72
+
73
+ after :each do
74
+ subject.context_id_factory = nil
75
+ end
76
+ end
65
77
  end
66
78
 
67
79
  describe 'included instance methods' do
@@ -76,6 +88,25 @@ describe PragmaticContext::Contextualizable do
76
88
  end
77
89
 
78
90
  describe 'as_jsonld' do
91
+ describe 'contextualize_with_id' do
92
+ it 'should call the block with the object instance as its only argument' do
93
+ @contextualizer.stub(:definitions_for_terms) { {} }
94
+ called = false
95
+ subject.class.contextualize_with_id do |obj|
96
+ obj.should eq subject
97
+ called = true
98
+ end
99
+ subject.as_jsonld
100
+ called.should eq true
101
+ end
102
+
103
+ it 'should respond with @id if a factory block has been set' do
104
+ @contextualizer.stub(:definitions_for_terms) { {} }
105
+ subject.class.contextualize_with_id { |obj| 'http://bacon' }
106
+ subject.as_jsonld.should == { "@id" => "http://bacon", "@context" => {} }
107
+ end
108
+ end
109
+
79
110
  it 'should respond with @type if it has been set' do
80
111
  @contextualizer.stub(:definitions_for_terms) { {} }
81
112
  subject.class.contextualize_as_type 'Food'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pragmatic_context
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mat Trudel