Class: FB2rb::TitleInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/fb2rb.rb

Overview

Holds <title-info>/<src-title-info> data

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(genres: [], authors: [], book_title: '', annotation: nil, keywords: [], date: nil, coverpage: nil, lang: 'en', src_lang: nil, translators: [], sequences: []) ⇒ TitleInfo

rubocop:disable Metrics/MethodLength, Metrics/ParameterLists



455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
# File 'lib/fb2rb.rb', line 455

def initialize(genres: [], # rubocop:disable Metrics/MethodLength, Metrics/ParameterLists
               authors: [],
               book_title: '',
               annotation: nil,
               keywords: [],
               date: nil,
               coverpage: nil,
               lang: 'en',
               src_lang: nil,
               translators: [],
               sequences: [])
  @genres = genres
  @authors = authors
  @book_title = book_title
  @annotation = annotation
  @keywords = keywords
  @date = date
  @coverpage = coverpage
  @lang = lang
  @src_lang = src_lang
  @translators = translators
  @sequences = sequences
end

Instance Attribute Details

#annotationString?

Returns:

  • (String, nil)


439
440
441
# File 'lib/fb2rb.rb', line 439

def annotation
  @annotation
end

#authorsArray<FB2rb::Author>

Returns:



435
436
437
# File 'lib/fb2rb.rb', line 435

def authors
  @authors
end

#book_titleString

Returns:

  • (String)


437
438
439
# File 'lib/fb2rb.rb', line 437

def book_title
  @book_title
end

#coverpageFB2rb::Coverpage?

Returns:



445
446
447
# File 'lib/fb2rb.rb', line 445

def coverpage
  @coverpage
end

#dateFB2rb::Date?

Returns:

  • (FB2rb::Date, nil)


443
444
445
# File 'lib/fb2rb.rb', line 443

def date
  @date
end

#genresArray<String>

Returns:

  • (Array<String>)


433
434
435
# File 'lib/fb2rb.rb', line 433

def genres
  @genres
end

#keywordsArray<String>

Returns:

  • (Array<String>)


441
442
443
# File 'lib/fb2rb.rb', line 441

def keywords
  @keywords
end

#langString

Returns:

  • (String)


447
448
449
# File 'lib/fb2rb.rb', line 447

def lang
  @lang
end

#sequencesArray<FB2rb::Sequence>

Returns:



453
454
455
# File 'lib/fb2rb.rb', line 453

def sequences
  @sequences
end

#src_langString?

Returns:

  • (String, nil)


449
450
451
# File 'lib/fb2rb.rb', line 449

def src_lang
  @src_lang
end

#translatorsArray<FB2rb::Author>

Returns:



451
452
453
# File 'lib/fb2rb.rb', line 451

def translators
  @translators
end

Class Method Details

.parse(xml, fb2_prefix, xlink_prefix) ⇒ FB2rb::TitleInfo

Returns:



480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
# File 'lib/fb2rb.rb', line 480

def self.parse(xml, fb2_prefix, xlink_prefix) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
  date = xml.at("./#{fb2_prefix}:date")
  coverpage = xml.at("./#{fb2_prefix}:coverpage")
  TitleInfo.new(
    genres: xml.xpath("./#{fb2_prefix}:genre/text()").map(&:text),
    authors: xml.xpath("./#{fb2_prefix}:author").map do |node|
      Author.parse(node, fb2_prefix)
    end,
    book_title: xml.at("./#{fb2_prefix}:book-title/text()")&.text,
    annotation: xml.at("./#{fb2_prefix}:annotation")&.children.to_s.strip,
    keywords: xml.at("./#{fb2_prefix}:keywords/text()")&.text&.split(', ') || [],
    date: date.nil? ? nil : FB2Date.parse(date),
    coverpage: coverpage.nil? ? nil : Coverpage.parse(coverpage, fb2_prefix, xlink_prefix),
    lang: xml.at("./#{fb2_prefix}:lang/text()").text,
    src_lang: xml.at("./#{fb2_prefix}:src-lang/text()")&.text,
    translators: xml.xpath("./#{fb2_prefix}:translator").map do |node|
      Author.parse(node, fb2_prefix)
    end,
    sequences: xml.xpath("./#{fb2_prefix}:sequence").map do |node|
      Sequence.parse(node)
    end
  )
end

Instance Method Details

#to_xml(xml, tag) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity



504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
# File 'lib/fb2rb.rb', line 504

def to_xml(xml, tag) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
  xml.send(tag) do
    genres.each do |genre|
      xml.genre(genre)
    end
    authors.each do |author|
      author.to_xml(xml, 'author')
    end
    xml.send('book-title', @book_title)
    unless @annotation.nil?
      xml.annotation do
        xml << @annotation
      end
    end
    xml.keywords(@keywords.join(', ')) unless keywords.nil?
    @date.to_xml(xml) unless date.nil?
    @coverpage.to_xml(xml) unless coverpage.nil?
    xml.lang(@lang)
    xml.send('src-lang') unless src_lang.nil?
    @translators.each do |translator|
      translator.to_xml(xml, 'translator')
    end
    @sequences.each do |sequence|
      sequence.to_xml(xml)
    end
  end
end