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, generate, letterify, method_missing, numerify, parse, rand, rand_in_range, regexify, resolve, respond_to_missing?, sample, shuffle, shuffle!, translate, unique, with_locale

Class Method Details

.dir(segment_count: 3, root: nil, directory_separator: ::File::Separator) ⇒ String

Produces a random directory name.

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



21
22
23
24
25
26
27
28
# File 'lib/faker/default/file.rb', line 21

def dir(segment_count: 3, root: nil, directory_separator: ::File::Separator)
  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



39
40
41
# File 'lib/faker/default/file.rb', line 39

def extension
  fetch('file.extension')
end

.file_name(dir: nil, name: nil, ext: nil, directory_separator: ::File::Separator) ⇒ String

Produces a random file name.

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



72
73
74
75
76
77
78
# File 'lib/faker/default/file.rb', line 72

def file_name(dir: nil, name: nil, ext: nil, directory_separator: ::File::Separator)
  dir ||= dir(segment_count: 1)
  name ||= Faker::Lorem.word.downcase
  ext ||= extension

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

.mime_type(media_type: nil) ⇒ String

Produces a random mime type.

Examples:

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

Returns:

  • (String)

Available since:

  • next



52
53
54
# File 'lib/faker/default/file.rb', line 52

def mime_type(media_type: nil)
  media_type ? fetch("file.mime_type.#{media_type}") : sample(sample(translate('faker.file.mime_type').values))
end