Class: RelatonBib::FullName

Inherits:
Object show all
Includes:
RelatonBib
Defined in:
lib/relaton_bib/full_name.rb

Overview

Person’s full name

Constant Summary

Constants included from RelatonBib

VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RelatonBib

array, format_date, grammar_hash, parse_date, parse_yaml

Methods included from Config

#configuration, #configure

Constructor Details

#initialize(**args) ⇒ FullName

Initialize FullName instance

Parameters:



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/relaton_bib/full_name.rb', line 26

def initialize(**args) # rubocop:disable Metrics/AbcSize
  unless args[:surname] || args[:completename]
    raise ArgumentError, "Should be given :surname or :completename"
  end

  @surname      = args[:surname]
  @abbreviation = args[:abbreviation]
  @forename     = args.fetch :forename, []
  @initials     = args[:initials].is_a?(String) ? LocalizedString.new(args[:initials]) : args[:initials]
  @addition     = args.fetch :addition, []
  @prefix       = args.fetch :prefix, []
  @completename = args[:completename]
end

Instance Attribute Details

#abbreviationRelatonBib::LocalizedString?

Returns:



13
14
15
# File 'lib/relaton_bib/full_name.rb', line 13

def abbreviation
  @abbreviation
end

#additionArray<RelatonBib::LocalizedString>



10
11
12
# File 'lib/relaton_bib/full_name.rb', line 10

def addition
  @addition
end

#completenameRelatonBib::LocalizedString?

Returns:



13
14
15
# File 'lib/relaton_bib/full_name.rb', line 13

def completename
  @completename
end

#forenameArray<RelatonBib::Forename>



7
8
9
# File 'lib/relaton_bib/full_name.rb', line 7

def forename
  @forename
end

#initialsArray<RelatonBib::LocalizedString>



10
11
12
# File 'lib/relaton_bib/full_name.rb', line 10

def initials
  @initials
end

#prefixArray<RelatonBib::LocalizedString>



10
11
12
# File 'lib/relaton_bib/full_name.rb', line 10

def prefix
  @prefix
end

#surnameRelatonBib::LocalizedString?

Returns:



13
14
15
# File 'lib/relaton_bib/full_name.rb', line 13

def surname
  @surname
end

Instance Method Details

#to_asciibib(pref) ⇒ String

Parameters:

  • pref (String)

Returns:

  • (String)


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/relaton_bib/full_name.rb', line 82

def to_asciibib(pref) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
  prf = pref.empty? ? pref : "#{pref}."
  prf += "name"
  out = ""
  out += abbreviation.to_asciibib "#{prf}.abbreviation" if abbreviation
  given = "#{pref}.given"
  out += forename.map do |fn|
    fn.to_asciibib given, forename.size
  end.join
  out += initials.to_asciibib "#{given}.formatted-initials" if initials
  out += surname.to_asciibib "#{prf}.surname" if surname
  addition.each do |ad|
    out += ad.to_asciibib "#{prf}.addition", addition.size
  end
  prefix.each { |pr| out += pr.to_asciibib "#{prf}.prefix", prefix.size }
  out += completename.to_asciibib "#{prf}.completename" if completename
  out
end

#to_hashHash

Returns:

  • (Hash)


65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/relaton_bib/full_name.rb', line 65

def to_hash # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity,Metrics/MethodLength
  hash = {}
  hash["abbreviation"] = abbreviation.to_hash if abbreviation
  if forename.any? || initials
    hash["given"] = {}
    hash["given"]["forename"] = single_element_array(forename) if forename&.any?
    hash["given"]["formatted_initials"] = initials.to_hash if initials
  end
  hash["surname"] = surname.to_hash if surname
  hash["addition"] = single_element_array(addition) if addition&.any?
  hash["prefix"] = single_element_array(prefix) if prefix&.any?
  hash["completename"] = completename.to_hash if completename
  hash
end

#to_xml(**opts) ⇒ Object

Parameters:

  • opts (Hash)

Options Hash (**opts):

  • :builder (Nokogiri::XML::Builder)

    XML builder

  • :lang (String)

    language



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/relaton_bib/full_name.rb', line 43

def to_xml(**opts) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
  opts[:builder].name do |builder|
    builder.abbreviation { abbreviation.to_xml builder } if abbreviation
    if completename
      builder.completename { completename.to_xml builder }
    else
      pref = prefix.select { |p| p.language&.include? opts[:lang] }
      pref = prefix unless pref.any?
      pref.each { |p| builder.prefix { p.to_xml builder } }
      frnm = forename.select { |f| f.language&.include? opts[:lang] }
      frnm = forename unless frnm.any?
      frnm.each { |f| f.to_xml builder }
      builder.send(:"formatted-initials") { initials.to_xml builder } if initials
      builder.surname { surname.to_xml builder }
      addn = addition.select { |a| a.language&.include? opts[:lang] }
      addn = addition unless addn.any?
      addn.each { |a| builder.addition { a.to_xml builder } }
    end
  end
end