Class: MoviesNews::Article

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

Constant Summary collapse

@@articles =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title, author = nil, story = nil) ⇒ Article

Returns a new instance of Article.



6
7
8
9
# File 'lib/movies_news/article.rb', line 6

def initialize(title, author = nil, story = nil)
  @title = title
  self.author = author unless author.nil?
end

Instance Attribute Details

#authorObject

Returns the value of attribute author.



2
3
4
# File 'lib/movies_news/article.rb', line 2

def author
  @author
end

#storyObject

Returns the value of attribute story.



2
3
4
# File 'lib/movies_news/article.rb', line 2

def story
  @story
end

#titleObject

Returns the value of attribute title.



2
3
4
# File 'lib/movies_news/article.rb', line 2

def title
  @title
end

Class Method Details

.allObject



11
12
13
# File 'lib/movies_news/article.rb', line 11

def self.all
  @@articles
end

.create(title) ⇒ Object



24
25
26
27
28
# File 'lib/movies_news/article.rb', line 24

def self.create(title)
  article = self.new(title)
  article.save
  article
end

.create_from_array(array) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/movies_news/article.rb', line 43

def self.create_from_array(array)
  array.each do |article|
    title = article[:title]
    author = article[:author]
    story = article[:story]

    new_author = MoviesNews::Author.find_or_create_by_name(author)
    new_article = self.new(title, new_author)
    new_article.story = story
    new_article.save
  end
end

.destroy_allObject



19
20
21
# File 'lib/movies_news/article.rb', line 19

def self.destroy_all
  self.all.clear
end

.find_by_title(title) ⇒ Object



35
36
37
# File 'lib/movies_news/article.rb', line 35

def self.find_by_title(title)
  self.all.detect { |x| x.title == title }
end

.find_or_create_by_title(title) ⇒ Object



39
40
41
# File 'lib/movies_news/article.rb', line 39

def self.find_or_create_by_title(title)
  self.find_by_title(title) || self.create(title)
end

.get_articlesObject



56
57
58
59
# File 'lib/movies_news/article.rb', line 56

def self.get_articles
  self.create_from_array(MoviesNews::Scrape.make_articles)
  @@articles
end

Instance Method Details

#saveObject



15
16
17
# File 'lib/movies_news/article.rb', line 15

def save
  @@articles << self unless MoviesNews::Article.all.detect {|a| a.title == self.title }
end