Class: OpenStax::Cnx::V1::BookPart
- Inherits:
-
Object
- Object
- OpenStax::Cnx::V1::BookPart
- Defined in:
- lib/openstax/cnx/v1/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
- #baked_book_location ⇒ Object
- #contents ⇒ Object
-
#initialize(hash: {}, is_root: false, book: nil) ⇒ BookPart
constructor
A new instance of BookPart.
- #is_chapter? ⇒ Boolean
- #parsed_title ⇒ Object
- #parts ⇒ Object
- #title ⇒ Object
Constructor Details
#initialize(hash: {}, is_root: false, book: nil) ⇒ BookPart
Returns a new instance of BookPart.
4 5 6 7 8 |
# File 'lib/openstax/cnx/v1/book_part.rb', line 4 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.
10 11 12 |
# File 'lib/openstax/cnx/v1/book_part.rb', line 10 def book @book end |
#hash ⇒ Object (readonly)
Returns the value of attribute hash.
10 11 12 |
# File 'lib/openstax/cnx/v1/book_part.rb', line 10 def hash @hash end |
#is_root ⇒ Object (readonly)
Returns the value of attribute is_root.
10 11 12 |
# File 'lib/openstax/cnx/v1/book_part.rb', line 10 def is_root @is_root end |
Instance Method Details
#baked_book_location ⇒ Object
18 19 20 |
# File 'lib/openstax/cnx/v1/book_part.rb', line 18 def baked_book_location @baked_book_location ||= parsed_title[:book_location] end |
#contents ⇒ Object
26 27 28 29 30 |
# File 'lib/openstax/cnx/v1/book_part.rb', line 26 def contents @contents ||= hash.fetch('contents') do |key| raise "#{self.class.name} id=#{@id} is missing #{key}" end end |
#is_chapter? ⇒ Boolean
42 43 44 45 |
# File 'lib/openstax/cnx/v1/book_part.rb', line 42 def is_chapter? # A BookPart is a chapter if none of its children are BookParts @is_chapter ||= parts.none? { |part| part.is_a?(self.class) } end |
#parsed_title ⇒ Object
12 13 14 15 16 |
# File 'lib/openstax/cnx/v1/book_part.rb', line 12 def parsed_title @parsed_title ||= OpenStax::Cnx::V1::Baked.parse_title( hash.fetch('title') { |key| raise "#{self.class.name} id=#{@id} is missing #{key}" } ) end |
#parts ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/openstax/cnx/v1/book_part.rb', line 32 def parts @parts ||= contents.map do |hash| if hash.has_key? 'contents' self.class.new(hash: hash, book: book) else OpenStax::Cnx::V1::Page.new(hash: hash, book: book) end end end |
#title ⇒ Object
22 23 24 |
# File 'lib/openstax/cnx/v1/book_part.rb', line 22 def title @title ||= parsed_title[:text] end |