Class: FB2rb::DocumentInfo

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

Overview

Holds <document-info> data

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(authors: [], program_used: nil, date: FB2Date.new, src_urls: [], src_ocr: nil, id: '', version: '', history: nil, publishers: []) ⇒ DocumentInfo

rubocop:disable Metrics/ParameterLists



367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# File 'lib/fb2rb.rb', line 367

def initialize(authors: [], # rubocop:disable Metrics/ParameterLists
               program_used: nil,
               date: FB2Date.new,
               src_urls: [],
               src_ocr: nil,
               id: '',
               version: '',
               history: nil,
               publishers: [])
  @authors = authors
  @program_used = program_used
  @date = date
  @src_urls = src_urls
  @src_ocr = src_ocr
  @id = id
  @version = version
  @history = history
  @publishers = publishers
end

Instance Attribute Details

#authorsArray<FB2rb::Author>

Returns:



349
350
351
# File 'lib/fb2rb.rb', line 349

def authors
  @authors
end

#dateFB2rb::FB2Date

Returns:



353
354
355
# File 'lib/fb2rb.rb', line 353

def date
  @date
end

#historyString?

Returns:

  • (String, nil)


363
364
365
# File 'lib/fb2rb.rb', line 363

def history
  @history
end

#idString

Returns:

  • (String)


359
360
361
# File 'lib/fb2rb.rb', line 359

def id
  @id
end

#program_usedString?

Returns:

  • (String, nil)


351
352
353
# File 'lib/fb2rb.rb', line 351

def program_used
  @program_used
end

#publishersArray<String>

Returns:

  • (Array<String>)


365
366
367
# File 'lib/fb2rb.rb', line 365

def publishers
  @publishers
end

#src_ocrString?

Returns:

  • (String, nil)


357
358
359
# File 'lib/fb2rb.rb', line 357

def src_ocr
  @src_ocr
end

#src_urlsArray<String>

Returns:

  • (Array<String>)


355
356
357
# File 'lib/fb2rb.rb', line 355

def src_urls
  @src_urls
end

#versionString

Returns:

  • (String)


361
362
363
# File 'lib/fb2rb.rb', line 361

def version
  @version
end

Class Method Details

.parse(xml, fb2_prefix) ⇒ FB2rb::DocumentInfo

Returns:



388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# File 'lib/fb2rb.rb', line 388

def self.parse(xml, fb2_prefix) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
  date = xml.at("./#{fb2_prefix}:date")
  DocumentInfo.new(
    authors: xml.xpath("./#{fb2_prefix}:author").map do |node|
      Author.parse(node, fb2_prefix)
    end,
    program_used: xml.at("./#{fb2_prefix}:program-used")&.text,
    date: date.nil? ? FB2Date.new : FB2Date.parse(date),
    src_urls: xml.xpath("./#{fb2_prefix}:src-url").map(&:text),
    src_ocr: xml.at("./#{fb2_prefix}:src-ocr")&.text,
    id: xml.at("./#{fb2_prefix}:id").text,
    version: xml.at("./#{fb2_prefix}:version")&.text,
    history: xml.at("./#{fb2_prefix}:history")&.children&.to_s&.strip,
    publishers: xml.xpath("./#{fb2_prefix}:publisher").map(&:text)
  )
end

Instance Method Details

#to_xml(xml) ⇒ Object

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



405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
# File 'lib/fb2rb.rb', line 405

def to_xml(xml) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
  xml.send(:'document-info') do
    @authors.each do |author|
      author.to_xml(xml, 'author')
    end
    xml.send('program-used', @program_used) unless @program_used.nil?
    @date.to_xml(xml)
    @src_urls.each do |src_url|
      xml.send('src-url', src_url)
    end
    xml.send('src-ocr', @src_ocr) unless @src_ocr.nil?
    xml.id(@id)
    xml.version(@version) unless @version.nil?
    unless @history.nil?
      xml.history do
        xml << @history
      end
    end
    @publishers.each do |publisher|
      xml.publisher(publisher)
    end
  end
end