Class: RelatonBib::TypedTitleString

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**args) ⇒ TypedTitleString



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/relaton_bib/typed_title_string.rb', line 14

def initialize(**args) # rubocop:disable Metrics/MethodLength
  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)



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

def title
  @title
end

#typeString (readonly)



4
5
6
# File 'lib/relaton_bib/typed_title_string.rb', line 4

def type
  @type
end

Class Method Details

.from_string(title, lang = nil, script = nil) ⇒ Array<self>



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

def self.from_string(title, lang = nil, script = nil)
  types = %w[title-intro title-main title-part]
  ttls = split_title(title)
  tts = ttls.map.with_index do |p, i|
    new type: types[i], content: p, language: lang, script: script if p
  end.compact
  tts << new(type: "main", content: ttls.compact.join(" - "),
             language: lang, script: script)
end

.intro_or_part(ttls) ⇒ Array<Strin, nil>



55
56
57
58
59
60
61
62
63
# File 'lib/relaton_bib/typed_title_string.rb', line 55

def self.intro_or_part(ttls)
  if /^(Part|Partie) \d+:/.match? ttls[1]
    [nil, ttls[0], ttls[1..-1].join(" -- ")]
  else
    parts = ttls.slice(2..-1)
    part = parts.join " -- " if parts.any?
    [ttls[0], ttls[1], part]
  end
end

.split_title(title) ⇒ Array<String, nil>



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

def self.split_title(title)
  ttls = title.sub(/\w\.Imp\s?\d+\u00A0:\u00A0/, "").split " - "
  case ttls.size
  when 0, 1 then [nil, ttls.first.to_s, nil]
  else intro_or_part ttls
  end
end

Instance Method Details

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



88
89
90
91
92
93
94
# File 'lib/relaton_bib/typed_title_string.rb', line 88

def to_asciibib(prefix = "", count = 1) # rubocop:disable Metrics/AbcSize
  pref = prefix.empty? ? prefix : prefix + "."
  out = count > 1 ? "#{pref}title::\n" : ""
  out += "#{pref}title.type:: #{type}\n" if type
  out += title.to_asciibib "#{pref}title"
  out
end

#to_hashHash



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/relaton_bib/typed_title_string.rb', line 72

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



66
67
68
69
# File 'lib/relaton_bib/typed_title_string.rb', line 66

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