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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#bookObject (readonly)

Returns the value of attribute book.



10
11
12
# File 'lib/openstax/cnx/v1/book_part.rb', line 10

def book
  @book
end

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



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

#contentsObject



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

Returns:

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



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

#partsObject



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

#titleObject



22
23
24
# File 'lib/openstax/cnx/v1/book_part.rb', line 22

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