class Microstation::Template

Constants

EMPTY_ARRAY
EMPTY_HASH

Public Class Methods

new(template, output_dir: nil, app: nil, name: nil) click to toggle source
# File lib/microstation/template.rb, line 16
def initialize(template, output_dir: nil, app: nil, name: nil)
  @changer = Changer.new(template, output_dir: output_dir, app: app, name: name)
end

Public Instance Methods

change_template_text_in_cells(drawing, locals = {}) click to toggle source
# File lib/microstation/template.rb, line 55
def change_template_text_in_cells(drawing, locals = {})
  drawing.scan_cells do |c|
    c.text_elements do |text|
      new_text = update_liquid_text(text, locals)
      text.replace new_text if new_text != text.to_s
    end
  end
end
change_template_text_normal(drawing, locals = {}) click to toggle source
# File lib/microstation/template.rb, line 48
def change_template_text_normal(drawing, locals = {})
  drawing.scan_text do |text|
    new_text = update_liquid_text(text, locals)
    text.replace new_text if new_text != text.to_s
  end
end
normalize_hash(scope) click to toggle source
# File lib/microstation/template.rb, line 74
def normalize_hash(scope)
  scope = scope.to_h if scope.respond_to?(:to_h)
  scope = scope.transform_keys(&:to_s) if scope.is_a? Hash
  scope
end
render(name: nil, output_dir: nil, locals: EMPTY_HASH, tagsets: EMPTY_ARRAY) click to toggle source
# File lib/microstation/template.rb, line 24
def render(name: nil, output_dir: nil, locals: EMPTY_HASH, tagsets: EMPTY_ARRAY)
  return if locals == EMPTY_HASH && tagsets == EMPTY_ARRAY
  @changer.run(name: name, output_dir: output_dir) do |drawing|
    locals = normalize_hash(locals)
    update_text(drawing, locals)
    update_tagsets(drawing, tagsets)
  end
end
run(update = {}) click to toggle source
# File lib/microstation/template.rb, line 33
def run(update = {})
  @changer.run do |drawing|
    locals = normalize_hash(update)
    return if locals == {}

    update_text(drawing, locals)
    update_tagsets(drawing, tagsets)
  end
end
template() click to toggle source
# File lib/microstation/template.rb, line 20
def template
  @changer.template
end
update_liquid_text(text, locals) click to toggle source
# File lib/microstation/template.rb, line 64
def update_liquid_text(text, locals)
  update_hash = normalize_hash(locals)
  compiled = ::Liquid::Template.parse(text.to_s)
  new_text = begin
               compiled.render(update_hash)
             rescue StandardError
               text
             end
end
update_tagsets(drawing, ts_arg) click to toggle source
# File lib/microstation/template.rb, line 80
def update_tagsets(drawing, ts_arg)
  return if ts_arg == []
  return if ts_arg == {}

  ts_arg = [ts_arg] if ts_arg.instance_of?(Hash)
  ts_arg.each do |hash_pair|
    drawing.update_tagset(hash_pair.keys[0], hash_pair.values[0])
  end
end
update_text(drawing, locals = {}) click to toggle source
# File lib/microstation/template.rb, line 43
def update_text(drawing, locals = {})
  change_template_text_normal(drawing, locals)
  change_template_text_in_cells(drawing, locals)
end