Class: Songfile::Song

Inherits:
Object
  • Object
show all
Defined in:
lib/songfile/song.rb

Direct Known Subclasses

ClassicSong

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title, rank = 10000) ⇒ Song

this method converts a Song.new specified title to the correct format when created



19
20
21
22
23
# File 'lib/songfile/song.rb', line 19

def initialize(title, rank=10000)       #this method converts a Song.new specified title to the correct format when created
    @title = titleize(title)
    @rank = rank.abs() # the absolute value prevents anyone from typing in a NEGATIVE rank
    puts "New song '#{@title}' with rank of #{@rank} was created."
end

Instance Attribute Details

#rankObject

attr_reader :rank # allows you to print the “rank” attribute outside the Song class (GETTER method) attr_writer :title, :rank # allows you to write to the “title” and “rank” attributes outside the Song class (SETTER method)



13
14
15
# File 'lib/songfile/song.rb', line 13

def rank
  @rank
end

#titleObject

attr_reader :rank # allows you to print the “rank” attribute outside the Song class (GETTER method) attr_writer :title, :rank # allows you to write to the “title” and “rank” attributes outside the Song class (SETTER method)



13
14
15
# File 'lib/songfile/song.rb', line 13

def title
  @title
end

Instance Method Details

#describeObject



40
41
42
# File 'lib/songfile/song.rb', line 40

def describe 
    puts "'#{@title}' has a rank of #{@rank}."
end

#statusObject



50
51
52
# File 'lib/songfile/song.rb', line 50

def status
    top_ten? ? "*Top 10*" : "*Not Top 10*"
end

#thumbs_down(value = 1) ⇒ Object



35
36
37
38
# File 'lib/songfile/song.rb', line 35

def thumbs_down(value=1)
    @rank += value
    puts "'#{@title}' received a thumbs down!"
end

#thumbs_up(value = 1) ⇒ Object



30
31
32
33
# File 'lib/songfile/song.rb', line 30

def thumbs_up(value=1)
    @rank -= value
    puts "'#{@title}' received a thumbs up!"
end

#titleize(title) ⇒ Object

the actual titleize method (only hangup is “Like” (as a verb) vs “like” (as a preposition))



15
16
17
# File 'lib/songfile/song.rb', line 15

def titleize(title) #the actual titleize method (only hangup is "Like" (as a verb) vs "like" (as a preposition))
    title.downcase.split.each_with_index.map{ |x, index| $Lowercase_words.include?(x) && index > 0 ? x : x.capitalize }.join(" ")
end

#to_sObject

defines what happens when you use puts on an object of class “Song”



54
55
56
57
58
# File 'lib/songfile/song.rb', line 54

def to_s #defines what happens when you use puts on an object of class "Song"
    "#{@title} (#{@rank})"
    #"#{@title} (#{@rank}) #{status}"
    #^^REMOVING "top 10" classification while the score is still computing!
end

#top_ten?Boolean

Returns:

  • (Boolean)


44
45
46
47
48
# File 'lib/songfile/song.rb', line 44

def top_ten?
    result = case @rank
    when 1..10 then true
    end
end