Class: Relaton::Render::Template::Name
- Inherits:
-
General
- Object
- General
- Relaton::Render::Template::Name
show all
- Defined in:
- lib/relaton/render/template/template.rb
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
#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
135
136
137
138
139
140
141
|
# File 'lib/relaton/render/template/template.rb', line 135
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
175
176
177
178
179
180
181
|
# File 'lib/relaton/render/template/template.rb', line 175
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]
164
165
166
167
168
169
170
171
172
173
|
# File 'lib/relaton/render/template/template.rb', line 164
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
183
184
185
186
187
188
189
190
191
192
193
|
# File 'lib/relaton/render/template/template.rb', line 183
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
# File 'lib/relaton/render/template/template.rb', line 195
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
143
144
145
146
147
148
149
150
151
152
|
# File 'lib/relaton/render/template/template.rb', line 143
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
154
155
156
157
158
159
160
|
# File 'lib/relaton/render/template/template.rb', line 154
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
|