Module: BinaryFixtures::FixtureHelpers

Included in:
ActiveRecord::FixtureSet::RenderContext
Defined in:
lib/binary_fixtures/fixture_helpers.rb

Instance Method Summary collapse

Instance Method Details

#binary_file(path, options = {}) ⇒ String

The contents of a binary file, in a YAML-friendly format.

Parameters:

  • path (String)

    the path of the binary file to be included, relative to the Rails application’s test/fixtures directory

  • options (Hash) (defaults to: {})

    optionally specify the current indent level

Options Hash (options):

  • indent (Integer)

    the number of spaces that the current line in the YAML fixture file is indented by

Returns:

  • (String)

    base64-encoded binary file contents



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/binary_fixtures/fixture_helpers.rb', line 14

def binary_file(path, options = {})
  # The line with base64 data must be indented further than the current line.
  indent = ' ' * ((options[:indent] || 2) + 2)

  file_path = Rails.root.join('test/fixtures').join(path)
  binary_data = File.binread file_path
  base64_data = Base64.encode64 binary_data
  base64_data.gsub! "\n", "\n#{indent}"
  base64_data.strip!

  "!!binary |\n#{indent}#{base64_data}"
end

#binary_file_size(path) ⇒ Integer

The number of bytes in a binary file.

Parameters:

  • path (String)

    the path of the binary file to be included, relative to the Rails application’s test/fixtures directory

Returns:

  • (Integer)

    the nubmer of bytes in the file



32
33
34
35
# File 'lib/binary_fixtures/fixture_helpers.rb', line 32

def binary_file_size(path)
  file_path = Rails.root.join('test/fixtures').join(path)
  File.stat(file_path).size
end