summaryrefslogtreecommitdiff
path: root/lib/psych.rb
diff options
context:
space:
mode:
authortuexss <andreas.beer@gmail.com>2016-01-06 22:32:08 +0100
committertuexss <andreas.beer@gmail.com>2016-01-08 16:06:45 +0100
commitcbef14d4f18a56cbc9e1c7d9bcdbcf5dc6510a68 (patch)
tree63300a3fc3fff1329f0b64ea6fb2ed2e9b93ee10 /lib/psych.rb
parent16552bad2b1bc7bbb54cc1206b10329919f46f39 (diff)
downloadpsych-cbef14d4f18a56cbc9e1c7d9bcdbcf5dc6510a68.zip
Add optional fallback return value parameter
Diffstat (limited to 'lib/psych.rb')
-rw-r--r--lib/psych.rb19
1 files changed, 12 insertions, 7 deletions
diff --git a/lib/psych.rb b/lib/psych.rb
index 1006998..ece5192 100644
--- a/lib/psych.rb
+++ b/lib/psych.rb
@@ -228,6 +228,8 @@ module Psych
# The version of libyaml Psych is using
LIBYAML_VERSION = Psych.libyaml_version.join '.'
+ FALLBACK = Struct.new :to_ruby # :nodoc:
+
###
# Load +yaml+ in to a Ruby data structure. If multiple documents are
# provided, the object contained in the first document will be returned.
@@ -247,8 +249,8 @@ module Psych
# ex.file # => 'file.txt'
# ex.message # => "(file.txt): found character that cannot start any token"
# end
- def self.load yaml, filename = nil
- result = parse(yaml, filename)
+ def self.load yaml, filename = nil, fallback = false
+ result = parse(yaml, filename, fallback)
result ? result.to_ruby : result
end
@@ -320,11 +322,11 @@ module Psych
# end
#
# See Psych::Nodes for more information about YAML AST.
- def self.parse yaml, filename = nil
+ def self.parse yaml, filename = nil, fallback = false
parse_stream(yaml, filename) do |node|
return node
end
- false
+ fallback
end
###
@@ -465,9 +467,12 @@ module Psych
###
# Load the document contained in +filename+. Returns the yaml contained in
- # +filename+ as a Ruby object
- def self.load_file filename
- File.open(filename, 'r:bom|utf-8') { |f| self.load f, filename }
+ # +filename+ as a Ruby object, or if the file is empty, it returns
+ # the specified default return value, which defaults to an empty Hash
+ def self.load_file filename, fallback = false
+ File.open(filename, 'r:bom|utf-8') { |f|
+ self.load f, filename, FALLBACK.new(fallback)
+ }
end
# :stopdoc: