Class: Book

Inherits:
Object
  • Object
show all
Extended by:
Findable::ClassMethods, Memorable::ClassMethods, Sortable::ClassMethods
Includes:
Memorable::InstanceMethods
Defined in:
lib/podcast_book_club/book.rb

Constant Summary collapse

@@all =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Memorable::ClassMethods

count, create, destroy_all

Methods included from Findable::ClassMethods

find_by_name, find_by_title, find_or_create_by_name, find_or_create_by_title

Methods included from Sortable::ClassMethods

sort_by_name, sort_by_title

Methods included from Memorable::InstanceMethods

#save

Constructor Details

#initialize(attributes) ⇒ Book

Returns a new instance of Book.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/podcast_book_club/book.rb', line 11

def initialize(attributes)
    @episode = []

    attributes.each do |k,v|
        unless v == nil

            if "#{k}" == "genre"
                self.add_genre(v)
            elsif "#{k}" == "episode"
                self.add_episode(v)
            elsif "#{k}" == "author"
                self.add_author(v)
            else
                self.send("#{k}=", v)
            end

        end
    end

end

Instance Attribute Details

#authorObject

Returns the value of attribute author.



2
3
4
# File 'lib/podcast_book_club/book.rb', line 2

def author
  @author
end

#episodeObject

Returns the value of attribute episode.



2
3
4
# File 'lib/podcast_book_club/book.rb', line 2

def episode
  @episode
end

#genreObject

Returns the value of attribute genre.



2
3
4
# File 'lib/podcast_book_club/book.rb', line 2

def genre
  @genre
end

#synopsisObject

Returns the value of attribute synopsis.



2
3
4
# File 'lib/podcast_book_club/book.rb', line 2

def synopsis
  @synopsis
end

#titleObject

Returns the value of attribute title.



2
3
4
# File 'lib/podcast_book_club/book.rb', line 2

def title
  @title
end

#urlObject

Returns the value of attribute url.



2
3
4
# File 'lib/podcast_book_club/book.rb', line 2

def url
  @url
end

Class Method Details

.allObject



32
33
34
# File 'lib/podcast_book_club/book.rb', line 32

def self.all
    @@all
end

.find_by_keyword(keyword) ⇒ Object



64
65
66
# File 'lib/podcast_book_club/book.rb', line 64

def self.find_by_keyword(keyword)
    self.all.select { |book| book.title.downcase.include?(keyword) || book.synopsis.downcase.include?(keyword) unless book.synopsis == nil }
end

Instance Method Details

#add_author(authors) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/podcast_book_club/book.rb', line 40

def add_author(authors)
    @author ||= []

    if authors.kind_of?(Array)
        authors.each do |a|
            author = Author.find_or_create_by_name(a)
            @author << author
            author.books << self
        end
    else
        author = Author.find_or_create_by_name(authors)
        @author << author
        author.books = self
    end

end

#add_episode(episode) ⇒ Object



36
37
38
# File 'lib/podcast_book_club/book.rb', line 36

def add_episode(episode)
    episode.add_book(self)
end

#add_genre(genre) ⇒ Object



57
58
59
60
61
62
# File 'lib/podcast_book_club/book.rb', line 57

def add_genre(genre)
    @genre ||= []

    new_genre = Genre.find_or_create_by_name(genre)
    new_genre.add_book(self)
end