Class: Relaton::Render::Template::Name

Inherits:
General
  • Object
show all
Defined in:
lib/relaton/render/template/template.rb

Direct Known Subclasses

AuthorCite

Constant Summary

Constants inherited from General

General::FIELD_DELIM, General::GT_DELIM, General::LT_DELIM, General::NON_SPACING_DELIM

Instance Attribute Summary

Attributes inherited from General

#template_raw

Instance Method Summary collapse

Methods inherited from General

#add_field_delim_to_template, #customise_liquid, #liquid_hash, #parse_options, #punct_field?, #render, #template_clean, #template_clean1, #template_process

Constructor Details

#initialize(opt = {}) ⇒ Name

Returns a new instance of Name.



170
171
172
173
174
175
176
# File 'lib/relaton/render/template/template.rb', line 170

def initialize(opt = {})
  @etal_count = opt[:template]["etal_count"]
  @etal_display = opt[:template]["etal_display"] || @etal_count
  opt[:template].delete("etal_count")
  opt[:template].delete("etal_display")
  super
end

Instance Method Details

#combine_nametemplate(parts, size) ⇒ Object



210
211
212
213
214
215
216
# File 'lib/relaton/render/template/template.rb', line 210

def combine_nametemplate(parts, size)
  case size
  when 1 then parts[0] + parts[3]
  when 2 then parts[0] + parts[2] + parts[3]
  else parts.join
  end
end

#expand_nametemplate(template, size) ⇒ Object

assumes that template contains, consecutively and not interleaved, …[0], …[1], …[2]



199
200
201
202
203
204
205
206
207
208
# File 'lib/relaton/render/template/template.rb', line 199

def expand_nametemplate(template, size)
  t = nametemplate_split(template)

  mid = (1..size - 2).each_with_object([]) do |i, m|
    m << t[1].gsub("[1]", "[#{i}]")
  end
  t[1] = mid.join
  t[2].gsub!(/\[\d+\]/, "[#{size - 1}]")
  template_process(combine_nametemplate(t, size))
end

#nametemplate_split(template) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
# File 'lib/relaton/render/template/template.rb', line 218

def nametemplate_split(template)
  curr = 0
  prec = ""
  t = template.split(/(\{[{%].+?[}%]\})/)
    .each_with_object([""]) do |n, m|
    m, curr, prec = nametemplate_split1(n, m, curr, prec)

    m
  end
  [t[0], t[1], t[-1], prec]
end

#nametemplate_split1(elem, acc, curr, prec) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/relaton/render/template/template.rb', line 230

def nametemplate_split1(elem, acc, curr, prec)
  if match = /\{[{%].+?\[(\d)\]/.match(elem)
    if match[1].to_i > curr
      curr += 1
      acc[curr] ||= ""
    end
    acc[curr] += prec
    prec = ""
    acc[curr] += elem
  elsif /\{%\s*endif/.match?(elem)
    acc[curr] += prec
    prec = ""
    acc[curr] += elem
  else prec += elem
  end
  [acc, curr, prec]
end

#template_select(names) ⇒ Object



178
179
180
181
182
183
184
185
186
187
# File 'lib/relaton/render/template/template.rb', line 178

def template_select(names)
  return nil if names.nil? || names.empty?

  case names[:surname].size
  when 1 then @template[:one]
  when 2 then @template[:two]
  when 3 then @template[:more]
  else template_select_etal(names)
  end
end

#template_select_etal(names) ⇒ Object



189
190
191
192
193
194
195
# File 'lib/relaton/render/template/template.rb', line 189

def template_select_etal(names)
  if @etal_count && names[:surname].size >= @etal_count
    expand_nametemplate(@template_raw[:etal], @etal_display)
  else
    expand_nametemplate(@template_raw[:more], names[:surname].size)
  end
end