Skip to content

Commit

Permalink
Merge pull request #463 from Shopify/load-file-options
Browse files Browse the repository at this point in the history
Forward keyword arguments in load_file and load_stream
  • Loading branch information
tenderlove authored Sep 10, 2020
2 parents 181a727 + 4e1dd37 commit 011dd50
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
10 changes: 5 additions & 5 deletions lib/psych.rb
Original file line number Diff line number Diff line change
Expand Up @@ -549,18 +549,18 @@ def self.to_json object
# end
# list # => ['foo', 'bar']
#
def self.load_stream yaml, legacy_filename = NOT_GIVEN, filename: nil, fallback: []
def self.load_stream yaml, legacy_filename = NOT_GIVEN, filename: nil, fallback: [], **kwargs
if legacy_filename != NOT_GIVEN
warn_with_uplevel 'Passing filename with the 2nd argument of Psych.load_stream is deprecated. Use keyword argument like Psych.load_stream(yaml, filename: ...) instead.', uplevel: 1 if $VERBOSE
filename = legacy_filename
end

result = if block_given?
parse_stream(yaml, filename: filename) do |node|
yield node.to_ruby
yield node.to_ruby(**kwargs)
end
else
parse_stream(yaml, filename: filename).children.map(&:to_ruby)
parse_stream(yaml, filename: filename).children.map { |node| node.to_ruby(**kwargs) }
end

return fallback if result.is_a?(Array) && result.empty?
Expand All @@ -571,9 +571,9 @@ def self.load_stream yaml, legacy_filename = NOT_GIVEN, filename: nil, fallback:
# Load the document contained in +filename+. Returns the yaml contained in
# +filename+ as a Ruby object, or if the file is empty, it returns
# the specified +fallback+ return value, which defaults to +false+.
def self.load_file filename, fallback: false
def self.load_file filename, **kwargs
File.open(filename, 'r:bom|utf-8') { |f|
self.load f, filename: filename, fallback: fallback
self.load f, filename: filename, **kwargs
}
end

Expand Down
34 changes: 34 additions & 0 deletions test/psych/test_psych.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,19 @@ def test_load_stream
assert_equal %w{ foo bar }, docs
end

def test_load_stream_freeze
docs = Psych.load_stream("--- foo\n...\n--- bar\n...", freeze: true)
assert_equal %w{ foo bar }, docs
docs.each do |string|
assert_predicate string, :frozen?
end
end

def test_load_stream_symbolize_names
docs = Psych.load_stream("---\nfoo: bar", symbolize_names: true)
assert_equal [{foo: 'bar'}], docs
end

def test_load_stream_default_fallback
assert_equal [], Psych.load_stream("")
end
Expand Down Expand Up @@ -242,6 +255,27 @@ def test_load_file
}
end

def test_load_file_freeze
Tempfile.create(['yikes', 'yml']) {|t|
t.binmode
t.write('--- hello world')
t.close

object = Psych.load_file(t.path, freeze: true)
assert_predicate object, :frozen?
}
end

def test_load_file_symbolize_names
Tempfile.create(['yikes', 'yml']) {|t|
t.binmode
t.write("---\nfoo: bar")
t.close

assert_equal({foo: 'bar'}, Psych.load_file(t.path, symbolize_names: true))
}
end

def test_load_file_default_fallback
Tempfile.create(['empty', 'yml']) {|t|
assert_equal false, Psych.load_file(t.path)
Expand Down

0 comments on commit 011dd50

Please sign in to comment.