Sha256: 1328b29b226dae24f7e5a982570ae7d3b639e59e32c11f0f353d27b1fe14565f

Contents?: true

Size: 1.24 KB

Versions: 2

Compression:

Stored size: 1.24 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

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
imdb_term-1.0.2 lib/imdb_term/movie.rb
imdb_term-1.0.1 lib/imdb_term/movie.rb