Sha256: 0f68026be0242afd480df2ed03062b7ca3917207595ca69f172ee795f9a074bb

Contents?: true

Size: 1.93 KB

Versions: 37

Compression:

Stored size: 1.93 KB

Contents

# frozen_string_literal: true

require "active_support/configuration_file"

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
      end

      def each(&block)
        rows.each(&block)
      end

      def model_class
        config_row["model_class"]
      end

      def ignored_fixtures
        config_row["ignore"]
      end

      private
        def rows
          @rows ||= raw_rows.reject { |fixture_name, _| fixture_name == "_fixture" }
        end

        def config_row
          @config_row ||= begin
            row = raw_rows.find { |fixture_name, _| fixture_name == "_fixture" }
            if row
              row.last
            else
              { 'model_class': nil, 'ignore': nil }
            end
          end
        end

        def raw_rows
          @raw_rows ||= begin
            data = ActiveSupport::ConfigurationFile.parse(@file, context:
              ActiveRecord::FixtureSet::RenderContext.create_subclass.new.get_binding)
            data ? validate(data).to_a : []
          rescue RuntimeError => error
            raise Fixture::FormatError, error.message
          end
        end

        # Validate our unmarshalled data.
        def validate(data)
          unless Hash === data || YAML::Omap === data
            raise Fixture::FormatError, "fixture is not a hash: #{@file}"
          end

          invalid = data.reject { |_, row| Hash === row }
          if invalid.any?
            raise Fixture::FormatError, "fixture key is not a hash: #{@file}, keys: #{invalid.keys.inspect}"
          end
          data
        end
    end
  end
end

Version data entries

37 entries across 37 versions & 4 rubygems

Version Path
activerecord-6.1.7.10 lib/active_record/fixture_set/file.rb
activerecord-6.1.7.9 lib/active_record/fixture_set/file.rb
activerecord-6.1.7.8 lib/active_record/fixture_set/file.rb
activerecord-6.1.7.7 lib/active_record/fixture_set/file.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activerecord-6.1.6.1/lib/active_record/fixture_set/file.rb
activerecord-6.1.7.6 lib/active_record/fixture_set/file.rb
activerecord-6.1.7.5 lib/active_record/fixture_set/file.rb
activerecord-6.1.7.4 lib/active_record/fixture_set/file.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/activerecord-6.1.6.1/lib/active_record/fixture_set/file.rb
activerecord-6.1.7.3 lib/active_record/fixture_set/file.rb
activerecord-6.1.7.2 lib/active_record/fixture_set/file.rb
activerecord-6.1.7.1 lib/active_record/fixture_set/file.rb
activerecord-6.1.7 lib/active_record/fixture_set/file.rb
activerecord-6.1.6.1 lib/active_record/fixture_set/file.rb
activerecord-6.1.6 lib/active_record/fixture_set/file.rb
activerecord-6.1.5.1 lib/active_record/fixture_set/file.rb
activerecord-6.1.5 lib/active_record/fixture_set/file.rb
activerecord-6.1.4.7 lib/active_record/fixture_set/file.rb
activerecord-6.1.4.6 lib/active_record/fixture_set/file.rb
activerecord-6.1.4.5 lib/active_record/fixture_set/file.rb