Class: Movie

Inherits:
Object
  • Object
show all
Defined in:
lib/imdb_term/movie.rb

Constant Summary collapse

@@all =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(movie_data) ⇒ Movie

Returns a new instance of Movie.



7
8
9
10
11
# File 'lib/imdb_term/movie.rb', line 7

def initialize(movie_data)
  movie_data.each do |key, value|
    self.send("#{key}=", value)
  end
end

Instance Attribute Details

#content_ratingObject

Returns the value of attribute content_rating.



3
4
5
# File 'lib/imdb_term/movie.rb', line 3

def content_rating
  @content_rating
end

#directorObject

Returns the value of attribute director.



3
4
5
# File 'lib/imdb_term/movie.rb', line 3

def director
  @director
end

#genresObject

Returns the value of attribute genres.



3
4
5
# File 'lib/imdb_term/movie.rb', line 3

def genres
  @genres
end

#idObject

Returns the value of attribute id.



3
4
5
# File 'lib/imdb_term/movie.rb', line 3

def id
  @id
end

#release_yearObject

Returns the value of attribute release_year.



3
4
5
# File 'lib/imdb_term/movie.rb', line 3

def release_year
  @release_year
end

#runtimeObject

Returns the value of attribute runtime.



3
4
5
# File 'lib/imdb_term/movie.rb', line 3

def runtime
  @runtime
end

#starsObject

Returns the value of attribute stars.



3
4
5
# File 'lib/imdb_term/movie.rb', line 3

def stars
  @stars
end

#summaryObject

Returns the value of attribute summary.



3
4
5
# File 'lib/imdb_term/movie.rb', line 3

def summary
  @summary
end

#titleObject

Returns the value of attribute title.



3
4
5
# File 'lib/imdb_term/movie.rb', line 3

def title
  @title
end

#typeObject

Returns the value of attribute type.



3
4
5
# File 'lib/imdb_term/movie.rb', line 3

def type
  @type
end

Class Method Details

.allObject



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

def self.all
  @@all
end

.create(movie_data) ⇒ Object



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

def self.create(movie_data)
  movie = self.new(movie_data)
  movie.save
  movie
end

.create_from_array(array) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/imdb_term/movie.rb', line 52

def self.create_from_array(array)
  movies = Array.new
  array.each do |movie_data|
    movies << self.create_or_update(movie_data)
  end
  movies
end

.create_or_update(movie_data) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/imdb_term/movie.rb', line 34

def self.create_or_update(movie_data)
  movie = self.find_by_id(movie_data[:id])
  if movie.nil?
    movie = self.create(movie_data)
  else
    movie.update(movie_data)
  end
  movie
end

.find_by_id(id) ⇒ Object



60
61
62
# File 'lib/imdb_term/movie.rb', line 60

def self.find_by_id(id)
  self.all.detect { |movie| movie.id.eql?(id) }
end

.find_by_title(title) ⇒ Object



69
70
71
# File 'lib/imdb_term/movie.rb', line 69

def self.find_by_title(title)
  self.all.detect { |movie| movie.title.casecmp(title) == 0 }
end

.find_or_create_by_id(id) ⇒ Object



64
65
66
67
# File 'lib/imdb_term/movie.rb', line 64

def self.find_or_create_by_id(id)
  movie = self.find_by_id(id)
  movie.nil? ? self.create({:id => id}) : movie
end

.new_from_array(array) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/imdb_term/movie.rb', line 44

def self.new_from_array(array)
  movies = Array.new
  array.each do |movie_data|
    movies << self.new(movie_data)
  end
  movies
end

Instance Method Details

#saveObject



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

def save
  @@all << self
  self
end

#update(movie_data) ⇒ Object



13
14
15
16
17
# File 'lib/imdb_term/movie.rb', line 13

def update(movie_data)
  movie_data.each do |key, value|
    self.send("#{key}=", value)
  end
end