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



349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'lib/fb2rb.rb', line 349

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:



331
332
333
# File 'lib/fb2rb.rb', line 331

def authors
  @authors
end

#dateFB2rb::FB2Date

Returns:



335
336
337
# File 'lib/fb2rb.rb', line 335

def date
  @date
end

#historyString?

Returns:

  • (String, nil)


345
346
347
# File 'lib/fb2rb.rb', line 345

def history
  @history
end

#idString

Returns:

  • (String)


341
342
343
# File 'lib/fb2rb.rb', line 341

def id
  @id
end

#program_usedString?

Returns:

  • (String, nil)


333
334
335
# File 'lib/fb2rb.rb', line 333

def program_used
  @program_used
end

#publishersArray<String>

Returns:

  • (Array<String>)


347
348
349
# File 'lib/fb2rb.rb', line 347

def publishers
  @publishers
end

#src_ocrString?

Returns:

  • (String, nil)


339
340
341
# File 'lib/fb2rb.rb', line 339

def src_ocr
  @src_ocr
end

#src_urlsArray<String>

Returns:

  • (Array<String>)


337
338
339
# File 'lib/fb2rb.rb', line 337

def src_urls
  @src_urls
end

#versionString

Returns:

  • (String)


343
344
345
# File 'lib/fb2rb.rb', line 343

def version
  @version
end

Class Method Details

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

Returns:



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

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

Instance Method Details

#to_xml(xml) ⇒ Object

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



387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# File 'lib/fb2rb.rb', line 387

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