Psych is always available on 1.9

This commit is contained in:
Jeremy Kemper 2012-05-31 09:46:52 -07:00
parent da048de773
commit 5f690b97ba
2 changed files with 25 additions and 31 deletions

@ -1,12 +1,10 @@
require 'yaml'
module ActiveRecord
# :stopdoc:
module Coders
class YAMLColumn
RESCUE_ERRORS = [ ArgumentError ]
if defined?(Psych) && defined?(Psych::SyntaxError)
RESCUE_ERRORS << Psych::SyntaxError
end
RESCUE_ERRORS = [ ArgumentError, Psych::SyntaxError ]
attr_accessor :object_class

@ -24,37 +24,33 @@ def each(&block)
rows.each(&block)
end
RESCUE_ERRORS = [ ArgumentError ] # :nodoc:
RESCUE_ERRORS = [ ArgumentError, Psych::SyntaxError ] # :nodoc:
private
if defined?(Psych) && defined?(Psych::SyntaxError)
RESCUE_ERRORS << Psych::SyntaxError
end
def rows
return @rows if @rows
def rows
return @rows if @rows
begin
data = YAML.load(render(IO.read(@file)))
rescue *RESCUE_ERRORS => error
raise Fixture::FormatError, "a YAML error occurred parsing #{@file}. Please note that YAML must be consistently indented using spaces. Tabs are not allowed. Please have a look at http://www.yaml.org/faq.html\nThe exact error was:\n #{error.class}: #{error}", error.backtrace
end
@rows = data ? validate(data).to_a : []
end
def render(content)
ERB.new(content).result
end
# Validate our unmarshalled data.
def validate(data)
unless Hash === data || YAML::Omap === data
raise Fixture::FormatError, 'fixture is not a hash'
begin
data = YAML.load(render(IO.read(@file)))
rescue *RESCUE_ERRORS => error
raise Fixture::FormatError, "a YAML error occurred parsing #{@file}. Please note that YAML must be consistently indented using spaces. Tabs are not allowed. Please have a look at http://www.yaml.org/faq.html\nThe exact error was:\n #{error.class}: #{error}", error.backtrace
end
@rows = data ? validate(data).to_a : []
end
raise Fixture::FormatError unless data.all? { |name, row| Hash === row }
data
end
def render(content)
ERB.new(content).result
end
# Validate our unmarshalled data.
def validate(data)
unless Hash === data || YAML::Omap === data
raise Fixture::FormatError, 'fixture is not a hash'
end
raise Fixture::FormatError unless data.all? { |name, row| Hash === row }
data
end
end
end
end