Class: OpenStax::Cnx::V1::BookPart

Inherits:
Object
  • Object
show all
Defined in:
lib/openstax/cnx/v1/book_part.rb

Constant Summary collapse

CONTENTS =
'contents'

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#bookObject (readonly)

Returns the value of attribute book.



6
7
8
# File 'lib/openstax/cnx/v1/book_part.rb', line 6

def book
  @book
end

#hashObject (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_rootObject (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_locationObject



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

#contentsObject



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

Returns:

  • (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

#pagesObject



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_titleObject



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

#partsObject



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

#titleObject



24
25
26
# File 'lib/openstax/cnx/v1/book_part.rb', line 24

def title
  @title ||= parsed_title[:text]
end