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.



137
138
139
140
141
142
143
# File 'lib/relaton/render/template/template.rb', line 137

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



177
178
179
180
181
182
183
# File 'lib/relaton/render/template/template.rb', line 177

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]



166
167
168
169
170
171
172
173
174
175
# File 'lib/relaton/render/template/template.rb', line 166

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



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

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



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/relaton/render/template/template.rb', line 197

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



145
146
147
148
149
150
151
152
153
154
# File 'lib/relaton/render/template/template.rb', line 145

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



156
157
158
159
160
161
162
# File 'lib/relaton/render/template/template.rb', line 156

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