bokov_library 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9646775dadacd823157bfe94d95869bbd1e47a0b
4
+ data.tar.gz: 7357955b45b6e61f851ba972a8f9cca1091231a2
5
+ SHA512:
6
+ metadata.gz: 6cbbbd1570bb576e8179be1e2b4f372359a66d5110c2429c22d7a3f917032ec9b739a685fdae96b87dd8f37d5238d96b6f92b424164491117f21beace6f3bd0a
7
+ data.tar.gz: f56a801ad400bdee9726ad8e7bd9468483f051f0b6c7e5c80a1986a22b66efe3ae4c9739b3845c8491379e4976694d3bf335544354c91175443c3480d0d38f4b
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ .idea/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.16.2
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at lexabokov18@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source "https://rubygems.org"
2
+
3
+ # git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in library.gemspec
6
+ gemspec
7
+
8
+ gem 'rspec'
9
+
data/Gemfile.lock ADDED
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ bokov_library (1.0.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.3)
10
+ rake (10.5.0)
11
+ rspec (3.7.0)
12
+ rspec-core (~> 3.7.0)
13
+ rspec-expectations (~> 3.7.0)
14
+ rspec-mocks (~> 3.7.0)
15
+ rspec-core (3.7.1)
16
+ rspec-support (~> 3.7.0)
17
+ rspec-expectations (3.7.0)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.7.0)
20
+ rspec-mocks (3.7.0)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.7.0)
23
+ rspec-support (3.7.1)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ bokov_library!
30
+ bundler (~> 1.16)
31
+ rake (~> 10.0)
32
+ rspec
33
+
34
+ BUNDLED WITH
35
+ 1.16.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 alexeybokov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # Library
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/library`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'library'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install library
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/library. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the Library project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/library/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "library"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,38 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "./version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "bokov_library"
8
+ spec.version = Library::VERSION
9
+ spec.authors = ["alexeybokov"]
10
+ spec.email = ["lexabokov18@gmail.com"]
11
+
12
+ spec.summary = "bokov_library"
13
+ spec.description = "My very very first gem, please don't look at it :)"
14
+ spec.homepage = "https://github.com/alexeybokov/Library"
15
+ spec.license = "MIT"
16
+
17
+ # # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ # if spec.respond_to?(:metadata)
20
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
21
+ # else
22
+ # raise "RubyGems 2.0 or newer is required to protect against " \
23
+ # "public gem pushes."
24
+ # end
25
+
26
+ # Specify which files should be added to the gem when it is released.
27
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
28
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
29
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
30
+ end
31
+ spec.bindir = "exe"
32
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
33
+ spec.require_paths = ["lib"]
34
+
35
+ spec.add_development_dependency "bundler", "~> 1.16"
36
+ spec.add_development_dependency "rake", "~> 10.0"
37
+ spec.add_development_dependency "rspec", "~> 3.0"
38
+ end
@@ -0,0 +1,71 @@
1
+ ---
2
+ :library:
3
+ :books:
4
+ - :title: The Great Gatsby
5
+ :author: F.Scott Fitzerald
6
+ - :title: The Lord of the Rings
7
+ :author: J. R. R. Tolkien
8
+ - :title: The Master and Margarita
9
+ :author: Mikhail Bulgakov
10
+ - :title: Book 4
11
+ :author: Mikhail Bulgakov
12
+ - :title: Book 5
13
+ :author: Mikhail Bulgakov
14
+ :readers:
15
+ - :name: Alexey
16
+ :email: alexey@gmail.com
17
+ :city: Dnipro
18
+ :street: Gagarina
19
+ :house: '8'
20
+ - :name: Anna
21
+ :email: anna@gmail.com
22
+ :city: Dnipro
23
+ :street: Nova
24
+ :house: '35'
25
+ - :name: Pavel
26
+ :email: pavel@gmail.com
27
+ :city: Odessa
28
+ :street: Great
29
+ :house: '69'
30
+ - :name: Viktoriya
31
+ :email: viktoriya@gmail.com
32
+ :city: Lviv
33
+ :street: Green
34
+ :house: '16'
35
+ - :name: Dmitriy
36
+ :email: dima@gmail.com
37
+ :city: Kyiv
38
+ :street: Nezalezhna
39
+ :house: '112'
40
+ :orders:
41
+ - :book: The Great Gatsby
42
+ :reader: Alexey
43
+ :date: 01/18/05
44
+ - :book: The Lord of the Rings
45
+ :reader: Anna
46
+ :date: 02/19/05
47
+ - :book: The Lord of the Rings
48
+ :reader: Pavel
49
+ :date: 03/20/05
50
+ - :book: The Master and Margarita
51
+ :reader: Viktoriya
52
+ :date: 04/19/05
53
+ - :book: Book 4
54
+ :reader: Alexey
55
+ :date: 05/18/05
56
+ - :book: Book 5
57
+ :reader: Dmitriy
58
+ :date: 06/18/05
59
+ - :book: Book 5
60
+ :reader: Dmitriy
61
+ :date: 07/18/05
62
+ - :book: Book 5
63
+ :reader: Alexey
64
+ :date: '08/18/05'
65
+ :authors:
66
+ - :name: F.Scott Fitzerald
67
+ :bio: text_biography
68
+ - :name: J. R. R. Tolkien
69
+ :bio: text_biography
70
+ - :name: Mikhail Bulgakov
71
+ :bio: text_biography
data/index.rb ADDED
@@ -0,0 +1,113 @@
1
+ require './lib/library'
2
+
3
+ library = Library.new
4
+
5
+ author1 = Author.new('F.Scott Fitzerald', 'text_biography')
6
+ author2 = Author.new('J. R. R. Tolkien', 'text_biography')
7
+ author3 = Author.new('Mikhail Bulgakov', 'text_biography')
8
+
9
+ library.save_resource(author1)
10
+ library.save_resource(author2)
11
+ library.save_resource(author3)
12
+
13
+ book1 = Book.new('The Great Gatsby', author1)
14
+ book2 = Book.new('The Lord of the Rings', author2)
15
+ book3 = Book.new('The Master and Margarita', author3)
16
+ book4 = Book.new('Book 4', author3)
17
+ book5 = Book.new('Book 5', author3)
18
+
19
+ library.save_resource(book1)
20
+ library.save_resource(book2)
21
+ library.save_resource(book3)
22
+ library.save_resource(book4)
23
+ library.save_resource(book5)
24
+
25
+
26
+ reader1 = Reader.new('Alexey', 'alexey@gmail.com')
27
+ reader1.set_address('Dnipro', 'Gagarina', '8')
28
+ reader2 = Reader.new('Anna', 'anna@gmail.com')
29
+ reader2.set_address('Dnipro', 'Nova', '35')
30
+ reader3 = Reader.new('Pavel', 'pavel@gmail.com')
31
+ reader3.set_address('Odessa', 'Great', '69')
32
+ reader4 = Reader.new('Viktoriya', 'viktoriya@gmail.com')
33
+ reader4.set_address('Lviv', 'Green', '16')
34
+ reader5 = Reader.new('Dmitriy', 'dima@gmail.com')
35
+ reader5.set_address('Kyiv', 'Nezalezhna', '112')
36
+
37
+ library.save_resource(reader1)
38
+ library.save_resource(reader2)
39
+ library.save_resource(reader3)
40
+ library.save_resource(reader4)
41
+ library.save_resource(reader5)
42
+
43
+ order1 = Order.new(book1, reader1, '01/18/05')
44
+ order2 = Order.new(book2, reader2, '02/19/05')
45
+ order3 = Order.new(book2, reader3, '03/20/05')
46
+ order4 = Order.new(book3, reader4, '04/19/05')
47
+ order5 = Order.new(book4, reader1, '05/18/05')
48
+ order6 = Order.new(book5, reader5, '06/18/05')
49
+ order7 = Order.new(book5, reader5, '07/18/05')
50
+ order8 = Order.new(book5, reader1, '08/18/05')
51
+
52
+ library.save_resource(order1)
53
+ library.save_resource(order2)
54
+ library.save_resource(order3)
55
+ library.save_resource(order4)
56
+ library.save_resource(order5)
57
+ library.save_resource(order6)
58
+ library.save_resource(order7)
59
+ library.save_resource(order8)
60
+
61
+ puts "++++++++++++++++ROUND 1 ++++++++++++++++++++++++++"
62
+ most_active_reader_finder = FindMostActiveReader.new(library)
63
+ most_active_reader_finder.perform
64
+
65
+ puts "The most active reader is: #{most_active_reader_finder.most_active_reader.name}"
66
+ puts "He has read #{most_active_reader_finder.max_taken_books_num} books in total."
67
+
68
+ puts "-----------------------------------"
69
+
70
+ most_popular_books = FindMostPopularBook.perform(library, places_number: 3)
71
+ puts "The most popular books are:\n\n"
72
+ most_popular_books.each_with_index do |book, index|
73
+ puts "#{index + 1}. #{book[0].title} - was taken #{book[1]} times"
74
+ number_of_readers = FindReadersNumberForBook.perform(library, book[0])
75
+ puts "Was read by #{number_of_readers} people\n\n"
76
+ end
77
+
78
+ puts "-----------------------------------"
79
+
80
+ # puts "Initial library state:"
81
+ # puts "=============================="
82
+ # puts library.inspect
83
+ # library.save_backup
84
+
85
+ # puts "After recovering:"
86
+ # puts "=============================="
87
+ library = Library.restore_from_backup
88
+ # puts library.inspect
89
+
90
+
91
+
92
+ puts "++++++++++++++++ROUND 2 ++++++++++++++++++++++++++"
93
+ most_active_reader_finder = FindMostActiveReader.new(library)
94
+ most_active_reader_finder.perform
95
+
96
+ puts "The most active reader is: #{most_active_reader_finder.most_active_reader.name}"
97
+ puts "He has read #{most_active_reader_finder.max_taken_books_num} books in total."
98
+
99
+ puts "-----------------------------------"
100
+
101
+ most_popular_books = FindMostPopularBook.perform(library, places_number: 3)
102
+ puts "The most popular books are:\n\n"
103
+ most_popular_books.each_with_index do |book, index|
104
+ puts "#{index + 1}. #{book[0].title} - was taken #{book[1]} times"
105
+ number_of_readers = FindReadersNumberForBook.perform(library, book[0])
106
+ puts "Was read by #{number_of_readers} people\n\n"
107
+ end
108
+
109
+ puts "-----------------------------------"
110
+
111
+ # library.save_books
112
+ # library.read_books
113
+
data/lib/author.rb ADDED
@@ -0,0 +1,17 @@
1
+ class Author
2
+ attr_accessor :name, :bio
3
+
4
+ def initialize(name, bio)
5
+ @name = name
6
+ @bio = bio
7
+ end
8
+
9
+ def to_h
10
+ {
11
+ name: name,
12
+ bio: bio
13
+ }
14
+ end
15
+ end
16
+
17
+
@@ -0,0 +1 @@
1
+ require 'library'
data/lib/book.rb ADDED
@@ -0,0 +1,15 @@
1
+ class Book < Library
2
+ attr_accessor :title, :author
3
+
4
+ def initialize(title, author)
5
+ @title = title
6
+ @author = author
7
+ end
8
+
9
+ def to_h
10
+ {
11
+ title: title,
12
+ author: author.name
13
+ }
14
+ end
15
+ end
@@ -0,0 +1,25 @@
1
+ require 'yaml'
2
+ require 'fileutils'
3
+
4
+ module FileWorker
5
+ extend self
6
+
7
+ BACKUP_FILE_NAME = 'library_backup.yaml'.freeze
8
+
9
+ def write_backup(raw_yaml)
10
+ create_data_dir_if_not_exist
11
+ File.open("data/#{BACKUP_FILE_NAME}", 'w+') do |f|
12
+ f.write(raw_yaml)
13
+ end
14
+ end
15
+
16
+ def read_backup
17
+ YAML.load_file("data/#{BACKUP_FILE_NAME}")
18
+ end
19
+
20
+ private
21
+
22
+ def create_data_dir_if_not_exist
23
+ FileUtils.mkdir_p('data')
24
+ end
25
+ end
@@ -0,0 +1,30 @@
1
+ class FindMostActiveReader
2
+
3
+ attr_reader :max_taken_books_num, :most_active_reader
4
+
5
+ def initialize(library)
6
+ @library = library
7
+ @max_taken_books_num = 0
8
+ @most_active_reader = nil
9
+ end
10
+
11
+ def perform
12
+ find_the_most_active_reader
13
+ self
14
+ end
15
+
16
+ private
17
+
18
+ def find_the_most_active_reader
19
+ frequency_hash = Hash.new { |hash, key| hash[key] = 0 }
20
+ library.orders.each do |order|
21
+ frequency_hash[order.reader] += 1
22
+ if frequency_hash[order.reader] > @max_taken_books_num
23
+ @most_active_reader = order.reader
24
+ @max_taken_books_num = frequency_hash[order.reader]
25
+ end
26
+ end
27
+ end
28
+
29
+ attr_reader :library
30
+ end
@@ -0,0 +1,36 @@
1
+ class FindMostPopularBook
2
+ def self.perform(library, places_number: 1)
3
+ new(library, places_number: places_number).perform
4
+ end
5
+
6
+ def initialize(library, places_number: 1)
7
+ @library = library
8
+ @places_number = places_number
9
+ end
10
+
11
+ def perform
12
+ sorted_frequencies = sort_frequency_hash(build_frequency_hash)
13
+ find_n_most_popular(sorted_frequencies)
14
+ end
15
+
16
+ private
17
+
18
+ def build_frequency_hash
19
+ frequency_hash = Hash.new { |hash, key| hash[key] = 0 }
20
+ library.orders.each { |order| frequency_hash[order.book] += 1 }
21
+ frequency_hash
22
+ end
23
+
24
+ def sort_frequency_hash(frequency_hash)
25
+ frequency_hash.sort_by { |_key, value| value }.reverse
26
+ end
27
+
28
+ def find_n_most_popular(sorted_frequencies)
29
+ (0...places_number).map do |position|
30
+ sorted_frequencies[position]
31
+ end
32
+ end
33
+
34
+ attr_reader :library, :places_number
35
+ end
36
+
@@ -0,0 +1,30 @@
1
+ class FindReadersNumberForBook
2
+ def self.perform(library, book)
3
+ new(library, book).perform
4
+ end
5
+
6
+ def initialize(library, book)
7
+ @library = library
8
+ @book = book
9
+ end
10
+
11
+ def perform
12
+ find_the_number_of_readers
13
+ end
14
+
15
+ private
16
+
17
+ def find_the_number_of_readers
18
+ book_readers = []
19
+ library.orders.each do |order|
20
+ book_readers.push(order.reader) if new_reader?(book_readers, order)
21
+ end
22
+ book_readers.size
23
+ end
24
+
25
+ def new_reader?(found_readers, order)
26
+ order.book == book && !found_readers.include?(order.reader)
27
+ end
28
+
29
+ attr_reader :library, :book
30
+ end
data/lib/library.rb ADDED
@@ -0,0 +1,55 @@
1
+ require 'reader'
2
+ require 'order'
3
+ require 'author'
4
+ require 'file_worker'
5
+ require 'library_backup_manager'
6
+ require 'library'
7
+ require 'book'
8
+ require 'find_most_active_reader'
9
+ require 'find_most_popular_book'
10
+ require 'find_readers_number_for_book'
11
+
12
+
13
+ class Library
14
+ include LibraryBackupManager
15
+
16
+ attr_accessor :books, :orders, :readers, :authors
17
+
18
+ def initialize
19
+ @books = []
20
+ @orders = []
21
+ @readers = []
22
+ @authors = []
23
+ end
24
+
25
+ def find_author(author_name)
26
+ found_author = authors.find { |author| author.name == author_name }
27
+ fail "Author #{author_name} wasn't found!" unless found_author
28
+ found_author
29
+ end
30
+
31
+ def find_book(book_title)
32
+ found_book = books.find { |book| book.title == book_title }
33
+ fail "Book #{book_title} wasn't found!" unless found_book
34
+ found_book
35
+ end
36
+
37
+ def find_reader(reader_name)
38
+ found_reader = readers.find { |reader| reader.name == reader_name }
39
+ fail "reader #{reader_name} wasn't found!" unless found_reader
40
+ found_reader
41
+ end
42
+
43
+ def save_resource(resourse)
44
+ case resourse.class.name
45
+ when 'Book'
46
+ books.push(resourse) unless books.include?(resourse)
47
+ when 'Order'
48
+ orders.push(resourse)
49
+ when 'Reader'
50
+ readers.push(resourse)
51
+ when 'Author'
52
+ authors.push(resourse)
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,83 @@
1
+ module LibraryBackupManager
2
+
3
+ def save_backup
4
+ FileWorker.write_backup(as_yaml)
5
+ end
6
+
7
+ def as_yaml
8
+ {
9
+ library: {
10
+ books: books.map(&:to_h), # .map { |book| book.to_h }
11
+ readers: readers.map(&:to_h),
12
+ orders: orders.map(&:to_h),
13
+ authors: authors.map(&:to_h)
14
+ }
15
+ }.to_yaml
16
+ end
17
+
18
+ def self.included(base)
19
+ base.class_eval do
20
+
21
+ def self.restore_from_backup
22
+ parsed = read_backup_with_validation
23
+ library = Library.new
24
+ restore_authors(library, parsed[:authors])
25
+ restore_books(library, parsed[:books])
26
+ restore_readers(library, parsed[:readers])
27
+ restore_orders(library, parsed[:orders])
28
+ library
29
+ end
30
+
31
+ def self.read_backup_with_validation
32
+ parsed = FileWorker.read_backup
33
+ fail 'Invalid Backup!' if !parsed.is_a?(Hash) || parsed[:library].nil?
34
+ parsed[:library]
35
+ end
36
+
37
+ def self.restore_authors(library, parsed_authors)
38
+ parsed_authors.each do |author_hash|
39
+ library.save_resource(Author.new(author_hash[:name], author_hash[:bio]))
40
+ end
41
+ end
42
+
43
+ def self.restore_books(library, parsed_books)
44
+ parsed_books.each do |book_hash|
45
+ author = library.find_author(book_hash[:author])
46
+ library.save_resource(Book.new(book_hash[:title], author))
47
+ end
48
+ end
49
+
50
+ def self.restore_readers(library, parsed_readers)
51
+ parsed_readers.each do |reader_hash|
52
+ reader = Reader.new(reader_hash[:name], reader_hash[:email])
53
+ reader.set_address(reader_hash[:city], reader_hash[:street], reader_hash[:house])
54
+ library.save_resource(reader)
55
+ end
56
+ end
57
+
58
+ def self.restore_orders(library, parsed_orders)
59
+ parsed_orders.each do |order_hash|
60
+ book = library.find_book(order_hash[:book])
61
+ reader = library.find_reader(order_hash[:reader])
62
+ order = Order.new(book, reader, order_hash[:date])
63
+ library.save_resource(order)
64
+ end
65
+ end
66
+ end
67
+ end
68
+
69
+ # def save_books
70
+ # File.open("books.txt", "w") do |f|
71
+ # @books.each { |b| f.puts "#{b.title}:#{b.author}" }
72
+ # end
73
+ # end
74
+ #
75
+ #
76
+ # def read_books
77
+ # return unless File.exists?("books.txt")
78
+ # book_fields = File.readlines("books.txt")
79
+ # book_fields.map! { |b| b.chomp }
80
+ # book_fields.map! { |b| b.split(":") }
81
+ # book_fields.each { |b| @books << Book.new(b[0], b[1]) unless find_book(b[0]) }
82
+ # end
83
+ end
data/lib/order.rb ADDED
@@ -0,0 +1,17 @@
1
+ class Order
2
+ attr_accessor :book, :reader, :date
3
+
4
+ def initialize(book, reader, date)
5
+ @book = book
6
+ @reader = reader
7
+ @date = date
8
+ end
9
+
10
+ def to_h
11
+ {
12
+ book: book.title,
13
+ reader: reader.name,
14
+ date: date
15
+ }
16
+ end
17
+ end
data/lib/reader.rb ADDED
@@ -0,0 +1,24 @@
1
+ class Reader
2
+ attr_accessor :name, :email, :city, :street, :house
3
+
4
+ def initialize(name, email)
5
+ @name = name
6
+ @email = email
7
+ end
8
+
9
+ def set_address(city, street, house)
10
+ @city = city
11
+ @street = street
12
+ @house = house
13
+ end
14
+
15
+ def to_h
16
+ {
17
+ name: name,
18
+ email: email,
19
+ city: city,
20
+ street: street,
21
+ house: house
22
+ }
23
+ end
24
+ end
data/version.rb ADDED
@@ -0,0 +1,3 @@
1
+ module Library
2
+ VERSION = '1.0.0'
3
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bokov_library
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - alexeybokov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-06-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: My very very first gem, please don't look at it :)
56
+ email:
57
+ - lexabokov18@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".idea/library.iml"
64
+ - ".idea/misc.xml"
65
+ - ".idea/modules.xml"
66
+ - ".idea/vcs.xml"
67
+ - ".idea/workspace.xml"
68
+ - ".rspec"
69
+ - ".travis.yml"
70
+ - CODE_OF_CONDUCT.md
71
+ - Gemfile
72
+ - Gemfile.lock
73
+ - LICENSE.txt
74
+ - README.md
75
+ - Rakefile
76
+ - bin/console
77
+ - bin/setup
78
+ - bokov_library.gemspec
79
+ - data/library_backup.yaml
80
+ - index.rb
81
+ - lib/author.rb
82
+ - lib/bokov_library.rb
83
+ - lib/book.rb
84
+ - lib/file_worker.rb
85
+ - lib/find_most_active_reader.rb
86
+ - lib/find_most_popular_book.rb
87
+ - lib/find_readers_number_for_book.rb
88
+ - lib/library.rb
89
+ - lib/library_backup_manager.rb
90
+ - lib/order.rb
91
+ - lib/reader.rb
92
+ - version.rb
93
+ homepage: https://github.com/alexeybokov/Library
94
+ licenses:
95
+ - MIT
96
+ metadata: {}
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 2.6.12
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: bokov_library
117
+ test_files: []