-
Notifications
You must be signed in to change notification settings - Fork 0
Add full library app #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
valter0ff
wants to merge
19
commits into
master
Choose a base branch
from
develop
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
0ec9124
Add models, add validations
cb51f8c
Refactor models, change validations logic
d68895c
add gemfile, add rubocop, fix a lot of rubocop warnings
f8462b5
add library model and seed module
f6b1a08
Change some models validation logic
48039d8
add Statistics module
bde32ce
remove errors attribute from models
d84c6a0
fix errors in Statistics module
7742ad0
fix all rubocop warnings
f321f0b
delete db.yml from git index
7ce3827
remove frozen string magic comment
424ff65
add raise error when object is not uniq
a2cf708
remove ? from predicate method valid?
850a78a
change validation logic, remove uniq validation, other small fixes
6b1ae7e
change valid? method and add method
89808e4
some fix of raise call
e86ce77
fix pass order to statistics module as parameter
0f6e185
change Seed module to be a class
c376c40
move error_class and seed to helpers
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| speedups: | ||
| rescue_vs_respond_to: true | ||
| module_eval: true | ||
| shuffle_first_vs_sample: true | ||
| for_loop_vs_each: true | ||
| each_with_index_vs_while: false | ||
| map_flatten_vs_flat_map: true | ||
| reverse_each_vs_reverse_each: true | ||
| select_first_vs_detect: true | ||
| sort_vs_sort_by: true | ||
| fetch_with_argument_vs_block: true | ||
| keys_each_vs_each_key: true | ||
| hash_merge_bang_vs_hash_brackets: true | ||
| block_vs_symbol_to_proc: true | ||
| proc_call_vs_yield: true | ||
| gsub_vs_tr: true | ||
| select_last_vs_reverse_detect: true | ||
| getter_vs_attr_reader: true | ||
| setter_vs_attr_writer: true | ||
|
|
||
| exclude_paths: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| db.yml | ||
| *.kate-swp |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| AllCops: | ||
| NewCops: enable | ||
|
|
||
| Style/RedundantArgument: # (new in 1.4) | ||
| Enabled: true | ||
| Style/SwapValues: # (new in 1.1) | ||
| Enabled: true | ||
| Style/FrozenStringLiteralComment: | ||
| Enabled: false | ||
| Style/Documentation: | ||
| Enabled: false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| 2.5.3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| source 'https://rubygems.org' | ||
|
|
||
| gem 'awesome_print' | ||
| gem 'faker' | ||
| gem 'fasterer' | ||
| gem 'pry-byebug' | ||
| gem 'rubocop', require: false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| GEM | ||
| remote: https://rubygems.org/ | ||
| specs: | ||
| ast (2.4.1) | ||
| awesome_print (1.8.0) | ||
| byebug (11.1.3) | ||
| coderay (1.1.3) | ||
| colorize (0.8.1) | ||
| concurrent-ruby (1.1.7) | ||
| faker (2.15.1) | ||
| i18n (>= 1.6, < 2) | ||
| fasterer (0.8.3) | ||
| colorize (~> 0.7) | ||
| ruby_parser (>= 3.14.1) | ||
| i18n (1.8.5) | ||
| concurrent-ruby (~> 1.0) | ||
| method_source (1.0.0) | ||
| parallel (1.20.1) | ||
| parser (3.0.0.0) | ||
| ast (~> 2.4.1) | ||
| pry (0.13.1) | ||
| coderay (~> 1.1) | ||
| method_source (~> 1.0) | ||
| pry-byebug (3.9.0) | ||
| byebug (~> 11.0) | ||
| pry (~> 0.13.0) | ||
| rainbow (3.0.0) | ||
| regexp_parser (2.0.2) | ||
| rexml (3.2.4) | ||
| rubocop (1.7.0) | ||
| parallel (~> 1.10) | ||
| parser (>= 2.7.1.5) | ||
| rainbow (>= 2.2.2, < 4.0) | ||
| regexp_parser (>= 1.8, < 3.0) | ||
| rexml | ||
| rubocop-ast (>= 1.2.0, < 2.0) | ||
| ruby-progressbar (~> 1.7) | ||
| unicode-display_width (>= 1.4.0, < 2.0) | ||
| rubocop-ast (1.3.0) | ||
| parser (>= 2.7.1.5) | ||
| ruby-progressbar (1.10.1) | ||
| ruby_parser (3.15.0) | ||
| sexp_processor (~> 4.9) | ||
| sexp_processor (4.15.1) | ||
| unicode-display_width (1.7.0) | ||
|
|
||
| PLATFORMS | ||
| ruby | ||
|
|
||
| DEPENDENCIES | ||
| awesome_print | ||
| faker | ||
| fasterer | ||
| pry-byebug | ||
| rubocop | ||
|
|
||
| BUNDLED WITH | ||
| 2.0.1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| require 'date' | ||
| require 'yaml' | ||
| require 'faker' | ||
| require_relative 'helpers/error_class' | ||
| require_relative 'helpers/seed' | ||
| require_relative 'modules/validation' | ||
| require_relative 'modules/statistics' | ||
| require_relative 'models/author' | ||
| require_relative 'models/reader' | ||
| require_relative 'models/book' | ||
| require_relative 'models/order' | ||
| require_relative 'models/library' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| class PresenceError < StandardError | ||
| end | ||
|
|
||
| class ClassError < StandardError | ||
| end | ||
|
|
||
| class EmptinessError < StandardError | ||
| end | ||
|
|
||
| class PositiveNumberError < StandardError | ||
| end | ||
|
|
||
| class NotAllowedObject < StandardError | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| class Seed | ||
| attr_reader :library | ||
|
|
||
| def initialize(library) | ||
| @library = library | ||
| end | ||
|
|
||
| def generate_data | ||
| seed_authors | ||
| seed_books | ||
| seed_readers | ||
| seed_orders | ||
| end | ||
|
|
||
| def seed_authors | ||
| 10.times do | ||
| library.add(Author.new(name: Faker::Book.unique.author, | ||
| biography: Faker::Lorem.sentence(word_count: 10))) | ||
| end | ||
| end | ||
|
|
||
| def seed_books | ||
| 50.times do | ||
| library.add(Book.new(title: Faker::Book.unique.title, | ||
| author: library.authors.sample)) | ||
| end | ||
| end | ||
|
|
||
| def seed_readers | ||
| 20.times do | ||
| library.add(Reader.new(name: Faker::Name.unique.name, | ||
| email: Faker::Internet.unique.email, | ||
| city: Faker::Address.city, | ||
| street: Faker::Address.street_name, | ||
| house: Faker::Address.building_number.to_i)) | ||
| end | ||
| end | ||
|
|
||
| def seed_orders | ||
| 100.times do | ||
| library.add(Order.new(book: library.books.sample, | ||
| reader: library.readers.sample, | ||
| date: Date.today - rand(0..30))) | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| require_relative 'autoload' | ||
|
|
||
| reader1 = Reader.new(name: 22, email: '@sabaka', street: '', house: -10) | ||
| reader2 = Reader.new(name: 'Mark', email: 'markus@gmail.com', city: 'London', street: 'Bayker', house: 21) | ||
| author1 = Author.new(name: 'Boris Akunin') | ||
| author2 = Author.new(name: '', biography: 'some bio') | ||
| book1 = Book.new(title: 'BEloe solnce', author: author1) | ||
| book2 = Book.new(title: '', author: 'Djek Vorobey') | ||
| order1 = Order.new(book: book2, reader: reader1) | ||
| order2 = Order.new(book: book1, reader: reader2) | ||
|
|
||
| lib = Library.new | ||
| lib.add(reader2) | ||
| lib.add(author1) | ||
| lib.add(book1) | ||
| lib.add(order2) | ||
|
|
||
| begin | ||
| lib.add(author2) | ||
| rescue EmptinessError => e | ||
| puts e.message | ||
| end | ||
|
|
||
| begin | ||
| lib.add(reader1) | ||
| rescue PresenceError => e | ||
| puts e.message | ||
| end | ||
|
|
||
| begin | ||
| lib.add(book2) | ||
| rescue ClassError => e | ||
| puts e.message | ||
| end | ||
|
|
||
| begin | ||
| lib.add(order1) | ||
| rescue ClassError => e | ||
| puts e.message | ||
| end | ||
|
|
||
| Seed.new(lib).generate_data | ||
| lib.store_to_file | ||
|
|
||
| puts lib.top_readers(lib.orders) | ||
| puts lib.top_books(lib.orders) | ||
| puts lib.number_readers_of_top_books(lib.orders) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| class Author | ||
| include Validator | ||
|
|
||
| attr_reader :name, :biography | ||
|
|
||
| def initialize(name: nil, biography: nil) | ||
| @name = name | ||
| @biography = biography | ||
| end | ||
|
|
||
| def validate! | ||
| check_presence | ||
| check_class | ||
| check_emptiness | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def check_presence | ||
| errors = validate_presence :name | ||
| raise PresenceError, errors unless errors.empty? | ||
| end | ||
|
|
||
| def check_class | ||
| errors = validate_class String, :name | ||
| raise ClassError, errors unless errors.empty? | ||
| end | ||
|
|
||
| def check_emptiness | ||
| errors = validate_emptiness :name | ||
| raise EmptinessError, errors unless errors.empty? | ||
| end | ||
|
valter0ff marked this conversation as resolved.
|
||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| class Book | ||
| include Validator | ||
|
|
||
| attr_reader :title, :author | ||
|
|
||
| def initialize(title: nil, author: nil) | ||
| @title = title | ||
| @author = author | ||
| end | ||
|
|
||
| def validate! | ||
| check_presence | ||
| check_class | ||
| check_emptiness | ||
| @author.validate! | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def check_presence | ||
| errors = validate_presence :title, :author | ||
| raise PresenceError, errors unless errors.empty? | ||
| end | ||
|
|
||
| def check_class | ||
| errors = [validate_class(String, :title), | ||
| validate_class(Author, :author)].flatten | ||
| raise ClassError, errors unless errors.empty? | ||
| end | ||
|
|
||
| def check_emptiness | ||
| errors = validate_emptiness :title | ||
| raise EmptinessError, errors unless errors.empty? | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| class Library | ||
| include Statistics | ||
|
|
||
| DATA_FILE = File.expand_path('../db.yml', __dir__) | ||
| CLASSES = [Integer, String, Date, Author, Book, Library, Order, Reader].freeze | ||
| ADDING_ERROR = "Object cann't be added to library".freeze | ||
|
|
||
| attr_reader :authors, :books, :readers, :orders | ||
|
|
||
| def initialize | ||
| @authors = [] | ||
| @books = [] | ||
| @readers = [] | ||
| @orders = [] | ||
|
|
||
| load_from_file! | ||
| end | ||
|
|
||
| def add(entity) | ||
| entity.validate! if entity.respond_to?(:validate!) | ||
| case entity | ||
| when Author then @authors << entity | ||
| when Book then @books << entity | ||
| when Reader then @readers << entity | ||
| when Order then @orders << entity | ||
| else | ||
| raise NotAllowedObject, ADDING_ERROR | ||
| end | ||
| end | ||
|
|
||
| def load_from_file! | ||
| data = YAML.safe_load(File.read(DATA_FILE), CLASSES, [], true) if File.exist?(DATA_FILE) | ||
|
|
||
| return unless data | ||
|
|
||
| @authors = data.authors | ||
| @books = data.books | ||
| @readers = data.readers | ||
| @orders = data.orders | ||
| end | ||
|
|
||
| def store_to_file | ||
| File.open(DATA_FILE, 'w') { |file| file.write(to_yaml) } | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| class Order | ||
| include Validator | ||
|
|
||
| attr_reader :book, :reader, :date | ||
|
|
||
| def initialize(book: nil, reader: nil, date: Date.today) | ||
| @book = book | ||
| @reader = reader | ||
| @date = date | ||
| end | ||
|
|
||
| def validate! | ||
| check_presence | ||
| check_class | ||
| @book.validate! | ||
| @reader.validate! | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def check_presence | ||
| errors = validate_presence :book | ||
| raise PresenceError, errors unless errors.empty? | ||
| end | ||
|
|
||
| def check_class | ||
| errors = [validate_class(Book, :book), | ||
| validate_class(Reader, :reader), | ||
| validate_class(Date, :date)].flatten | ||
| raise ClassError, errors unless errors.empty? | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.