Class: OpenStax::Cnx::V1::BookPart
- Inherits:
-
Object
- Object
- OpenStax::Cnx::V1::BookPart
- Defined in:
- lib/openstax/cnx/v1/book_part.rb
Constant Summary collapse
- CONTENTS =
'contents'
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
- #pages ⇒ Object
- #parsed_title ⇒ Object
- #parts ⇒ Object
- #title ⇒ Object
Constructor Details
#initialize(hash: {}, is_root: false, book: nil) ⇒ BookPart
Returns a new instance of BookPart.
8 9 10 11 12 |
# File 'lib/openstax/cnx/v1/book_part.rb', line 8 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.
6 7 8 |
# File 'lib/openstax/cnx/v1/book_part.rb', line 6 def book @book end |
#hash ⇒ Object (readonly)
Returns the value of attribute hash.
6 7 8 |
# File 'lib/openstax/cnx/v1/book_part.rb', line 6 def hash @hash end |
#is_root ⇒ Object (readonly)
Returns the value of attribute is_root.
6 7 8 |
# File 'lib/openstax/cnx/v1/book_part.rb', line 6 def is_root @is_root end |
Instance Method Details
#baked_book_location ⇒ Object
20 21 22 |
# File 'lib/openstax/cnx/v1/book_part.rb', line 20 def baked_book_location @baked_book_location ||= parsed_title[:book_location] end |
#contents ⇒ Object
28 29 30 31 32 |
# File 'lib/openstax/cnx/v1/book_part.rb', line 28 def contents @contents ||= hash.fetch(CONTENTS) do |key| raise "#{self.class.name} id=#{@id} is missing #{key}" end end |
#is_chapter? ⇒ Boolean
44 45 46 47 |
# File 'lib/openstax/cnx/v1/book_part.rb', line 44 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 |
#pages ⇒ Object
49 50 51 52 53 |
# File 'lib/openstax/cnx/v1/book_part.rb', line 49 def pages @pages ||= parts.map do |part| part.is_a?(Page) ? part : part.pages end.flatten end |
#parsed_title ⇒ Object
14 15 16 17 18 |
# File 'lib/openstax/cnx/v1/book_part.rb', line 14 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
34 35 36 37 38 39 40 41 42 |
# File 'lib/openstax/cnx/v1/book_part.rb', line 34 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
24 25 26 |
# File 'lib/openstax/cnx/v1/book_part.rb', line 24 def title @title ||= parsed_title[:text] end |