Module: MuchFactory::Random

Defined in:
lib/much-factory.rb

Constant Summary collapse

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

Class Method Summary collapse

Class Method Details

.binaryObject



191
192
193
# File 'lib/much-factory.rb', line 191

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

.date_stringObject



141
142
143
# File 'lib/much-factory.rb', line 141

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

.datetime_stringObject



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

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

.dir_path_string(length = nil) ⇒ Object



173
174
175
176
# File 'lib/much-factory.rb', line 173

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

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



186
187
188
189
# File 'lib/much-factory.rb', line 186

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

.file_name_string(length = nil) ⇒ Object



168
169
170
171
# File 'lib/much-factory.rb', line 168

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

.file_path_stringObject



178
179
180
# File 'lib/much-factory.rb', line 178

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

.float(max = nil) ⇒ Object

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



137
138
139
# File 'lib/much-factory.rb', line 137

def self.float(max = nil)
  (max || 100).to_f * rand
end

.hex_string(length = nil) ⇒ Object



163
164
165
166
# File 'lib/much-factory.rb', line 163

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

.integer(max = nil) ⇒ Object

rand given a max int value returns integers between 0 and max-1



132
133
134
# File 'lib/much-factory.rb', line 132

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

.string(length = nil) ⇒ Object



155
156
157
158
159
160
161
# File 'lib/much-factory.rb', line 155

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

.time_stringObject



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

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

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



182
183
184
# File 'lib/much-factory.rb', line 182

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