Class: Faker::File

Inherits:
Base
  • Object
show all
Defined in:
lib/faker/default/file.rb

Constant Summary

Constants inherited from Base

Base::LLetters, Base::Letters, Base::NOT_GIVEN, Base::Numbers, Base::ULetters

Class Method Summary collapse

Methods inherited from Base

bothify, disable_enforce_available_locales, fetch, fetch_all, flexible, letterify, method_missing, numerify, parse, rand, rand_in_range, regexify, resolve, respond_to_missing?, sample, shuffle, translate, unique, with_locale

Class Method Details

.dir(legacy_segment_count = NOT_GIVEN, legacy_root = NOT_GIVEN, legacy_directory_separator = NOT_GIVEN, segment_count: 3, root: nil, directory_separator: ::File::Separator) ⇒ String

Produces a random directory name.

rubocop:disable Metrics/ParameterLists

Examples:

Faker::File.dir #=> "et_error/sint_voluptas/quas_veritatis"
Faker::File.dir(segment_count: 2) #=> "ea-suscipit/ut-deleniti"
Faker::File.dir(segment_count: 3, root: nil, directory_separator: '/') #=> "est_porro/fugit_eveniet/incidunt-autem"
Faker::File.dir(segment_count: 3, root: nil, directory_separator: '\\') #=> "aut-ullam\\quia_quisquam\\ut-eos"

Parameters:

  • segment_count (Integer) (defaults to: 3)

    Specifies the number of nested folders in the generated string.

  • root (String) (defaults to: nil)

    Specifies the root of the generated string.

  • directory_separator (String) (defaults to: ::File::Separator)

    Specifies the separator between the segments.

Returns:

  • (String)

Available since:

  • 1.6.4


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/faker/default/file.rb', line 22

def dir(legacy_segment_count = NOT_GIVEN, legacy_root = NOT_GIVEN, legacy_directory_separator = NOT_GIVEN, segment_count: 3, root: nil, directory_separator: ::File::Separator)
  # rubocop:enable Metrics/ParameterLists
  warn_for_deprecated_arguments do |keywords|
    keywords << :segment_count if legacy_segment_count != NOT_GIVEN
    keywords << :root if legacy_root != NOT_GIVEN
    keywords << :directory_separator if legacy_directory_separator != NOT_GIVEN
  end

  Array
    .new(segment_count) { Faker::Internet.slug }
    .unshift(root)
    .compact
    .join(directory_separator)
    .squeeze(directory_separator)
end

.extensionString

Produces a random file extension.

Examples:

Faker::File.extension #=> "mp3"

Returns:

  • (String)

Available since:

  • 1.6.4


47
48
49
# File 'lib/faker/default/file.rb', line 47

def extension
  fetch('file.extension')
end

.file_name(legacy_dir = NOT_GIVEN, legacy_name = NOT_GIVEN, legacy_ext = NOT_GIVEN, legacy_directory_separator = NOT_GIVEN, dir: nil, name: nil, ext: nil, directory_separator: ::File::Separator) ⇒ String

Produces a random file name.

rubocop:disable Metrics/ParameterLists

Examples:

Faker::File.file_name(dir: 'path/to') #=> "path/to/something_random.jpg"
Faker::File.file_name(dir: 'foo/bar', name: 'baz') #=> "foo/bar/baz.zip"
Faker::File.file_name(dir: 'foo/bar', name: 'baz', ext: 'doc') #=> "foo/bar/baz.doc"
Faker::File.file_name(dir: 'foo/bar', name: 'baz', ext: 'mp3', directory_separator: '\\') #=> "foo/bar\\baz.mp3"

Parameters:

  • dir (String) (defaults to: nil)

    Specifies the path used for the generated file.

  • name (String) (defaults to: nil)

    Specifies the filename used for the generated file.

  • ext (String) (defaults to: nil)

    Specifies the extension used the generated file.

  • directory_separator (String) (defaults to: ::File::Separator)

    Specifies the separator between the directory and name elements.

Returns:

  • (String)

Available since:

  • 1.6.4


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/faker/default/file.rb', line 81

def file_name(legacy_dir = NOT_GIVEN, legacy_name = NOT_GIVEN, legacy_ext = NOT_GIVEN, legacy_directory_separator = NOT_GIVEN, dir: nil, name: nil, ext: nil, directory_separator: ::File::Separator)
  # rubocop:enable Metrics/ParameterLists
  warn_for_deprecated_arguments do |keywords|
    keywords << :dir if legacy_dir != NOT_GIVEN
    keywords << :name if legacy_name != NOT_GIVEN
    keywords << :ext if legacy_ext != NOT_GIVEN
    keywords << :directory_separator if legacy_directory_separator != NOT_GIVEN
  end

  dir ||= dir(segment_count: 1)
  name ||= Faker::Lorem.word.downcase
  ext ||= extension

  [dir, name].join(directory_separator) + ".#{ext}"
end

.mime_typeString

Produces a random mime type.

Examples:

Faker::File.mime_type #=> "application/pdf"

Returns:

  • (String)

Available since:

  • 1.6.4


60
61
62
# File 'lib/faker/default/file.rb', line 60

def mime_type
  fetch('file.mime_type')
end