Class: RelatonBib::TypedTitleString

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

Constant Summary collapse

TITLE_TYPES =
%w[alternative original unofficial subtitle main].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**args) ⇒ TypedTitleString

Returns a new instance of TypedTitleString.

Parameters:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/relaton_bib/typed_title_string.rb', line 18

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

  unless args[:title] || args[:content]
    raise ArgumentError, %{Keyword "title" or "content" should be passed.}
  end

  @type = args[:type]

  if args[:title]
    @title = args[:title]
  else
    fsargs = args.select do |k, _v|
      %i[content language script format].include? k 
    end
    @title = FormattedString.new(fsargs)
  end
end

Instance Attribute Details

#titleRelatonBib::FormattedString (readonly)



9
10
11
# File 'lib/relaton_bib/typed_title_string.rb', line 9

def title
  @title
end

#typeString (readonly)

Returns:

  • (String)


6
7
8
# File 'lib/relaton_bib/typed_title_string.rb', line 6

def type
  @type
end

Instance Method Details

#to_hashHash

Returns:

  • (Hash)


47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/relaton_bib/typed_title_string.rb', line 47

def to_hash
  th = title.to_hash
  return th unless type

  hash = { "type" => type }
  if th.is_a? String
    hash["content"] = th
  else
    hash.merge! th
  end
  hash
end

#to_xml(builder) ⇒ Object

Parameters:

  • builder (Nokogiri::XML::Builder)


41
42
43
44
# File 'lib/relaton_bib/typed_title_string.rb', line 41

def to_xml(builder)
  builder.parent[:type] = type if type
  title.to_xml builder
end