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 Method Summary collapse

Methods inherited from General

#customise_liquid, #liquid_hash, #parse_options, #render, #template_clean, #template_clean1, #template_process

Constructor Details

#initialize(opt = {}) ⇒ Name

Returns a new instance of Name.



120
121
122
123
124
# File 'lib/relaton/render/template/template.rb', line 120

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

Instance Method Details

#expand_nametemplate(template, size) ⇒ Object

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



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

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
  template_process(t[0] + mid.join + t[2].gsub(/\[2\]/,
                                               "[#{size - 1}]"))
end

#nametemplate_split(template) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/relaton/render/template/template.rb', line 155

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[-1] += prec
  t
end

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



168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/relaton/render/template/template.rb', line 168

def nametemplate_split1(elem, acc, curr, prec)
  if match = /\{[{%].+?\[(\d)\]/.match(elem)
    curr += 1 if match[1].to_i > curr
    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



126
127
128
129
130
131
132
133
134
135
# File 'lib/relaton/render/template/template.rb', line 126

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



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

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