Class: MoviesNews::Author

Inherits:
Object
  • Object
show all
Defined in:
lib/movies_news/author.rb

Constant Summary collapse

@@all =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Author

Returns a new instance of Author.



8
9
10
11
# File 'lib/movies_news/author.rb', line 8

def initialize(name)
  @name = name
  @articles = []
end

Instance Attribute Details

#articlesObject

Returns the value of attribute articles.



4
5
6
# File 'lib/movies_news/author.rb', line 4

def articles
  @articles
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/movies_news/author.rb', line 5

def name
  @name
end

Class Method Details

.allObject



13
14
15
# File 'lib/movies_news/author.rb', line 13

def self.all
  @@all
end

.create(name) ⇒ Object



25
26
27
28
29
# File 'lib/movies_news/author.rb', line 25

def self.create(name)
  author = self.new(name)
  author.save
  author
end

.destroy_allObject



21
22
23
# File 'lib/movies_news/author.rb', line 21

def self.destroy_all
  self.all.clear
end

.find_by_name(name) ⇒ Object



36
37
38
# File 'lib/movies_news/author.rb', line 36

def self.find_by_name(name)
  self.all.detect { |x| x.name == name }
end

.find_or_create_by_name(name) ⇒ Object



40
41
42
# File 'lib/movies_news/author.rb', line 40

def self.find_or_create_by_name(name)
  self.find_by_name(name) || self.create(name)
end

Instance Method Details

#add_article(article) ⇒ Object



31
32
33
34
# File 'lib/movies_news/author.rb', line 31

def add_article(article)
  article.author = self if article.author.nil?
  self.articles << article unless self.articles.include?(article)
end

#saveObject



17
18
19
# File 'lib/movies_news/author.rb', line 17

def save
 @@all << self
end