Skip to content

Commit c76a7eb

Browse files
committed
Make API consistently return either result of yield, or the result. This allows a better flow when blocks are used and the results come from the evaluated block.
1 parent 672a58f commit c76a7eb

File tree

2 files changed

+26
-27
lines changed

2 files changed

+26
-27
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ rvm:
1010
- 2.2
1111
- jruby
1212
cache: bundler
13+
sudo: false
1314
matrix:
1415
allow_failures:
1516
- rvm: 2.2

lib/json/ld/api.rb

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,9 @@ def initialize(input, context, options = {}, &block)
154154
# @yield jsonld
155155
# @yieldparam [Array<Hash>] jsonld
156156
# The expanded JSON-LD document
157-
# @return [Array<Hash>]
158-
# The expanded JSON-LD document
157+
# @yieldreturn [Object] returned object
158+
# @return [Object, Array<Hash>]
159+
# If a block is given, the result of evaluating the block is returned, otherwise, the expanded JSON-LD document
159160
# @see http://json-ld.org/spec/latest/json-ld-api/#expansion-algorithm
160161
def self.expand(input, options = {})
161162
result = nil
@@ -169,8 +170,7 @@ def self.expand(input, options = {})
169170

170171
# Finally, if element is a JSON object, it is wrapped into an array.
171172
result = [result].compact unless result.is_a?(Array)
172-
yield result if block_given?
173-
result
173+
block_given? ? yield(result) : result
174174
end
175175

176176
##
@@ -191,8 +191,9 @@ def self.expand(input, options = {})
191191
# @yield jsonld
192192
# @yieldparam [Hash] jsonld
193193
# The compacted JSON-LD document
194-
# @return [Hash]
195-
# The compacted JSON-LD document
194+
# @yieldreturn [Object] returned object
195+
# @return [Object, Hash]
196+
# If a block is given, the result of evaluating the block is returned, otherwise, the compacted JSON-LD document
196197
# @raise [JsonLdError]
197198
# @see http://json-ld.org/spec/latest/json-ld-api/#compaction-algorithm
198199
def self.compact(input, context, options = {})
@@ -214,8 +215,7 @@ def self.compact(input, context, options = {})
214215
end
215216
result = ctx.merge(result) unless ctx.empty?
216217
end
217-
yield result if block_given?
218-
result
218+
block_given? ? yield(result) : result
219219
end
220220

221221
##
@@ -233,8 +233,9 @@ def self.compact(input, context, options = {})
233233
# @yield jsonld
234234
# @yieldparam [Hash] jsonld
235235
# The framed JSON-LD document
236-
# @return [Array<Hash>]
237-
# The framed JSON-LD document
236+
# @yieldreturn [Object] returned object
237+
# @return [Object, Hash]
238+
# If a block is given, the result of evaluating the block is returned, otherwise, the flattened JSON-LD document
238239
# @raise [InvalidFrame]
239240
# @see http://json-ld.org/spec/latest/json-ld-api/#framing-algorithm
240241
def self.flatten(input, context, options = {})
@@ -273,8 +274,7 @@ def self.flatten(input, context, options = {})
273274
end
274275
end
275276

276-
yield flattened if block_given?
277-
flattened
277+
block_given? ? yield(flattened) : flattened
278278
end
279279

280280
##
@@ -303,8 +303,9 @@ def self.flatten(input, context, options = {})
303303
# @yield jsonld
304304
# @yieldparam [Hash] jsonld
305305
# The framed JSON-LD document
306-
# @return [Array<Hash>]
307-
# The framed JSON-LD document
306+
# @yieldreturn [Object] returned object
307+
# @return [Object, Hash]
308+
# If a block is given, the result of evaluating the block is returned, otherwise, the framed JSON-LD document
308309
# @raise [InvalidFrame]
309310
# @see http://json-ld.org/spec/latest/json-ld-api/#framing-algorithm
310311
def self.frame(input, frame, options = {})
@@ -372,8 +373,7 @@ def self.frame(input, frame, options = {})
372373
result = cleanup_preserve(result)
373374
end
374375

375-
yield result if block_given?
376-
result
376+
block_given? ? yield(result) : result
377377
end
378378

379379
##
@@ -389,6 +389,7 @@ def self.frame(input, frame, options = {})
389389
# @raise [JsonLdError]
390390
# @yield statement
391391
# @yieldparam [RDF::Statement] statement
392+
# @return [RDF::Enumerable] set of statements, unless a block is given.
392393
def self.toRdf(input, options = {}, &block)
393394
unless block_given?
394395
results = []
@@ -448,7 +449,6 @@ def self.toRdf(input, options = {}, &block)
448449
end
449450
end
450451
end
451-
results
452452
end
453453

454454
##
@@ -462,8 +462,9 @@ def self.toRdf(input, options = {}, &block)
462462
# @yield jsonld
463463
# @yieldparam [Hash] jsonld
464464
# The JSON-LD document in expanded form
465-
# @return [Array<Hash>]
466-
# The JSON-LD document in expanded form
465+
# @yieldreturn [Object] returned object
466+
# @return [Object, Hash]
467+
# If a block is given, the result of evaluating the block is returned, otherwise, the expanded JSON-LD document
467468
def self.fromRdf(input, options = {}, &block)
468469
options = {useNativeTypes: false}.merge(options)
469470
result = nil
@@ -472,8 +473,7 @@ def self.fromRdf(input, options = {}, &block)
472473
result = api.from_statements(input)
473474
end
474475

475-
yield result if block_given?
476-
result
476+
block_given? ? yield(result) : result
477477
end
478478

479479
##
@@ -482,9 +482,11 @@ def self.fromRdf(input, options = {}, &block)
482482
# @param [Hash<Symbol => Object>] options
483483
# @option options [Boolean] :validate
484484
# Allow only appropriate content types
485-
# @return [RemoteDocument] retrieved remote document and context information unless block given
486485
# @yield remote_document
487486
# @yieldparam [RemoteDocument] remote_document
487+
# @yieldreturn [Object] returned object
488+
# @return [Object, RemoteDocument]
489+
# If a block is given, the result of evaluating the block is returned, otherwise, the retrieved remote document and context information unless block given
488490
# @raise [JsonLdError]
489491
def self.documentLoader(url, options = {})
490492
options = OPEN_OPTS.merge(options)
@@ -512,11 +514,7 @@ def self.documentLoader(url, options = {})
512514

513515
doc_uri = remote_doc.base_uri rescue url
514516
doc = RemoteDocument.new(doc_uri, remote_doc.read, contextUrl)
515-
if block_given?
516-
yield(doc)
517-
else
518-
doc
519-
end
517+
block_given? ? yield(doc) : doc
520518
end
521519
rescue IOError => e
522520
raise JSON::LD::JsonLdError::LoadingDocumentFailed, e.message

0 commit comments

Comments
 (0)