Class: Movie

Inherits:
Object
  • Object
show all
Includes:
Rankable
Defined in:
lib/flicks/movie.rb

Direct Known Subclasses

Movie3D

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Rankable

#<=>, #hit?, #normalized_rank, #status, #thumbs_down, #thumbs_up

Constructor Details

#initialize(title, rank = 0) ⇒ Movie

Returns a new instance of Movie.



9
10
11
12
13
# File 'lib/flicks/movie.rb', line 9

def initialize(title, rank=0)
    @title = title.capitalize
    @rank = rank
    @snack_carbs = Hash.new(0)
end

Instance Attribute Details

#rankObject

Returns the value of attribute rank.



6
7
8
# File 'lib/flicks/movie.rb', line 6

def rank
  @rank
end

#titleObject

Returns the value of attribute title.



7
8
9
# File 'lib/flicks/movie.rb', line 7

def title
  @title
end

Class Method Details

.from_csv(line) ⇒ Object



15
16
17
18
# File 'lib/flicks/movie.rb', line 15

def self.from_csv(line)
    title, rank = line.split(",")
    Movie.new(title, Integer(rank))
end

Instance Method Details

#ate_snack(snack) ⇒ Object



28
29
30
31
32
# File 'lib/flicks/movie.rb', line 28

def ate_snack(snack)
    @snack_carbs[snack.name] += snack.carbs
    puts "#{@title} led to #{snack.carbs} #{snack.name} carbs being consumed."
    puts "#{@title}'s snacks: #{@snack_carbs}"
end

#carbs_consumedObject



34
35
36
# File 'lib/flicks/movie.rb', line 34

def carbs_consumed
    @snack_carbs.values.reduce(0, :+)
end

#each_snackObject



38
39
40
41
42
43
# File 'lib/flicks/movie.rb', line 38

def each_snack
    @snack_carbs.each do |name, carbs|
        snack = Snack.new(name, carbs)
        yield(snack)
    end
end

#to_csvObject



24
25
26
# File 'lib/flicks/movie.rb', line 24

def to_csv
    "#{title},#{rank}"
end

#to_sObject



20
21
22
# File 'lib/flicks/movie.rb', line 20

def to_s
    "#{@title} has a rank of #{normalized_rank} (#{status})"
end