Class: OpenStax::Content::BookPart

Inherits:
Object
  • Object
show all
Defined in:
lib/openstax/content/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.



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

#bookObject (readonly)

Returns the value of attribute book.



11
12
13
# File 'lib/openstax/content/book_part.rb', line 11

def book
  @book
end

#hashObject (readonly)

Returns the value of attribute hash.



11
12
13
# File 'lib/openstax/content/book_part.rb', line 11

def hash
  @hash
end

#is_rootObject (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_pagesObject



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_locationObject



17
18
19
# File 'lib/openstax/content/book_part.rb', line 17

def book_location
  @book_location ||= parsed_title.book_location
end

#contentsObject



34
35
36
# File 'lib/openstax/content/book_part.rb', line 34

def contents
  @contents ||= hash.fetch('contents')
end

#parsed_titleObject



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

#partsObject



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

#titleObject



21
22
23
# File 'lib/openstax/content/book_part.rb', line 21

def title
  @title ||= parsed_title.text
end

#uuidObject

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