aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/fixture_set
diff options
context:
space:
mode:
authorAlexey Muranov <alexey.muranov@gmail.com>2012-10-07 20:32:48 +0200
committerAlexey Muranov <alexey.muranov@gmail.com>2012-10-07 20:43:18 +0200
commitc9ccef554cb5f28d4c4b8d1eb8f5b4de6e25f341 (patch)
treeb2b5233169ab00bed0532952917527f6ecf85ad1 /activerecord/lib/active_record/fixture_set
parentd871807a78ca607f5f78e8eaab99e3d46f284c63 (diff)
downloadrails-c9ccef554cb5f28d4c4b8d1eb8f5b4de6e25f341.tar.gz
rails-c9ccef554cb5f28d4c4b8d1eb8f5b4de6e25f341.tar.bz2
rails-c9ccef554cb5f28d4c4b8d1eb8f5b4de6e25f341.zip
Move/rename files to follow naming conventions
Diffstat (limited to 'activerecord/lib/active_record/fixture_set')
-rw-r--r--activerecord/lib/active_record/fixture_set/file.rb56
1 files changed, 56 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/fixture_set/file.rb b/activerecord/lib/active_record/fixture_set/file.rb
new file mode 100644
index 0000000000..11b53275e1
--- /dev/null
+++ b/activerecord/lib/active_record/fixture_set/file.rb
@@ -0,0 +1,56 @@
+require 'erb'
+require 'yaml'
+
+module ActiveRecord
+ class FixtureSet
+ class File # :nodoc:
+ include Enumerable
+
+ ##
+ # Open a fixture file named +file+. When called with a block, the block
+ # is called with the filehandle and the filehandle is automatically closed
+ # when the block finishes.
+ def self.open(file)
+ x = new file
+ block_given? ? yield(x) : x
+ end
+
+ def initialize(file)
+ @file = file
+ @rows = nil
+ end
+
+ def each(&block)
+ rows.each(&block)
+ end
+
+ RESCUE_ERRORS = [ ArgumentError, Psych::SyntaxError ] # :nodoc:
+
+ private
+ 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'
+ end
+
+ raise Fixture::FormatError unless data.all? { |name, row| Hash === row }
+ data
+ end
+ end
+ end
+end