Class: MoviesNews::Article
- Inherits:
-
Object
- Object
- MoviesNews::Article
- Defined in:
- lib/movies_news/article.rb
Constant Summary collapse
- @@articles =
[]
Instance Attribute Summary collapse
-
#author ⇒ Object
Returns the value of attribute author.
-
#story ⇒ Object
Returns the value of attribute story.
-
#title ⇒ Object
Returns the value of attribute title.
Class Method Summary collapse
- .all ⇒ Object
- .create(title) ⇒ Object
- .create_from_array(array) ⇒ Object
- .destroy_all ⇒ Object
- .find_by_title(title) ⇒ Object
- .find_or_create_by_title(title) ⇒ Object
- .get_articles ⇒ Object
Instance Method Summary collapse
-
#initialize(title, author = nil, story = nil) ⇒ Article
constructor
A new instance of Article.
- #save ⇒ Object
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, = nil, story = nil) @title = title self. = unless .nil? end |
Instance Attribute Details
#author ⇒ Object
Returns the value of attribute author.
2 3 4 |
# File 'lib/movies_news/article.rb', line 2 def @author end |
#story ⇒ Object
Returns the value of attribute story.
2 3 4 |
# File 'lib/movies_news/article.rb', line 2 def story @story end |
#title ⇒ Object
Returns the value of attribute title.
2 3 4 |
# File 'lib/movies_news/article.rb', line 2 def title @title end |
Class Method Details
.all ⇒ Object
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] = article[:author] story = article[:story] = MoviesNews::Author.find_or_create_by_name() new_article = self.new(title, ) new_article.story = story new_article.save end end |
.destroy_all ⇒ Object
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_articles ⇒ Object
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
#save ⇒ Object
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 |