Sha256: 059e329eabf43a7993b98ed42865e6a8eec2f61b7fbe24bb468a5aa8d63d320c

Contents?: true

Size: 1.34 KB

Versions: 1

Compression:

Stored size: 1.34 KB

Contents

class Movie

  attr_accessor :id, :title, :release_year, :content_rating, :runtime, :genres, :summary, :director, :stars, :type

  @@all = []

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

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

  def save
    @@all << self
    self
  end

  def self.all
    @@all
  end

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

  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

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

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

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

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

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

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
imdb_term-1.0.4 lib/imdb_term/movie.rb