Class: RelatonBib::TypedTitleString

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

Constant Summary collapse

ARGS =
%i[content language script format].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**args) ⇒ TypedTitleString

Returns a new instance of TypedTitleString.

Parameters:



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/relaton_bib/typed_title_string.rb', line 105

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]

  case args[:title]
  when FormattedString then @title = args[:title]
  when Hash then @title = FormattedString.new(**args[:title])
  else
    fsargs = args.select { |k, _v| ARGS.include? k }
    @title = FormattedString.new(**fsargs)
  end
end

Instance Attribute Details

#titleRelatonBib::FormattedString (readonly)



97
98
99
# File 'lib/relaton_bib/typed_title_string.rb', line 97

def title
  @title
end

#typeString (readonly)

Returns:

  • (String)


94
95
96
# File 'lib/relaton_bib/typed_title_string.rb', line 94

def type
  @type
end

Class Method Details

.from_string(title, lang = nil, script = nil, format = "text/plain") ⇒ TypedTitleStringCollection

Create TypedTitleStringCollection from string

Parameters:

  • title (String)

    title string

  • lang (String, nil) (defaults to: nil)

    language code Iso639

  • script (String, nil) (defaults to: nil)

    script code Iso15924

  • format (String) (defaults to: "text/plain")

    format text/html, text/plain

Returns:



131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/relaton_bib/typed_title_string.rb', line 131

def self.from_string(title, lang = nil, script = nil, format = "text/plain")
  types = %w[title-intro title-main title-part]
  ttls = split_title(title)
  tts = ttls.map.with_index do |p, i|
    next unless p

    new type: types[i], content: p, language: lang, script: script, format: format
  end.compact
  tts << new(type: "main", content: ttls.compact.join(" - "),
             language: lang, script: script, format: format)
  TypedTitleStringCollection.new tts
end

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

Parameters:

Returns:



156
157
158
159
160
161
162
163
164
# File 'lib/relaton_bib/typed_title_string.rb', line 156

def self.intro_or_part(ttls)
  if /^(Part|Partie) \d+:/.match? ttls[1]
    [nil, ttls[0], ttls[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>

Parameters:

  • title (String)

Returns:



146
147
148
149
150
151
152
# File 'lib/relaton_bib/typed_title_string.rb', line 146

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

Parameters:

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

    number of titles

Returns:

  • (String)


183
184
185
186
187
188
189
# File 'lib/relaton_bib/typed_title_string.rb', line 183

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", 1, !(type.nil? || type.empty?)
  out
end

#to_hashHash

Returns:

  • (Hash)


173
174
175
176
177
178
# File 'lib/relaton_bib/typed_title_string.rb', line 173

def to_hash
  th = title.to_hash
  return th unless type

  th.merge "type" => type
end

#to_xml(builder) ⇒ Object

Parameters:

  • builder (Nokogiri::XML::Builder)


167
168
169
170
# File 'lib/relaton_bib/typed_title_string.rb', line 167

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