Class: RelatonBib::ContributorRole

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

Overview

Contributor’s role.

Constant Summary collapse

TYPES =
%w[author performer publisher editor adapter translator
distributor].freeze

Constants included from RelatonBib

VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RelatonBib

parse_date

Constructor Details

#initialize(**args) ⇒ ContributorRole

Returns a new instance of ContributorRole.

Parameters:

  • type (String)

    allowed types “author”, “editor”, “cartographer”, “publisher”

  • description (Array<String>)


23
24
25
26
27
28
29
30
31
32
# File 'lib/relaton_bib/contribution_info.rb', line 23

def initialize(**args)
  if args[:type] && !TYPES.include?(args[:type])
    warn %{[relaton-bib] Contributor's type "#{args[:type]}" is invalid.}
  end

  @type = args[:type]
  @description = args.fetch(:description, []).map do |d|
    FormattedString.new content: d, format: nil
  end
end

Instance Attribute Details

#descriptionArray<RelatonBib::FormattedString> (readonly)



15
16
17
# File 'lib/relaton_bib/contribution_info.rb', line 15

def description
  @description
end

#typeStrig (readonly)

Returns:

  • (Strig)


18
19
20
# File 'lib/relaton_bib/contribution_info.rb', line 18

def type
  @type
end

Instance Method Details

#to_asciibib(prefix = "", count = 1) ⇒ Object

2return [String]

Parameters:

  • prefix (String) (defaults to: "")
  • count (Integer) (defaults to: 1)

    number of contributors



57
58
59
60
61
62
63
64
65
# File 'lib/relaton_bib/contribution_info.rb', line 57

def to_asciibib(prefix = "", count = 1)
  pref = prefix.empty? ? prefix : prefix + "."
  out = count > 1 ? "#{prefix}::\n" : ""
  description.each do |d|
    out += d.to_asciibib "#{pref}role.description", description.size
  end
  out += "#{pref}role.type:: #{type}\n" if type
  out
end

#to_hashHash, String

Returns:

  • (Hash, String)


44
45
46
47
48
49
50
51
52
# File 'lib/relaton_bib/contribution_info.rb', line 44

def to_hash
  if description&.any?
    hash = { "description" => single_element_array(description) }
    hash["type"] = type if type
    hash
  elsif type
    type
  end
end

#to_xml(builder) ⇒ Object

Parameters:

  • builder (Nokogiri::XML::Builder)


35
36
37
38
39
40
41
# File 'lib/relaton_bib/contribution_info.rb', line 35

def to_xml(builder)
  builder.role(type: type) do
    description.each do |d|
      builder.description { |desc| d.to_xml(desc) }
    end
  end
end