Class: MoviesNews::Author
- Inherits:
-
Object
- Object
- MoviesNews::Author
- Defined in:
- lib/movies_news/author.rb
Constant Summary collapse
- @@all =
[]
Instance Attribute Summary collapse
-
#articles ⇒ Object
Returns the value of attribute articles.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
- .all ⇒ Object
- .create(name) ⇒ Object
- .destroy_all ⇒ Object
- .find_by_name(name) ⇒ Object
- .find_or_create_by_name(name) ⇒ Object
Instance Method Summary collapse
- #add_article(article) ⇒ Object
-
#initialize(name) ⇒ Author
constructor
A new instance of Author.
- #save ⇒ Object
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
#articles ⇒ Object
Returns the value of attribute articles.
4 5 6 |
# File 'lib/movies_news/author.rb', line 4 def articles @articles end |
#name ⇒ Object (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
.all ⇒ Object
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) = self.new(name) .save end |
.destroy_all ⇒ Object
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. = self if article..nil? self.articles << article unless self.articles.include?(article) end |
#save ⇒ Object
17 18 19 |
# File 'lib/movies_news/author.rb', line 17 def save @@all << self end |