Skip to content

Commit

Permalink
Return yields as array (#1)
Browse files Browse the repository at this point in the history
* Return yield results as array when in block mode

* Bump to 0.1.1

* Rubocop

* What was wrong with that syntax?
  • Loading branch information
kke authored Jan 8, 2019
1 parent 6afd869 commit ea54fda
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
14 changes: 7 additions & 7 deletions lib/yaml/safe_load_stream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ module YAMLSafeLoadStream
# @yield [document] each document in the stream is yielded if a block is given
# @return [Array] when a block is not given, returns an Array of documents
module_function def safe_load_stream(yaml, filename = nil)
streams = []
result = []
::YAML.parse_stream(yaml, filename) do |stream|
raise_if_tags(stream, filename)
if block_given?
yield stream.to_ruby
else
streams << stream.to_ruby
end
result << if block_given?
yield(stream.to_ruby)
else
stream.to_ruby
end
end
streams unless block_given?
result
end

module_function def raise_if_tags(obj, filename = nil, doc_num = 1)
Expand Down
2 changes: 1 addition & 1 deletion lib/yaml/safe_load_stream/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module YAMLSafeLoadStream
VERSION = "0.1.0"
VERSION = "0.1.1"
end
10 changes: 10 additions & 0 deletions spec/yaml/safe_load_stream_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@
expect(docs.first['hello']).to eq 'world'
expect(docs.last['hello']).to eq 'universe'
end

it 'returns the result of yields as array' do
docs = YAMLSafeLoadStream.safe_load_stream(multidoc_stream) do |doc|
expect(doc).to be_a Hash
doc
end

expect(docs.first['hello']).to eq 'world'
expect(docs.last['hello']).to eq 'universe'
end
end

context 'used without a block' do
Expand Down

3 comments on commit ea54fda

@birajpaul600
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@birajpaul600
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@birajpaul600
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.