Module: FileTree::ContentGenerator

Defined in:
lib/file_tree/content_generator.rb

Constant Summary collapse

LETTERS =
["a".."z", "A".."Z", "0".."9", " ", "\n", "_", "*"].map{|i| Array(i)}.flatten
CONTENT_TYPES =
[:binary, :text, :empty]

Class Method Summary collapse

Class Method Details

.create(type) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/file_tree/content_generator.rb', line 6

def self.create(type)
  type = CONTENT_TYPES.sample if type == :random

  case type
    when :binary
      create_binary_content
    when :text
      create_text_content
    when :empty
      ""
    else
      raise(ArgumentError, "Unsupported content type: #{type}")
  end
end

.create_binary_contentObject



21
22
23
# File 'lib/file_tree/content_generator.rb', line 21

def self.create_binary_content
  Random.new.bytes(random_file_size)
end

.create_text_contentObject



25
26
27
# File 'lib/file_tree/content_generator.rb', line 25

def self.create_text_content
  (1..random_file_size).map{LETTERS.sample}.join
end

.random_file_sizeObject



29
30
31
# File 'lib/file_tree/content_generator.rb', line 29

def self.random_file_size
  rand(Defaults.min_file_size..Defaults.max_file_size)
end