Class: OpenStax::Content::BookPart
- Inherits:
-
Object
- Object
- OpenStax::Content::BookPart
- Defined in:
- lib/openstax/content/book_part.rb
Instance Attribute Summary collapse
-
#book ⇒ Object
readonly
Returns the value of attribute book.
-
#hash ⇒ Object
readonly
Returns the value of attribute hash.
-
#is_root ⇒ Object
readonly
Returns the value of attribute is_root.
Instance Method Summary collapse
- #all_pages ⇒ Object
- #book_location ⇒ Object
- #contents ⇒ Object
-
#initialize(hash: {}, is_root: false, book: nil) ⇒ BookPart
constructor
A new instance of BookPart.
- #parsed_title ⇒ Object
- #parts ⇒ Object
- #title ⇒ Object
-
#uuid ⇒ Object
Old content used to have id == “subcol” for units and chapters If we encounter that, just assign a random UUID to them.
Constructor Details
#initialize(hash: {}, is_root: false, book: nil) ⇒ BookPart
Returns a new instance of BookPart.
5 6 7 8 9 |
# File 'lib/openstax/content/book_part.rb', line 5 def initialize(hash: {}, is_root: false, book: nil) @hash = hash @is_root = is_root @book = book end |
Instance Attribute Details
#book ⇒ Object (readonly)
Returns the value of attribute book.
11 12 13 |
# File 'lib/openstax/content/book_part.rb', line 11 def book @book end |
#hash ⇒ Object (readonly)
Returns the value of attribute hash.
11 12 13 |
# File 'lib/openstax/content/book_part.rb', line 11 def hash @hash end |
#is_root ⇒ Object (readonly)
Returns the value of attribute is_root.
11 12 13 |
# File 'lib/openstax/content/book_part.rb', line 11 def is_root @is_root end |
Instance Method Details
#all_pages ⇒ Object
48 49 50 51 52 |
# File 'lib/openstax/content/book_part.rb', line 48 def all_pages @all_pages ||= parts.flat_map do |part| part.is_a?(OpenStax::Content::Page) ? [ part ] : part.all_pages end end |
#book_location ⇒ Object
17 18 19 |
# File 'lib/openstax/content/book_part.rb', line 17 def book_location @book_location ||= parsed_title.book_location end |
#contents ⇒ Object
34 35 36 |
# File 'lib/openstax/content/book_part.rb', line 34 def contents @contents ||= hash.fetch('contents') end |
#parsed_title ⇒ Object
13 14 15 |
# File 'lib/openstax/content/book_part.rb', line 13 def parsed_title @parsed_title ||= OpenStax::Content::Title.new hash.fetch('title') end |
#parts ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/openstax/content/book_part.rb', line 38 def parts @parts ||= contents.map do |hash| if hash.has_key? 'contents' self.class.new book: book, hash: hash else OpenStax::Content::Page.new book: book, hash: hash end end end |
#title ⇒ Object
21 22 23 |
# File 'lib/openstax/content/book_part.rb', line 21 def title @title ||= parsed_title.text end |
#uuid ⇒ Object
Old content used to have id == “subcol” for units and chapters If we encounter that, just assign a random UUID to them
27 28 29 30 31 32 |
# File 'lib/openstax/content/book_part.rb', line 27 def uuid @uuid ||= begin uuid = hash['id'] uuid.nil? || uuid == 'subcol' ? SecureRandom.uuid : uuid.split('@').first end end |