Sha256: c2c812edcd151ae199b7f37b173c49328a0dfadbb28e101e2005fac4d7eb5f5b

Contents?: true

Size: 1.33 KB

Versions: 2

Compression:

Stored size: 1.33 KB

Contents

require 'base64'

require 'rails'

module BinaryFixtures::FixtureHelpers
  # The contents of a binary file, in a YAML-friendly format.
  #
  # @param [String] path the path of the binary file to be included, relative
  #   to the Rails application's test/fixtures directory
  # @param [Hash] options optionally specify the current indent level
  # @option options [Integer] indent the number of spaces that the current line
  #   in the YAML fixture file is indented by
  # @return [String] base64-encoded binary file contents
  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

  # The number of bytes in a binary file.
  #
  # @param [String] path the path of the binary file to be included, relative
  #   to the Rails application's test/fixtures directory
  # @return [Integer] the nubmer of bytes in the file
  def binary_file_size(path)
    file_path = Rails.root.join('test/fixtures').join(path)
    File.stat(file_path).size
  end
end  # module BinaryFixtures::FixtureHelpers

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
binary_fixtures-0.1.3 lib/binary_fixtures/fixture_helpers.rb
binary_fixtures-0.1.2 lib/binary_fixtures/fixture_helpers.rb