Module: Assert::Factory::Random

Defined in:
lib/assert/factory.rb

Constant Summary collapse

DICTIONARY =
[*'a'..'z'].freeze

Class Method Summary collapse

Class Method Details

.binaryObject



158
159
160
# File 'lib/assert/factory.rb', line 158

def self.binary
  [ self.integer(10000) ].pack('N*')
end

.date_stringObject



108
109
110
# File 'lib/assert/factory.rb', line 108

def self.date_string
  Time.now.strftime("%Y-%m-%d")
end

.datetime_stringObject



112
113
114
# File 'lib/assert/factory.rb', line 112

def self.datetime_string
  Time.now.strftime("%Y-%m-%d %H:%M:%S")
end

.dir_path_string(length = nil) ⇒ Object



140
141
142
143
# File 'lib/assert/factory.rb', line 140

def self.dir_path_string(length = nil)
  length ||= 12
  File.join(*self.string(length).scan(/.{1,4}/))
end

.email_string(domain = nil, length = nil) ⇒ Object



153
154
155
156
# File 'lib/assert/factory.rb', line 153

def self.email_string(domain = nil, length = nil)
  domain ||= "#{self.string(5)}.com"
  "#{self.string(length)}@#{domain}"
end

.file_name_string(length = nil) ⇒ Object



135
136
137
138
# File 'lib/assert/factory.rb', line 135

def self.file_name_string(length = nil)
  length ||= 6
  "#{self.string(length)}.#{self.string(3)}"
end

.file_path_stringObject



145
146
147
# File 'lib/assert/factory.rb', line 145

def self.file_path_string
  File.join(self.dir_path_string, self.file_name_string)
end

.float(max = nil) ⇒ Object

‘rand` with no args gives a float between 0 and 1



104
105
106
# File 'lib/assert/factory.rb', line 104

def self.float(max = nil)
  (self.integer((max || 100) - 1) + rand).to_f
end

.hex_string(length = nil) ⇒ Object



130
131
132
133
# File 'lib/assert/factory.rb', line 130

def self.hex_string(length = nil)
  length ||= 10
  self.integer(("f" * length).hex - 1).to_s(16).rjust(length, '0')
end

.integer(max = nil) ⇒ Object



99
100
101
# File 'lib/assert/factory.rb', line 99

def self.integer(max = nil)
  rand(max || 32_766) + 1
end

.slug_string(length = nil) ⇒ Object



125
126
127
128
# File 'lib/assert/factory.rb', line 125

def self.slug_string(length = nil)
  length ||= 8
  self.string(length).scan(/.{1,4}/).join('-')
end

.string(length = nil) ⇒ Object



121
122
123
# File 'lib/assert/factory.rb', line 121

def self.string(length = nil)
  [*0..((length || 10) - 1)].map{ |n| DICTIONARY[rand(DICTIONARY.size)] }.join
end

.time_stringObject



116
117
118
# File 'lib/assert/factory.rb', line 116

def self.time_string
  Time.now.strftime("%H:%M:%S")
end

.url_string(host = nil, length = nil) ⇒ Object



149
150
151
# File 'lib/assert/factory.rb', line 149

def self.url_string(host = nil, length = nil)
  File.join(host.to_s, self.dir_path_string(length))
end