books-stock-stats 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/book_in_stock.rb +14 -0
- data/lib/books_stock_stats/version.rb +5 -0
- data/lib/books_stock_stats.rb +4 -0
- data/lib/csv_generator.rb +25 -0
- data/lib/csv_reader.rb +103 -0
- metadata +51 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: b9a116a4e92ef459a7abc413c5e980cbea132831dc7e8b975137fe3a2f6b7e89
|
|
4
|
+
data.tar.gz: 92ce0a2347a85c78e88a2b2b7e759f84c0085e4ca4a3e2c08f119ea377d05d37
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: e832a36470b91ff716b253d61ebda38de4c69bd27cca6194d6c203a62e4cd39de6d50d0398000815ba22f32b2505aafcd035e3ab60e00a97c401096c5b659af5
|
|
7
|
+
data.tar.gz: f56a75000828de8795ba14fe4c7de6c1268fdaab6703ffcb8bdbfdf5a331d3c881b2bd022bae6431bba099049f5d80a0b3c1c702e4ef1fe511d66bb6905f4df1
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
class BookInStock
|
|
2
|
+
attr_reader :date, :isbn
|
|
3
|
+
attr_accessor :amount
|
|
4
|
+
|
|
5
|
+
def initialize (date, isbn, amount)
|
|
6
|
+
@date = date
|
|
7
|
+
@isbn = isbn
|
|
8
|
+
@amount = amount.to_f
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def to_s
|
|
12
|
+
"Date: #{@date}, ISBN: #{@isbn}, Amount: #{@amount}"
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
require 'csv'
|
|
2
|
+
|
|
3
|
+
module CsvGenerator
|
|
4
|
+
def generate_row
|
|
5
|
+
date_list = ['01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31']
|
|
6
|
+
date = "#{rand(1800..2022)}-#{date_list[0..11].sample}-#{date_list.sample}"
|
|
7
|
+
isbn = "#{rand(100..999)}-#{rand(10)}-#{rand(1_000_000..9_999_999)}-#{rand(10)}-#{rand(10)}"
|
|
8
|
+
amount = "#{rand(10...100)}.#{rand(10...100)}"
|
|
9
|
+
row = [date, isbn, amount]
|
|
10
|
+
return row
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def csv_generator(n, file_name: 'books_stock.csv')
|
|
14
|
+
headers = %w[Date ISBN Amount]
|
|
15
|
+
enum = Enumerator.new do |y|
|
|
16
|
+
loop do
|
|
17
|
+
y << generate_row
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
CSV.open(file_name, 'w+', write_headers: true, headers: headers) do |row|
|
|
22
|
+
(n * 1_000_000).times { row << enum.next }
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
data/lib/csv_reader.rb
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
require 'csv'
|
|
2
|
+
require_relative 'book_in_stock'
|
|
3
|
+
require_relative 'csv_generator'
|
|
4
|
+
|
|
5
|
+
module CsvReader
|
|
6
|
+
class Reader
|
|
7
|
+
include CsvGenerator
|
|
8
|
+
|
|
9
|
+
attr_accessor :books
|
|
10
|
+
|
|
11
|
+
def initialize
|
|
12
|
+
@books = Array.new
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def read_in_csv_data(csv_file_name)
|
|
16
|
+
CSV.foreach(csv_file_name, headers: true) do |row|
|
|
17
|
+
@books << BookInStock.new(
|
|
18
|
+
row["Date"],
|
|
19
|
+
row["ISBN"],
|
|
20
|
+
row["Amount"]
|
|
21
|
+
)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def to_csv(file_name: 'books_stock.csv')
|
|
26
|
+
enum = @books.to_enum
|
|
27
|
+
headers = %w[Date ISBN Amount]
|
|
28
|
+
CSV.open(file_name, 'w+', write_headers: true, headers: headers) do |row|
|
|
29
|
+
enum.each { |book| row << ["#{book.date}", "#{book.isbn}", "#{book.amount}"] }
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def total_value_in_stock
|
|
34
|
+
enum = @books.to_enum
|
|
35
|
+
sum = 0
|
|
36
|
+
loop do
|
|
37
|
+
sum += enum.next.amount
|
|
38
|
+
end
|
|
39
|
+
return sum.round(2)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def unique_books
|
|
43
|
+
enum = @books.to_enum
|
|
44
|
+
uniques = Hash.new { |key, value| key[value] = 0 }
|
|
45
|
+
loop do
|
|
46
|
+
uniques[enum.next.isbn] += 1
|
|
47
|
+
end
|
|
48
|
+
return uniques
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def book_select (isbn)
|
|
52
|
+
enum = @books.to_enum
|
|
53
|
+
qty = 0
|
|
54
|
+
date = ''
|
|
55
|
+
price = ''
|
|
56
|
+
enum.each do |book|
|
|
57
|
+
if book.isbn == isbn
|
|
58
|
+
qty += 1
|
|
59
|
+
price = book.amount
|
|
60
|
+
date = book.date
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
return "Book: #{isbn}\nQuantity: #{qty}\nRelease date: #{date}\nPrice: #{price}"
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def find_price_error(isbn)
|
|
67
|
+
enum = @books.to_enum
|
|
68
|
+
prices = Array.new
|
|
69
|
+
enum.each do |book|
|
|
70
|
+
if book.isbn == isbn
|
|
71
|
+
prices.append book.amount
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
return "Precios encontrados para el mismo libro: #{prices}"
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def set_price(isbn, price)
|
|
78
|
+
enum = @books.to_enum
|
|
79
|
+
enum.each do |book|
|
|
80
|
+
if book.isbn == isbn
|
|
81
|
+
book.amount = price
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
return "Nuevo precio establecido correctamente!"
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def add_book(date, isbn, amount)
|
|
88
|
+
@books << BookInStock.new("#{date}","#{isbn}",amount)
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def delete_book(isbn)
|
|
92
|
+
enum = @books.to_enum
|
|
93
|
+
n = 0
|
|
94
|
+
enum.each do |book|
|
|
95
|
+
if book.isbn == isbn
|
|
96
|
+
@books.delete_at n
|
|
97
|
+
end
|
|
98
|
+
n =+ 1
|
|
99
|
+
end
|
|
100
|
+
return "El libro se sacó del stock correctamente!"
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: books-stock-stats
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- CaneNicolas
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2022-05-26 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: ''
|
|
14
|
+
email:
|
|
15
|
+
- cane.nicolas@hotmail.com
|
|
16
|
+
executables: []
|
|
17
|
+
extensions: []
|
|
18
|
+
extra_rdoc_files: []
|
|
19
|
+
files:
|
|
20
|
+
- lib/book_in_stock.rb
|
|
21
|
+
- lib/books_stock_stats.rb
|
|
22
|
+
- lib/books_stock_stats/version.rb
|
|
23
|
+
- lib/csv_generator.rb
|
|
24
|
+
- lib/csv_reader.rb
|
|
25
|
+
homepage: https://github.com/CaneNicolas/books-stock-stats
|
|
26
|
+
licenses: []
|
|
27
|
+
metadata:
|
|
28
|
+
allowed_push_host: https://rubygems.org
|
|
29
|
+
homepage_uri: https://github.com/CaneNicolas/books-stock-stats
|
|
30
|
+
source_code_uri: https://github.com/CaneNicolas/books-stock-stats
|
|
31
|
+
changelog_uri: https://github.com/CaneNicolas/books-stock-stats
|
|
32
|
+
post_install_message:
|
|
33
|
+
rdoc_options: []
|
|
34
|
+
require_paths:
|
|
35
|
+
- lib
|
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - ">="
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: 2.6.0
|
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
42
|
+
requirements:
|
|
43
|
+
- - ">="
|
|
44
|
+
- !ruby/object:Gem::Version
|
|
45
|
+
version: '0'
|
|
46
|
+
requirements: []
|
|
47
|
+
rubygems_version: 3.3.7
|
|
48
|
+
signing_key:
|
|
49
|
+
specification_version: 4
|
|
50
|
+
summary: This is a gem to manage a stock of books.
|
|
51
|
+
test_files: []
|