Class: ActiveRecord::FixtureSet::File
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Instance Chain:
|
|
Inherits: | Object |
Defined in: | activerecord/lib/active_record/fixture_set/file.rb |
Class Method Summary
- .new(file) ⇒ File constructor
-
.open(file)
Open a fixture file named
file
.
Instance Attribute Summary
::Enumerable
- Included
#many? | Returns |
Instance Method Summary
- #each(&block)
- #ignored_fixtures
- #model_class
- #config_row private
- #raw_rows private
- #rows private
-
#validate(data)
private
Validate our unmarshalled data.
- #validate_config_row(data) private
::Enumerable
- Included
#compact_blank | Returns a new |
#exclude? | The negative of the |
#excluding | Returns a copy of the enumerable excluding the specified elements. |
#in_order_of | Returns a new |
#including | Returns a new array that includes the passed elements. |
#index_by | Convert an enumerable to a hash, using the block result as the key and the element as the value. |
#index_with | Convert an enumerable to a hash, using the element as the key and the block result as the value. |
#maximum | Calculates the maximum from the extracted elements. |
#minimum | Calculates the minimum from the extracted elements. |
#pick | Extract the given key from the first element in the enumerable. |
#pluck | Extract the given key from each element in the enumerable. |
#sole | Returns the sole item in the enumerable. |
#without | Alias for Enumerable#excluding. |
#as_json |
::ActiveSupport::EnumerableCoreExt::Constants
- Included
Constructor Details
.new(file) ⇒ File
# File 'activerecord/lib/active_record/fixture_set/file.rb', line 19
def initialize(file) @file = file end
Class Method Details
.open(file)
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.
# File 'activerecord/lib/active_record/fixture_set/file.rb', line 14
def self.open(file) x = new file block_given? ? yield(x) : x end
Instance Method Details
#config_row (private)
[ GitHub ]# File 'activerecord/lib/active_record/fixture_set/file.rb', line 40
def config_row @config_row ||= begin row = raw_rows.find { |fixture_name, _| fixture_name == "_fixture" } if row validate_config_row(row.last) else { 'model_class': nil, 'ignore': nil } end end end
#each(&block)
[ GitHub ]# File 'activerecord/lib/active_record/fixture_set/file.rb', line 23
def each(&block) rows.each(&block) end
#ignored_fixtures
[ GitHub ]# File 'activerecord/lib/active_record/fixture_set/file.rb', line 31
def ignored_fixtures config_row["ignore"] end
#model_class
[ GitHub ]# File 'activerecord/lib/active_record/fixture_set/file.rb', line 27
def model_class config_row["model_class"] end
#raw_rows (private)
[ GitHub ]# File 'activerecord/lib/active_record/fixture_set/file.rb', line 51
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. end end
#rows (private)
[ GitHub ]# File 'activerecord/lib/active_record/fixture_set/file.rb', line 36
def rows @rows ||= raw_rows.reject { |fixture_name, _| fixture_name == "_fixture" } end
#validate(data) (private)
Validate our unmarshalled data.
# File 'activerecord/lib/active_record/fixture_set/file.rb', line 76
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
#validate_config_row(data) (private)
[ GitHub ]# File 'activerecord/lib/active_record/fixture_set/file.rb', line 61
def validate_config_row(data) unless Hash === data raise Fixture::FormatError, "Invalid `_fixture` section: `_fixture` must be a hash: #{@file}" end begin data.assert_valid_keys("model_class", "ignore") rescue ArgumentError => error raise Fixture::FormatError, "Invalid `_fixture` section: #{error.}: #{@file}" end data end