Class: Generador

Inherits:
Object
  • Object
show all
Defined in:
lib/csv_generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nombre, cantidad) ⇒ Generador

Returns a new instance of Generador.



8
9
10
11
# File 'lib/csv_generator.rb', line 8

def initialize(nombre, cantidad)
  @nombre_archivo = nombre
  @cantidad = cantidad
end

Instance Attribute Details

#cantidadObject

Returns the value of attribute cantidad.



6
7
8
# File 'lib/csv_generator.rb', line 6

def cantidad
  @cantidad
end

#nombre_archivoObject

Returns the value of attribute nombre_archivo.



6
7
8
# File 'lib/csv_generator.rb', line 6

def nombre_archivo
  @nombre_archivo
end

Instance Method Details

#date_genObject



17
18
19
20
21
# File 'lib/csv_generator.rb', line 17

def date_gen
  from = 0.0
  to = Time.now
  Time.at(from + rand * (to.to_f - from.to_f)).strftime('%d/%m/%Y')
end

#file_genObject



27
28
29
30
31
32
33
# File 'lib/csv_generator.rb', line 27

def file_gen
  CSV.open(@nombre_archivo, 'w+', headers:['Date', 'ISBN', 'Amount'], write_headers: true) do |archivo|
    (@cantidad * 1_000_000).times do
      archivo << [date_gen.to_s, isbn_gen.to_s, price_gen]
    end
  end
end

#isbn_genObject



13
14
15
# File 'lib/csv_generator.rb', line 13

def isbn_gen
  "#{rand.to_s[2..4]}-#{rand.to_s[2..2]}-#{rand.to_s[2..8]}-#{rand.to_s[2..2]}-#{rand.to_s[2..2]}"
end

#price_genObject



23
24
25
# File 'lib/csv_generator.rb', line 23

def price_gen
  rand(0.0..99.99).truncate(2)
end