Last active
December 25, 2015 14:29
-
-
Save J-Scag/6991502 to your computer and use it in GitHub Desktop.
Serialize TODO for Flatiron School day 16
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
| RSpec.configure do |config| | |
| # Use color in STDOUT | |
| config.color_enabled = true | |
| # Use color not only in STDOUT but also in pagers and files | |
| config.tty = true | |
| # Use the specified formatter | |
| config.formatter = :progress # :progress, :html, :textmate | |
| end | |
| #implement a song class and artist class to pass the specs below. | |
| #serialize method should replace spaces in the song title with underscores | |
| #and write to the current working directory | |
| class Song | |
| attr_accessor :title, :artist | |
| def serialize | |
| File.open("#{self.title.downcase.gsub(" ", "_")}.txt", "w+") do |f| | |
| f.write("#{self.artist.name} - #{self.title}") | |
| end | |
| end | |
| def self.deserialize(file) | |
| content = (File.open(file)).read | |
| new_song = Song.new | |
| new_song.artist = Artist.new(content.split(" - ")[0]) | |
| new_song.title = content.split(" - ")[1] | |
| new_song | |
| end | |
| end | |
| class Artist | |
| attr_accessor :name | |
| def initialize(name) | |
| @name = name | |
| end | |
| end | |
| describe Song do | |
| it "has a title" do | |
| song = Song.new | |
| song.title = "Night Moves" | |
| song.title.should eq("Night Moves") | |
| end | |
| it "has an artist" do | |
| song = Song.new | |
| song.title = "Night Moves" | |
| song.artist = Artist.new("Bob Seger") | |
| song.artist.name.should eq("Bob Seger") | |
| end | |
| it "can save a representation of itself to a file" do | |
| song = Song.new | |
| song.title = "Night Moves" | |
| song.artist = Artist.new("Bob Seger") | |
| song.serialize | |
| File.read("night_moves.txt").should match /Bob Seger - Night Moves/ | |
| end | |
| it "can read a file and create new instances based on its contents" do | |
| new_song = Song.deserialize("night_moves.txt") | |
| new_song.title.should eq("Night Moves") | |
| new_song.artist.name.should eq("Bob Seger") | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment