├── .github └── workflows │ └── linters.yml ├── .gitignore ├── Gemfile ├── Gemfile.lock ├── README.md ├── app.rb ├── book.rb ├── books.json ├── checks.rb ├── classroom.rb ├── create_book.rb ├── create_person.rb ├── create_rental.rb ├── create_student.rb ├── create_teacher.rb ├── decorator.rb ├── main.rb ├── nameable.rb ├── people.json ├── person.rb ├── rental.rb ├── rentals.json ├── screenshot.gif ├── spec ├── book_spec.rb ├── classroom_spec.rb ├── decorator_spec.rb ├── person_spec.rb ├── rental_spec.rb ├── student_spec.rb └── teacher_spec.rb ├── student.rb └── teacher.rb /.github/workflows/linters.yml: -------------------------------------------------------------------------------- 1 | name: Linters 2 | 3 | on: pull_request 4 | 5 | jobs: 6 | rubocop: 7 | name: Rubocop 8 | runs-on: ubuntu-18.04 9 | 10 | steps: 11 | - uses: actions/checkout@v2 12 | - uses: actions/setup-ruby@v1 13 | with: 14 | ruby-version: ">=3.0.x" 15 | - name: Setup Rubocop 16 | run: | 17 | gem install --no-document rubocop -v '>= 1.0, < 2.0' # https://docs.rubocop.org/en/stable/installation/ 18 | [ -f .rubocop.yml ] || wget https://raw.githubusercontent.com/microverseinc/linters-config/master/ruby/.rubocop.yml 19 | - name: Rubocop Report 20 | run: rubocop --color 21 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .rubocop.yml -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | gem 'rubocop', '>= 1.0', '< 2.0' 4 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | GEM 2 | remote: https://rubygems.org/ 3 | specs: 4 | ast (2.4.2) 5 | parallel (1.22.1) 6 | parser (3.1.1.0) 7 | ast (~> 2.4.1) 8 | rainbow (3.1.1) 9 | regexp_parser (2.2.1) 10 | rexml (3.2.5) 11 | rubocop (1.26.1) 12 | parallel (~> 1.10) 13 | parser (>= 3.1.0.0) 14 | rainbow (>= 2.2.2, < 4.0) 15 | regexp_parser (>= 1.8, < 3.0) 16 | rexml 17 | rubocop-ast (>= 1.16.0, < 2.0) 18 | ruby-progressbar (~> 1.7) 19 | unicode-display_width (>= 1.4.0, < 3.0) 20 | rubocop-ast (1.16.0) 21 | parser (>= 3.1.1.0) 22 | ruby-progressbar (1.11.0) 23 | unicode-display_width (2.1.0) 24 | 25 | PLATFORMS 26 | x86_64-linux 27 | 28 | DEPENDENCIES 29 | rubocop (>= 1.0, < 2.0) 30 | 31 | BUNDLED WITH 32 | 2.2.32 33 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # OOP-school-library 2 | 3 | > This project is a tool to record what books are in a library and who borrows them. The app allows users you to: 4 | > 5 | > - Add new students or teachers. 6 | > - Add new books. 7 | > - Save records of who borrowed a given book and when. 8 | > And all of this is built in a beautiful and well-organized way! 9 | 10 | ![console app](./screenshot.gif) 11 | 12 | ## Built With 13 | 14 | - Major languages: Ruby 15 | 16 | ## Getting Started 17 | 18 | To get a local copy up and running follow these simple steps 19 | 20 | ### Prerequisites 21 | 22 | - `ruby` required for running the code. 23 | - `gem` required for linters. 24 | 25 | ### Setup 26 | 27 | Run the following command in your terminal to clone this repo to your local machine 28 | 29 | ```bash 30 | $ git@github.com:Sahar-AbdelSamad/OOP-school-library.git 31 | ``` 32 | 33 | Change directory into the newly created project 34 | 35 | ```bash 36 | $ cd OOP-school-library 37 | ``` 38 | 39 | ### Usage 40 | 41 | #### In the project directory you can run the following command to execute the code: 42 | 43 | ```bash 44 | $ ruby main.rb 45 | ``` 46 | 47 | #### Or alternatively 48 | 49 | #### you can add execute permissions to `main.rb` by running this command 50 | 51 | ```bash 52 | $ chmod 755 main.rb 53 | ``` 54 | 55 | #### Now the file can be run directly without calling Ruby first 56 | 57 | ```bash 58 | $ ./main.rb 59 | ``` 60 | 61 | ## Authors 62 | 63 | 👤 **Sahar Abdel Samad** 64 | 65 | - GitHub: [@sahar-abdelsamad](https://github.com/Sahar-AbdelSamad) 66 | - Twitter: [@abdelsamadsahar](https://twitter.com/AbdelSamadSahar) 67 | - LinkedIn: [sahar-abdel-samad](https://www.linkedin.com/in/sahar-abdel-samad/) 68 | 69 | 👤 **Fernando Herrera** 70 | 71 | - GitHub: [@fherrerao](https://github.com/fherrerao); 72 | - LinkedIn: [Fernando Herrera](https://www.linkedin.com/in/fherrerao/); 73 | - Twitter: [Fernando Herrera](https://twitter.com/fherrera0206); 74 | 75 | ## 🤝 Contributing 76 | 77 | Contributions, issues, and feature requests are welcome! 78 | 79 | Feel free to check the [issues page](../../issues/). 80 | 81 | ## Show your support 82 | 83 | Give a ⭐️ if you like this project! 84 | 85 | ## Acknowledgments 86 | 87 | - Hat tip to anyone whose code was used 88 | - Inspiration 89 | - etc 90 | -------------------------------------------------------------------------------- /app.rb: -------------------------------------------------------------------------------- 1 | require './book' 2 | require './student' 3 | require './teacher' 4 | require './rental' 5 | require './person' 6 | require './checks' 7 | require_relative 'create_book' 8 | require_relative 'create_person' 9 | require './create_rental' 10 | require 'json' 11 | 12 | class App 13 | include Checks 14 | attr_accessor :books, :people 15 | 16 | def initialize 17 | @people = [] 18 | @books = [] 19 | @rents = [] 20 | @create_book = CreateBook.new(@books) 21 | @create_person = CreatePerson.new(@people) 22 | @create_rental = CreateRental.new(@rents, @books, @people) 23 | end 24 | 25 | def run(choice) 26 | case choice 27 | when 1 28 | list_books 29 | when 2 30 | list_people 31 | when 3 32 | @create_person.create_person 33 | when 4 34 | @create_book.create_book 35 | when 5 36 | @create_rental.create_rental 37 | when 6 38 | list_rental 39 | end 40 | end 41 | 42 | def load_books 43 | if File.exist?('books.json') 44 | data = JSON.parse(File.read('books.json'), create_additions: true) 45 | data.each do |book| 46 | @books.push(Book.new(book['title'], book['author'])) 47 | end 48 | else 49 | [] 50 | end 51 | end 52 | 53 | def load_people 54 | if File.exist?('people.json') 55 | data = JSON.parse(File.read('people.json'), create_additions: true) 56 | data.each do |person| 57 | case person['json_class'] 58 | when 'Student' 59 | student = Student.new(nil, person['age'], name: person['name'], 60 | parent_permission: person['parent_permission']) 61 | 62 | student.id = person['id'] 63 | @people.push(student) 64 | when 'Teacher' 65 | teacher = Teacher.new(person['specialization'], person['age'], name: person['name']) 66 | teacher.id = person['id'] 67 | @people.push(teacher) 68 | end 69 | end 70 | else 71 | [] 72 | end 73 | end 74 | 75 | def load_files 76 | load_books 77 | load_people 78 | load_rentals 79 | end 80 | 81 | def load_rentals 82 | if File.exist?('rentals.json') 83 | data = JSON.parse(File.read('rentals.json'), create_additions: true) 84 | data.map do |rentals| 85 | person = @people.find { |people| people.id == rentals['id_people'] } 86 | book = @books.find { |books| books.title == rentals['book_title'] } 87 | @rents.push(Rental.new(rentals['date'], book, person)) 88 | end 89 | else 90 | [] 91 | end 92 | end 93 | 94 | def list_books 95 | @books.each { |book| puts "Title: \"#{book.title}\", Author: \"#{book.author}\"" } 96 | end 97 | 98 | def list_people 99 | @people.each_with_index do |person, index| 100 | puts "#{index}) [#{person.class.name}] Name: #{person.name}, ID: #{person.id}, Age: #{person.age}" 101 | end 102 | end 103 | 104 | def list_rental 105 | print "ID of person:\s" 106 | id = gets.chomp.to_i 107 | puts "Rentals:\s" 108 | @rents.map do |rental| 109 | puts "Date: #{rental.date}, Book \"#{rental.book.title}\" by #{rental.book.author}\n\n" if id == rental.person.id 110 | end 111 | end 112 | 113 | def save_files 114 | File.write('books.json', JSON.generate(@books)) 115 | File.write('people.json', JSON.generate(@people)) 116 | File.write('rentals.json', JSON.generate(@rents)) 117 | end 118 | end 119 | -------------------------------------------------------------------------------- /book.rb: -------------------------------------------------------------------------------- 1 | class Book 2 | attr_accessor :rentals, :title, :author 3 | 4 | def initialize(title, author) 5 | @title = title 6 | @author = author 7 | @rentals = [] 8 | end 9 | 10 | def add_rental(date, person) 11 | rental = Rental.new(date, self, person) 12 | @rentals << rental unless @rentals.include?(rental) 13 | end 14 | 15 | def to_json(*args) 16 | { 17 | JSON.create_id => self.class.name, 18 | 'title' => @title, 19 | 'author' => @author 20 | }.to_json(*args) 21 | end 22 | end 23 | -------------------------------------------------------------------------------- /books.json: -------------------------------------------------------------------------------- 1 | [{"json_class":"Book","title":"fer","author":"fer"}] -------------------------------------------------------------------------------- /checks.rb: -------------------------------------------------------------------------------- 1 | module Checks 2 | def not_empty(message: 'Please enter a valid value') 3 | print message 4 | value = gets.chomp 5 | loop do 6 | break if value.length.positive? 7 | 8 | print message 9 | value = gets.chomp 10 | end 11 | value 12 | end 13 | 14 | def numeric(message: 'Please enter a valid value', error: "Please enter a valid number:\s") 15 | print message 16 | value = gets.chomp.to_i 17 | loop do 18 | break if value.positive? 19 | 20 | print error 21 | value = gets.chomp.to_i 22 | end 23 | value 24 | end 25 | 26 | def date(message: 'Please enter a valid value', error: "Please enter a date in the format YYYY/MM/DD:\s") 27 | print message 28 | value = gets.chomp 29 | loop do 30 | return value if value.match(%r{([0-9]){4}/([0-1])([0-9])/([0-3])([0-9])}) 31 | 32 | print error 33 | value = gets.chomp 34 | end 35 | value 36 | end 37 | end 38 | -------------------------------------------------------------------------------- /classroom.rb: -------------------------------------------------------------------------------- 1 | class Classroom 2 | attr_accessor :students, :label 3 | 4 | def initialize(label) 5 | @label = label 6 | @students = [] 7 | end 8 | 9 | def add_student(student) 10 | students.push(student) 11 | student.classroom = self 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /create_book.rb: -------------------------------------------------------------------------------- 1 | require './book' 2 | require './checks' 3 | 4 | class CreateBook 5 | include Checks 6 | 7 | def initialize(books) 8 | @books = books 9 | @book = nil 10 | end 11 | 12 | def create_book 13 | title = not_empty(message: "Title:\s") 14 | author = not_empty(message: "Author:\s") 15 | puts "Book created successfully \n\n" 16 | book = Book.new(title, author) 17 | @books << book unless @books.include?(book) 18 | end 19 | end 20 | -------------------------------------------------------------------------------- /create_person.rb: -------------------------------------------------------------------------------- 1 | require './create_student' 2 | require './create_teacher' 3 | 4 | class CreatePerson 5 | def initialize(person) 6 | @person = person 7 | @create_student = CreateStudent.new(@person) 8 | @create_teacher = CreateTeacher.new(@person) 9 | end 10 | 11 | def create_person 12 | loop do 13 | print "Do you want to create a student (1) or a teacher (2)? [Input the number]:\s" 14 | person = gets.chomp 15 | case person 16 | when '1' 17 | @create_student.create_student 18 | when '2' 19 | @create_teacher.create_teacher 20 | end 21 | 22 | break if %w[1 2].include?(person) 23 | end 24 | 25 | puts "Person created successfully\n\n" 26 | end 27 | end 28 | -------------------------------------------------------------------------------- /create_rental.rb: -------------------------------------------------------------------------------- 1 | require './rental' 2 | require './checks' 3 | require './app' 4 | 5 | class CreateRental 6 | include Checks 7 | 8 | def initialize(rents, books, people) 9 | @rents = rents 10 | @people = people 11 | @books = books 12 | end 13 | 14 | def create_rental 15 | select_book 16 | select_person 17 | date = date(message: "Date (format YYYY/MM/DD):\s") 18 | rent = Rental.new(date, @book, @person) 19 | @rents << rent unless @rents.include?(rent) 20 | puts "Rental created successfully\n\n" 21 | end 22 | 23 | def select_book 24 | return unless @books.length.positive? 25 | 26 | loop do 27 | puts 'Select a book from the following list by number' 28 | @books.each_with_index do |book, index| 29 | puts "#{index}) Title: \"#{book.title}\", Author: \"#{book.author}\"" 30 | end 31 | number = gets.chomp 32 | @book = @books[number.to_i] 33 | break if number.to_i < @books.length and number.to_i >= 0 and number.length.positive? 34 | end 35 | end 36 | 37 | def select_person 38 | return unless @books.length.positive? 39 | 40 | loop do 41 | puts 'Select a person from the following list by number (not id)' 42 | @people.each_with_index do |person, index| 43 | puts "#{index}) [#{person.class.name}] Name: #{person.name}, ID: #{person.id}, Age: #{person.age}" 44 | end 45 | number = gets.chomp 46 | @person = @people[number.to_i] 47 | break if number.to_i < @people.length and number.to_i >= 0 and number.length.positive? 48 | end 49 | end 50 | end 51 | -------------------------------------------------------------------------------- /create_student.rb: -------------------------------------------------------------------------------- 1 | require './student' 2 | require './checks' 3 | 4 | class CreateStudent 5 | include Checks 6 | 7 | def initialize(person) 8 | @person = person 9 | end 10 | 11 | def create_student 12 | age = numeric(message: "Age:\s") 13 | name = not_empty(message: "Name:\s") 14 | loop do 15 | print "Has parent permission [Y/N]?\s" 16 | permission = gets.chomp 17 | if %w[y Y].include?(permission) 18 | student = Student.new(nil, age, name: name, parent_permission: true) 19 | @person << student unless @person.include?(student) 20 | break 21 | elsif %w[n N].include?(permission) 22 | student = Student.new(nil, age, name: name, parent_permission: false) 23 | @person << student unless @person.include?(student) 24 | break 25 | end 26 | end 27 | end 28 | end 29 | -------------------------------------------------------------------------------- /create_teacher.rb: -------------------------------------------------------------------------------- 1 | require './teacher' 2 | require './checks' 3 | 4 | class CreateTeacher 5 | include Checks 6 | 7 | def initialize(person) 8 | @person = person 9 | end 10 | 11 | def create_teacher 12 | age = numeric(message: "Age:\s") 13 | name = not_empty(message: "Name:\s") 14 | specialization = not_empty(message: "Specialization:\s") 15 | teacher = Teacher.new(specialization, age, name: name) 16 | @person << teacher unless @person.include?(teacher) 17 | end 18 | end 19 | -------------------------------------------------------------------------------- /decorator.rb: -------------------------------------------------------------------------------- 1 | require_relative 'nameable' 2 | 3 | class Decorator < Nameable 4 | attr_accessor :nameable 5 | 6 | def initialize(nameable) 7 | super() 8 | @nameable = nameable 9 | end 10 | 11 | def correct_name 12 | @nameable.correct_name 13 | end 14 | end 15 | 16 | class CapitalizeDecorator < Decorator 17 | def correct_name 18 | @nameable.correct_name.capitalize 19 | end 20 | end 21 | 22 | class TrimmerDecorator < Decorator 23 | def correct_name 24 | @nameable.correct_name[0, 9] if @nameable.correct_name.length > 10 25 | end 26 | end 27 | -------------------------------------------------------------------------------- /main.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | require './app' 3 | require 'json' 4 | def main 5 | puts 'Welcome to school Library App!' 6 | app = App.new 7 | app.load_files 8 | loop do 9 | puts "Please choose an option by entering a number: 10 | 1 - List all books 11 | 2 - List all people 12 | 3 - Create a person 13 | 4 - Create a book 14 | 5 - Create a rental 15 | 6 - List all rentals for a given person id 16 | 7 - Exit" 17 | choice = gets.chomp.to_i 18 | 19 | if choice == 7 20 | app.save_files 21 | break 22 | end 23 | app.run(choice) 24 | end 25 | 26 | puts "Thank you for using this app!\n\n" 27 | end 28 | main 29 | -------------------------------------------------------------------------------- /nameable.rb: -------------------------------------------------------------------------------- 1 | class Nameable 2 | def correct_name 3 | raise NotImplementedError, "#{self.class} has not implemented method '#{__method__}'" 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /people.json: -------------------------------------------------------------------------------- 1 | [{"json_class":"Student","id":90,"name":"Student1","age":33,"parent_permission":true},{"json_class":"Teacher","id":54,"specialization":"math","age":44,"name":"Teacher1"}] -------------------------------------------------------------------------------- /person.rb: -------------------------------------------------------------------------------- 1 | class Person 2 | attr_accessor :name, :age, :rentals, :id 3 | 4 | def initialize(age, name: 'Unknown', parent_permission: true) 5 | super() 6 | @id = Random.rand(1..100) 7 | @name = name 8 | @age = age 9 | @parent_permission = parent_permission 10 | @rentals = [] 11 | end 12 | 13 | def can_use_services? 14 | of_age? || @parent_permission 15 | end 16 | 17 | def correct_name 18 | @name 19 | end 20 | 21 | def add_rental(date, book) 22 | rental = Rental.new(date, book, self) 23 | @rentals << rental unless @rentals.include?(rental) 24 | end 25 | 26 | private 27 | 28 | def of_age? 29 | @age >= 18 30 | end 31 | end 32 | -------------------------------------------------------------------------------- /rental.rb: -------------------------------------------------------------------------------- 1 | class Rental 2 | attr_accessor :person, :date, :book 3 | 4 | def initialize(date, book, person) 5 | @person = person 6 | person.rentals << self 7 | @book = book 8 | book.rentals << self 9 | @date = date 10 | end 11 | 12 | def to_json(*args) 13 | { 14 | JSON.create_id => self.class.name, 15 | 'date' => @date, 16 | 'book_title' => @book.title, 17 | 'id_people' => @person.id 18 | }.to_json(*args) 19 | end 20 | end 21 | -------------------------------------------------------------------------------- /rentals.json: -------------------------------------------------------------------------------- 1 | [{"json_class":"Rental","date":"2022/10/22","book_title":"fer","id_people":90}] -------------------------------------------------------------------------------- /screenshot.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sahar-AbdelSamad/OOP-school-library/de9d513f707da1a504adc2b550588b97a0113d89/screenshot.gif -------------------------------------------------------------------------------- /spec/book_spec.rb: -------------------------------------------------------------------------------- 1 | require_relative '../book' 2 | 3 | describe Book do 4 | before(:each) do 5 | @book = Book.new('Ruby', 'Sahar') 6 | end 7 | 8 | it 'should have a title' do 9 | expect(@book.title).to eq('Ruby') 10 | end 11 | 12 | it 'should have an author' do 13 | expect(@book.author).to eq('Sahar') 14 | end 15 | 16 | it 'should have rentals' do 17 | expect(@book.rentals).to eq([]) 18 | end 19 | end 20 | -------------------------------------------------------------------------------- /spec/classroom_spec.rb: -------------------------------------------------------------------------------- 1 | require_relative '../classroom' 2 | require_relative '../student' 3 | 4 | describe Classroom do 5 | before(:each) do 6 | @classroom = Classroom.new('A') 7 | end 8 | 9 | it 'takes 1 parameter and returns a Classroom object' do 10 | expect(@classroom).to be_an_instance_of Classroom 11 | end 12 | 13 | it 'should have a label' do 14 | expect(@classroom.label).to eq('A') 15 | end 16 | 17 | it 'should have an empty students array' do 18 | expect(@classroom.students).to eq([]) 19 | end 20 | 21 | it 'should add a student to the students array' do 22 | student = Student.new('1A', 18, name: 'John', parent_permission: true) 23 | @classroom.add_student(student) 24 | expect(@classroom.students).to eq([student]) 25 | end 26 | end 27 | -------------------------------------------------------------------------------- /spec/decorator_spec.rb: -------------------------------------------------------------------------------- 1 | require_relative '../decorator' 2 | require_relative '../nameable' 3 | 4 | describe Decorator do 5 | before(:each) do 6 | @decorator = Decorator.new('Fernando') 7 | end 8 | 9 | it 'should have a nameable' do 10 | expect(@decorator.nameable).to eq('Fernando') 11 | end 12 | end 13 | -------------------------------------------------------------------------------- /spec/person_spec.rb: -------------------------------------------------------------------------------- 1 | require_relative '../person' 2 | 3 | describe Person do 4 | before(:each) do 5 | @person = Person.new(18, name: 'John', parent_permission: true) 6 | end 7 | 8 | it 'should have a name' do 9 | expect(@person.name).to eq('John') 10 | end 11 | 12 | it 'should have an age' do 13 | expect(@person.age).to eq(18) 14 | end 15 | 16 | it 'should have rentals' do 17 | expect(@person.rentals).to eq([]) 18 | end 19 | 20 | # it 'should have a parent_permission' do 21 | # expect(@person.parent_permission).to eq(true) 22 | # end 23 | 24 | it 'should have a can_use_services?' do 25 | expect(@person.can_use_services?).to eq(true) 26 | end 27 | 28 | # it 'should have a correct_name' do 29 | # expect(@person.correct_name).to eq('John') 30 | # end 31 | 32 | # it 'should have a to_json' do 33 | # expect(@person.to_json).to eq('{"id":1,"name":"John","age":18,"parent_permission":true}') 34 | # end 35 | end 36 | -------------------------------------------------------------------------------- /spec/rental_spec.rb: -------------------------------------------------------------------------------- 1 | require_relative '../rental' 2 | require_relative '../person' 3 | require_relative '../book' 4 | describe Rental do 5 | before(:each) do 6 | @person = Person.new(18, name: 'John', parent_permission: true) 7 | @book = Book.new('The Hobbit', 'J.R.R. Tolkien') 8 | @rental = Rental.new('2020/10/10', @book, @person) 9 | end 10 | 11 | it 'takes 3 parameters and returns a Rental object' do 12 | expect(@rental).to be_an_instance_of Rental 13 | end 14 | 15 | it 'should have a date' do 16 | expect(@rental.date).to eq('2020/10/10') 17 | end 18 | 19 | it 'should have a book' do 20 | expect(@rental.book).to eq(@book) 21 | end 22 | 23 | it 'should have a person' do 24 | expect(@rental.person).to eq(@person) 25 | end 26 | end 27 | -------------------------------------------------------------------------------- /spec/student_spec.rb: -------------------------------------------------------------------------------- 1 | require_relative '../student' 2 | require_relative '../person' 3 | 4 | describe Student do 5 | before(:each) do 6 | @student = Student.new('1A', 18, name: 'John', parent_permission: true) 7 | end 8 | 9 | it 'should have a name' do 10 | expect(@student.name).to eq('John') 11 | end 12 | 13 | it 'should have an age' do 14 | expect(@student.age).to eq(18) 15 | end 16 | 17 | it 'should have rentals' do 18 | expect(@student.rentals).to eq([]) 19 | end 20 | 21 | it 'should have a parent_permission' do 22 | expect(@student.parent_permission).to eq(true) 23 | end 24 | 25 | it 'should have a can_use_services?' do 26 | expect(@student.can_use_services?).to eq(true) 27 | end 28 | 29 | it 'should have a correct_name' do 30 | expect(@student.correct_name).to eq('John') 31 | end 32 | end 33 | -------------------------------------------------------------------------------- /spec/teacher_spec.rb: -------------------------------------------------------------------------------- 1 | require_relative '../teacher' 2 | describe Teacher do 3 | before(:each) do 4 | @teacher = Teacher.new('Maths', 18, name: 'John', parent_permission: true) 5 | end 6 | it 'should have a name' do 7 | expect(@teacher.name).to eq('John') 8 | end 9 | it 'should have an age' do 10 | expect(@teacher.age).to eq(18) 11 | end 12 | it 'should have rentals' do 13 | expect(@teacher.rentals).to eq([]) 14 | end 15 | it 'should have a parent_permission' do 16 | expect(@teacher.parent_permission).to eq(true) 17 | end 18 | it 'should have a can_use_services?' do 19 | expect(@teacher.can_use_services?).to eq(true) 20 | end 21 | it 'should have a correct_name' do 22 | expect(@teacher.correct_name).to eq('John') 23 | end 24 | end 25 | -------------------------------------------------------------------------------- /student.rb: -------------------------------------------------------------------------------- 1 | require_relative 'person' 2 | 3 | class Student < Person 4 | attr_accessor :classroom, :parent_permission 5 | 6 | def initialize(classroom, age, name: 'Unknown', parent_permission: true) 7 | super(age, name: name, parent_permission: parent_permission) 8 | @classroom = classroom 9 | end 10 | 11 | def play_hooky 12 | "¯\(ツ)/¯" 13 | end 14 | 15 | def change_classroom=(classroom) 16 | @classroom = classroom 17 | classroom.students.push(self) unless classroom.students.include?(self) 18 | end 19 | 20 | def to_json(*args) 21 | { 22 | JSON.create_id => self.class.name, 23 | 'id' => @id, 24 | 'name' => @name, 25 | 'age' => @age, 26 | 'parent_permission' => @parent_permission 27 | }.to_json(*args) 28 | end 29 | end 30 | -------------------------------------------------------------------------------- /teacher.rb: -------------------------------------------------------------------------------- 1 | require_relative 'person' 2 | class Teacher < Person 3 | attr_accessor :parent_permission 4 | 5 | def initialize(specialization, age, name: 'Unknown', parent_permission: true) 6 | super(age, name: name, parent_permission: parent_permission) 7 | @specialization = specialization 8 | end 9 | 10 | def can_use_services? 11 | true 12 | end 13 | 14 | def to_json(*args) 15 | { 16 | JSON.create_id => self.class.name, 17 | 'id' => @id, 18 | 'specialization' => @specialization, 19 | 'age' => @age, 20 | 'name' => @name 21 | }.to_json(*args) 22 | end 23 | end 24 | --------------------------------------------------------------------------------