Skip to content

Commit

Permalink
Changes as reviewed
Browse files Browse the repository at this point in the history
  • Loading branch information
lilskyex0x committed Nov 17, 2023
1 parent b4df7a2 commit 9102f48
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 16 deletions.
2 changes: 1 addition & 1 deletion classes/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ class Person < Nameable
attr_accessor :name, :age, :rentals

def initialize(age: nil, name: 'Unknown', parent_permission: true)
super()
@id = Random.rand(1..1000)
@age = age
@name = name
@parent_permission = parent_permission
@rentals = []
super()
end

def add_rental(book, date)
Expand Down
2 changes: 1 addition & 1 deletion classes/student.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def to_json(*_args)
'age' => @age,
'name' => @name,
'parent_permission' => @parent_permission,
'classroom' => @classroom
'classroom' => @classroom.label
}.to_json
end
end
27 changes: 20 additions & 7 deletions data/rentals.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
"object_id": 100,
"title": "Ruby 101",
"author": "Tshepo",
"rentals": [

]
"rentals": []
},
"person": {
"id": 716,
Expand All @@ -25,9 +23,7 @@
"object_id": 120,
"title": "Computer Science",
"author": "Dumisani",
"rentals": [

]
"rentals": []
},
"person": {
"id": 906,
Expand All @@ -36,5 +32,22 @@
"parent_permission": true,
"specialization": "Botany"
}
},
{
"date": "2023-11-17",
"book": {
"_class": "Book",
"object_id": 120,
"title": "Computer Science",
"author": "Dumisani",
"rentals": []
},
"person": {
"id": 716,
"age": "23",
"name": "MyaMya",
"parent_permission": true,
"classroom": "Sport"
}
}
]
]
4 changes: 3 additions & 1 deletion spec/book_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
it 'to_json should turn book into a json file' do
file_path = './data/book_test.json'
json = File.exist?(file_path) ? JSON.parse(File.read(file_path)) : {}
File.write(file_path, JSON.pretty_generate(book))
json_data = JSON.parse(book.to_json)
expect(book.title).to eql(json_data['title'])
expect(book.author).to eql(json_data['author'])
puts "book.title: #{book.title}"
puts "json['title']: #{json['title']}"
expect(book.title).to eql(json['title'])
Expand Down
12 changes: 12 additions & 0 deletions spec/decorator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require './classes/decorator'

RSpec.describe Decorator do
let(:nameable) { instance_double('Nameable', correct_name: 'Original Name') }
subject { described_class.new(nameable) }

describe '#correct_name' do
it 'delegates to the correct_name method of the underlying nameable object' do
expect(subject.correct_name).to eq('Original Name')
end
end
end
12 changes: 12 additions & 0 deletions spec/nameable_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'rspec'
require './classes/nameable'

describe Nameable do
let(:nameable_instance) { Nameable.new }

describe '#correct_name' do
it 'raises NotImplementedError' do
expect { nameable_instance.correct_name }.to raise_error(NotImplementedError, /#{__method__}/)
end
end
end
14 changes: 11 additions & 3 deletions spec/rental_spec.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
require 'rspec'
require 'json'
require './classes/book'
require './classes/teacher'
require './classes/rental'

describe Book do
context 'Given a Book named The Sphere by Michael Crichton' do
before :each do
@person = Person.new(age: 24, name: 'Santiago')
@person = Teacher.new(specialization: 'Mathematics', age: 52, name: 'Lilsnow', parent_permission: true)
@book = Book.new('Sphere', 'Michael Crichton')
end

it '@rental should be instance of Rental' do
it 'to_json should return a valid JSON representation of the rental' do
rental = Rental.new('2020-10-23', @book, @person)
expect(rental).to be_an_instance_of(Rental)
json_data = JSON.parse(rental.to_json)

expect(json_data['date']).to eq('2020-10-23')
expect(json_data['book']['title']).to eq('Sphere')
expect(json_data['book']['author']).to eq('Michael Crichton')
expect(json_data['person']['age']).to eq(52)
expect(json_data['person']['name']).to eq('Lilsnow')
end
end
end
17 changes: 14 additions & 3 deletions spec/student_spec.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
require 'rspec'
require 'json'
require_relative '../classes/student'
require_relative '../classes/classroom'

describe Student do
context 'Given a Student named Santiago' do
context 'Given a Student named Lilsnow' do
before :each do
@classroom = Classroom.new('Gepgraphy')
@student = Student.new(age: 24, name: 'Santiago', parent_permission: false, classroom: @classroom)
@classroom = Classroom.new('Geography')
@student = Student.new(age: 24, name: 'Lilsnow', parent_permission: false, classroom: @classroom)
end
it '@student be an instance of Student' do
expect(@student).to be_an_instance_of(Student)
Expand All @@ -24,5 +25,15 @@
it 'plays hooky' do
expect(@student.play_hooky).to eq('¯\(ツ)/¯')
end

it 'to_json should return a valid JSON representation of the student' do
json_data = JSON.parse(@student.to_json)

expect(json_data['id']).to be_instance_of(Integer)
expect(json_data['age']).to eq(24)
expect(json_data['name']).to eq('Lilsnow')
expect(json_data['parent_permission']).to be_falsy
expect(json_data['classroom']).to eq('Geography') # Adjust based on your implementation
end
end
end
13 changes: 13 additions & 0 deletions spec/teacher_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'json'
require './classes/teacher'

RSpec.describe Teacher do
Expand Down Expand Up @@ -25,4 +26,16 @@
expect(subject.can_use_services?).to be true
end
end

describe '#to_json' do
it 'returns a valid JSON representation of the teacher' do
json_data = JSON.parse(subject.to_json)

expect(json_data['id']).to be_a(Integer)
expect(json_data['age']).to eq(age)
expect(json_data['name']).to eq(name)
expect(json_data['parent_permission']).to eq(parent_permission)
expect(json_data['specialization']).to eq(specialization)
end
end
end

0 comments on commit 9102f48

Please sign in to comment.